Question: /* FILE: struct5.c */ /* Arrays can be used with structures just like any other C data type. */ #include #define SIZE 5 struct part{
/* FILE: struct5.c */
/* Arrays can be used with structures just like any other C data type.
*/
#include
#define SIZE 5
struct part{ char name[124]; long no; double price; }; void print_part(struct part p);
int main( ) {
struct part board; struct part inventory[SIZE];
int i;
/* One "part" */ /* Array to hold SIZE "part"s */
for(i=0; i < SIZE; i++){ sprintf(board.name,"I/O card #%d", i); board.no = 127356 + i; board.price = 99.50 + i*3; inventory[i] = board; }
for(i=0; i < SIZE; i++){ print_part(inventory[i]); printf(" "); /* Display the array of structures. */
}
return 0; }
void print_part(struct part p) { printf("Product: %s ", p.name); printf("Part No.: %ld ", p.no); printf("Unit price: %.2f ", p.price); return; }
/* FILE: struct6.c */
/* The address of a structure can be passed to a function just like any other C data type. */
#include
#define SIZE 5
struct part{ char name[124]; long no; double price; }; void print_part(struct part* p);
int main( ) {
struct part board; struct part inventory[SIZE];
int i;
for(i=0; i < SIZE; i++){ sprintf(board.name,"I/O card #%d", i); board.no = 127356 + i; board.price = 99.50 + i*3; inventory[i] = board; }
print_part(&board); printf(" "); for(i=0; i < SIZE; i++){ print_part(&inventory[i]); printf(" "); /* print_part( ) expects an address. */ /* Display the array of structures. */
}
return 0; }
void print_part(struct part* p) { printf("Product: %s ", (*p).name); printf("Part No.: %ld ", (*p).no); printf("Unit price: %.2f ", (*p).price); return; }
Exercise #7: ------------ - Add the print function from the struct5.c C program to the struct6.c C program. Save the file as a C++ program. Now there are two functions with the same name (different parameter lists) in the same program. This is an example of an overloaded function. - compile it, to see that the C++ compiler is not confused by two functions with the same name - slightly modify the program so that it will call both functions when it runs - compile and run the program to see that it works
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
