This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: EME5 Homework 9 Solutions November 29, 2010 3 Chapter 13, Problem 1 3.1 Part c False. enum by default begins with a 0. 3.2 Part d True. 4 Chapter 13, Problem 5 4.1 Part a struct inventory { char partName[30]; int partNumber; float price; int stock; int reorder; }; 5 Chapter 13, Problem 6 5.1 Solution Code #include <stdio.h> struct Product{ char name[10]; double price; double weight; }s[4]; 1 double totalSale(struct Product fruit) { int n; double total = 0; for (n = 0;n < 4;n++) total += fruit[n].price*fruit[n].weight; return total; } int largestWeight(struct Product fruit) { int n, p; double weight = fruit[0].weight; for (n = 0;n < 4;n++) { if (weight < fruit[n].weight) { weight = fruit[n].weight; p = n; } } return p; } int largestDollar(struct Product fruit) { int n, p; double dollar = fruit[0].weight*fruit[0].price; for (n = 0;n < 4;n++) if (dollar < fruit[n].weight*fruit[n].price) { dollar = fruit[n].weight * fruit[n].price; p = n; } return p; } int main() { char line[200]; double totalsale; int i, weightindex, dollarindex; fgets(line, 200, stdin); /* skip the comment line */ printf("%s", line); for (i = 0;i < 4;i++) /* input data */ scanf("%s%lf%lf", s[i].name, &s[i].price, &s[i].weight); 2 for (i = 0;i < 4;i++) /* print out to verify the input data */ printf("%6s %8lf %8lf\n", s[i].name, s[i].price, s[i].weight); totalsale = totalSale(s); printf("\nThe total sale for a day is $%.2lf\n", totalsale); weightindex = largestWeight(s); printf("The largest weight sold is %s with %.2lf pound.\n", s[weightindex].name, s[weightindex].weight); dollarindex = largestDollar(s); printf("The product with the largest dollar amount sold is ’%s’.\n", s[dollarindex].name); return 0; } 5.2 Solution Output # product name, unit price ($), units (pound) sold orange 1.200000 506.210000 apple 1.250000 944.230000 grape 2.250000 393.500000 cherry 4.250000 234.560000 The total sale for a day is $3669.99 The largest weight sold is apple with 944.23 pound. The product with the largest dollar amount sold is ’apple’. 6 Chapter 13, Problem 14 6.1 Solution Code #include <stdio.h> /* define a sentinel number for end of data */ #define EOD 1 struct Student { int id; char name[32]; double gpa; }; double averageGPA(struct Student *sp) { double gpa, totalval; int i; totalval=0.0; 3 for(i=0; sp>id != EOD; i++, sp++) { totalval += sp>gpa; } gpa = totalval/i; return gpa; } int main() { struct Student s = {{101, "John", 3.33}, {102, "Doe", 3}, {103, "Peter", 3.1}, {EOD, NULL, EOD}}; printf("GPA for students in the list is %.2f\n", averageGPA(s)); return 0; } 6.2 Solution Output GPA for students in the list is 3.14 7 Chapter 13, Problem 19 The value returned is 13. The cast operation is not necessary....
View
Full
Document
This note was uploaded on 04/23/2011 for the course EME 5 taught by Professor Staff during the Fall '08 term at UC Davis.
 Fall '08
 Staff

Click to edit the document details