Write a program to perform the following: Create an array of 7 double random values (the price of items) between 1.0 and 100.0. Create an array of 7 integer random values(units of items sold) between 0 and 100. Create an array called “total sale” which contains the total sale for each item. Calculate the total sale for this store and display it If the number of items sold is more than 50, print that item#? needs to be reordered.
C
Write a program to perform the following:
- Create an array of 7 double random values (the price of items) between 1.0 and 100.0.
- Create an array of 7 integer random values(units of items sold) between 0 and 100.
- Create an array called “total sale” which contains the total sale for each item.
- Calculate the total sale for this store and display it
- If the number of items sold is more than 50, print that item#? needs to be reordered.
Display Example; -------------------------------------------
item # | Price (random) | Units Sold (random) | Total Sale (price*units sold) |
1 | 25.23 | 2 | 50.46 |
2 | 3.45 | 100 | 345.00 |
3 | 40.56 | 34 | 1,379.04 |
: | : | : | : |
7 | 12.35 | 48 | 592.80 |
The total sale of this store is ???.
Item #3 needs to be reordered.
------------------------------------------------------------
1. Must create functions to generate random numbers for each array
2. Must use a function to calculate the sale of each item
3. Do not use Exit statement.
4. Do not use global variables.
5. Single return from a function.
Program code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
double doublerand(double low, double high ) /* function to generate double type random number */
{
srand((unsigned int)clock());
return ( (double)rand() * ( high - low ) ) / (double)RAND_MAX + low;
}
int intrand(int lower, int upper) /* function to generate integer type random number */
{
return (rand() % (upper - lower + 1)) + lower;
}
double sales(int unit, double price) /* function to compute sales */
{
return unit*price;
}
int main()
{
double Price[7];
int Units[7];
double totalSales[7], storeSales=0.0;
int i;
for(i=0;i<7;i++)
{
Price[i]=doublerand(1.0,100.0); /* function call to generate double type random number */
Units[i]=intrand(0,100); /* function call to generate integer type random number */
totalSales[i]=sales(Units[i],Price[i]); /* function call to calculate total sales */
storeSales +=totalSales[i]; /* calculate the store sales*/
}
printf("Display Example;--------------------------------------\n");
printf("item# Price(random) Units Sold(random) Total Sales(Price*Units Sold)\n");
for(i=0;i<7;i++)
printf("%-5d%7.2lf%15d%25.2lf\n",i+1,Price[i],Units[i],totalSales[i]); /* display details */
printf("\nThe total sale of this store is: %.2lf",storeSales);
for(i=0;i<7;i++)
{
if(Units[i]>50) /* check for units solds>50*/
printf("\nItem #%d needs to be recordered.",i+1);
}
printf("\n------------------------------------------------------");
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 3 images