Originally created by Idel Martinez and Jerrett Longworth in Fall 2020.
Here is some additional practice using structs. Some of these might take a little bit of thinking, but this practice is here to help you get more comfortable with the material.
But first, some typedef
fun.
typedef
Practice100.00 - 89.00 = 11.00
?int main(void)
{
typedef double more_precise_number;
typedef float less_precise_number;
more_precise_number x = 100;
less_precise_number y = 89;
printf("%___ - %___ = %___\n", x, y, x - y);
return 0;
}
main()
? Would it compile at all? Assume all necessary
libraries are included.struct my_structure
{
int x;
char text[64];
};
int main(void)
{
struct my_structure structy_mc_struct_face;
("%d\n", structy_mc_struct_face.x);
printf
return 0;
}
struct my_structure
{
int x;
char text[64];
};
int main(void)
{
struct my_structure structy_mc_struct_face;
structy_mc_struct_face.x = 65;
printf("%c\n", structy_mc_struct_face.x);
return 0;
}
My hands are aching from writing code 24/7. I alone kept the 2018
electoral interference at bay. How can I save myself the 6 keystrokes
and stop typing struct
every time I want to create an
instance of the my_structure
struct? See the previous
question on line 9 for reference.
Good. The world is still going to be safe, but now I have a new (completely unrelated) problem. Suppose I am teaching an introduction to C programming class at a university, but for some reason this university didn’t pick a system to store course material and grades. (Something called “Webcourses” was suggested, but it didn’t seem to catch on.) That said, now I want to create my own system of storing student grades. I have some basic code to get started, but it needs some work. How can I set the names of my 3 students in class to “Alice”, “Bob”, and “Charlie”?
struct gradebook
{
char student[128];
int grades[10];
double avg;
};
typedef struct gradebook gradebook;
int main(void)
{
int n = 3;
[n];
gradebook cop3223
// Your code goes here
}
struct gradebook
{
char student[128];
int grades[10];
double avg;
};
typedef struct gradebook gradebook;
void calculate_student_avg(gradebook *class, _____)
{
double sum;
for (int i = 0; i < _____; i++)
{
= _____;
sum for (int j = 0; j _____ 10; j++)
{
+= class[i].grades[_____];
sum }
[i].avg = sum / _____;
class}
}
void print_averages(_____, _____)
{
for (int i = 0; _____; i++)
{
("Average for _____ is %lf\n", _____, _____[i].avg);
printf}
}
int main(void)
{
int n = 3;
[n];
gradebook cop3223
// Filling in student grades with data
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 10; j++)
{
[i].grades[j] = (i + 1) * (j + i);
cop3223}
}
// Add student names
(cop3223[0].student, "Frances E. Allen");
_____(__________________, "Barbara Liskov");
_____
_____________________________________________// Find average
(_____, n);
calculate_student_avg
// Print average
(_____, _____);
print_averages
return 0;
}
deposit
and withdraw
functions don’t work. How can I solve this?struct balance
{
int dollars;
int cents;
double total;
};
void deposit(struct balance *checking, double amount)
{
(&checking).dollars += amount;
}
void withdraw(struct balance *checking, double amount)
{
*checking.dollars -= amount;
}
int main(void)
{
struct balance my_checking;
(&my_checking, -100);
deposit(&my_checking, 1E6);
withdraw
(&my_checking, 145.54);
deposit(&my_checking, 0.01);
withdraw
return 0;
}