Code:
#include <stdio.h>
#define SIZE 16
int lowest(double perOunce[]);
int highest(double *perOunce);
double average(double *perOunce);
void showCK(char type[],double weight[],unsigned long sku[],double price[],double perOunce[],char choice);
int main(void) {
char type[]={"CCCCCCCCCCCCCCKK"};
double weight[]={3.5,7.00,16.00,3.50,7.00,16.00,3.00,6.00,14.00,
3.00,6.00,6.00,15.00,3.15,3.50,14.00};
unsigned long sku[]={66311,62677,1235303,902829,902837,1235265,1145568,1145690,
1145550,1042629,1042602,1042580,1042599,1052187,146390,1056638};
double price[]={6.99,9.99,16.99,6.99,10.99,17.99,7.99,10.99,16.99,
5.49,9.99,9.99,20.99,7.99,6.99,17.99};
double perOunce[SIZE]={0.0}, average1;
char choice;
int i, idx, index[SIZE];
for(i=0;i<SIZE;i++) {
index[i]=i; /* set the index up for later sorting */
perOunce[i] = price[i]/weight[i]; /*calculate and store price per ounce */
}
do {
printf("\n\n\n\n\n\t\t Welcome to the Cat Food Info Program\n\n");
printf("\t Today's Cat Foods Are:\n\n");
printf("\t What would You like to See?\n\n");
printf("\t [L]owest price per ounce of all foods \n\n");
printf("\t [H]ighest price per ounce of all foods\n\n");
printf("\t [A]verage price per ounce of all foods\n\n");
printf("\t [C]at food info only\n\n");
printf("\t [K]itten food info only\n\n");
printf("\t Your choice (or Q to quit): ");
scanf("%c", &choice);
(void) getchar(); /* remove newline from input stream */
if(choice < 'a') choice += 32; /* change choice to lowercase, if it's uppercase */
else if(choice=='l')
i = lowest(perOunce);
else if(choice=='h')
i = highest(perOunce);
else if(choice=='a') {
average1 = average(perOunce);
printf("%.2lf", average1);
}
else if(choice=='c' || choice=='k')
showCK(type,weight,sku,price,perOunce,choice);
else
printf("\n I didn't understand that choice, please enter another\n\n");
if(choice=='l' || choice=='h')
printf(" %c %6.2f %10lu %6.2f %6.2f\n",type[i],weight[i],sku[i],price[i],perOunce[i]);
}while(choice != 'q');
return 0;
}
double average(double *perOunce) {
int i;
double avg;
for(i=0,avg=0.0;i<SIZE;i++) {
avg += *(perOunce+i); /* sum up all prices */
}
avg /= SIZE; /* divide to get the average */
printf("\n The average price of all our products is: ");
return avg;
}
int highest(double *perOunce) {
int i, hi;
double highest;
for(i=0,highest=*perOunce;i<SIZE;i++) {
if(*(perOunce+i) > highest)
hi = i;
}
printf("\n The highest price per ounce cat food is \n");
printf(" Type Pounds SKU Price Price Per Pound\n");
printf(" ===============================================\n");
return hi;
}
int lowest(double perOunce[SIZE]) {
int i, low;
double lowest;
for(i=0,lowest=perOunce[0];i<SIZE;i++) {
if(perOunce[i] < lowest)
low = i;
}
printf("\n The lowest price per ounce cat food is: \n");
printf(" Type Pounds SKU Price Price Per Pound\n");
printf(" ===============================================\n");
return low;
}
void showCK(char type[SIZE],double weight[SIZE],unsigned long sku[SIZE],double price[SIZE],double perOunce[SIZE],char choice) {
int i;
printf(" Type Pounds SKU Price Price Per Pound\n");
printf(" ===============================================\n");
choice -= 32; /* switch it to uppercase letter */
for(i=0;i<SIZE;i++) {
if(type[i]==choice)
printf(" %c %6.2f %10lu %6.2f %6.2f\n",type[i],weight[i],sku[i],price[i],perOunce[i]);
}
printf("\n\t\t press Enter to Continue");
(void) getchar();
}