/* File: pgm4.c
Programmer: Mark Fienup
Homework assignment #4 with arrays
October 2004
Description: This program tallies the weekly sales for a mail-order house.
The mail order house sells five different products whose retail prices are
stored in the array retailPrices. The program should read a series of
product number and quantity sold pairs. A sentinel-value of -999 is used
for the product number to stop input.
*/
#include <stdio.h>
#define MAX_PRODUCT 5 // Total number of products
// function prototypes
void InitializePrices( float retailPrices[] );
void ReadProductsAndQuantities( int productQuantities[] );
void PrintSummary( float retailPrices[], int productQuantities[] );
int main() {
float retailPrices[MAX_PRODUCT + 1]; // we'll waste element 0
int productQuantities[MAX_PRODUCT + 1] = {0};
InitializePrices(retailPrices);
ReadProductsAndQuantities(productQuantities);
PrintSummary(retailPrices, productQuantities);
} // end main
/* This function initializes the retail prices in the array retailPrices.
Product 1's price is at index 1, etc.
*/
void InitializePrices( float retailPrices[] ) {
retailPrices[1] = 3.44;
retailPrices[2] = 5.02;
retailPrices[3] = 2.14;
retailPrices[4] = 6.30;
retailPrices[5] = 7.11;
} // end InitializePrices
/* This function reads the product # and quantity pairs from the user until a
sentinel value of -999 is read. The quantities of each product are tallied
in the corresponding productsQuantities element, i.e., product 1's quantity
tally is stored at productQuantities[1], etc.
*/
void ReadProductsAndQuantities( int productQuantities[] ) {
int productNumber; // Users choice number of what product
int quantity; // Number of units sold
printf("Enter the a product number (or -999 to quit): ");
scanf("%d", &productNumber);
while (productNumber != -999) {
if ( productNumber >= 1 && productNumber <= MAX_PRODUCT) {
printf("How many were purchased? ");
scanf("%d", &quantity);
productQuantities[productNumber] += quantity;
} else {
printf("Product number must be between 1 and %d!\n", MAX_PRODUCT);
} // end if
printf("Enter the a product number (or -999 to quit): ");
scanf("%d", &productNumber);
} // end while
} // end ReadProductsAndQuantities
/* This function calculates and displays the total retail sales for
each products individually and the total overall sales.
*/
void PrintSummary( float retailPrices[], int productQuantities[] ) {
int index;
double totalSales;
totalSales = 0.0;
printf("\n \n SUMMARY OF SALES\n");
for (index = 1; index <= MAX_PRODUCT; index++ ) {
printf("There were %d Product %d's sold. Totaling $%.2f. \n\n",
productQuantities[index], index,
productQuantities[index] * retailPrices[ index ] );
totalSales += productQuantities[index] * retailPrices[ index ];
} // end for
printf("TOTAL SALES: $ %.2f \n", totalSales);
} // end PrintSummary