In: Computer Science
Write a program in C to perform the following:
Item Price/unit units sold Total
1 33.45 2 66.9
2 435.22 5 2176.1
3 55.12 100 551.2
.. … … …
____________________________________________
Total store sale: ??????
Requirement
Code:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//declaring variables
int price[10];
double item[10], itemSale[10], total=0;
//function to generate random double values
double randfrom(double low, double high)
{
//range for the value
double range = (high - low);
double div = RAND_MAX / range;
//returing the double value
return low + (rand() / div);
}
//function to generate number of sold items array
void generateItem()
{
int i;
for(i=0;i<10;i++)
{
//storing the random integer values
item[i] = rand() % 100 + 1;
}
}
//function to generate the price array
void generatePrice()
{
int i;
srand((unsigned)time(NULL));
for(i=0;i<10;i++)
{
//calling the randfrom function to store the double values
price[i] = randfrom(0,200.0);
}
}
//function to calculate the sale of each item
double calculateSale(int it, double pr)
{
return(it*pr);
}
//function to show the sale details
void show()
{
int i=0;
printf("\n Item \t Price/unit \t Units Sold \t Total");
//printing the values using loop
for(i=0;i<10;i++)
{
printf("\n %d \t %0.2f \t\t %d \t\t %0.1f",(i+1),(price[i]),item[i],itemSale[i]);
}
printf("\n------------------------------------------------");
//printing the total sales
printf("\nTotal Store Sales=\t\t\t%0.1f",total);
printf("\n------------------------------------------------");
}
//main function
int main()
{
int i;
//calling the functions to store array values
generatePrice();
generateItem();
for(i=0;i<10;i++)
{
//calculating sales using function
itemSale[i]=calculateSale(item[i],price[i]);
//calculating the total sales
total=total+sale[i];
}
//calling the show function
show();
//condition for items over 100
for(i=0;i<10;i++)
{
if(item[i]>=100)
{
printf("\nItem No: %d need to be reordered.",(i+1));
}
}
return 0;
}//end of the program
Output:
(i) The above code is divided in different functions as mentioned in the case study in order to calculate total sales generated. The variables for number of items item, for price of the items price, for sale of each item itemSale array is declared globally along with other variable total which will store the total amount of sale generated.
(ii) The function generateItem generates random integers using the function rand() and stored it into the array item and for generating random double array two functions are written. One function randfrom calculates the double value within the specified range usig rand and randmax and returns the value. In another function named generatePrice the randfrom is called within a for loop and stored into the array price.
(iii) CalculateSale takes two arguments one is sale and another one is number of items and it returns the multiplication of them. From the main function generateItem and generatePrice is called after that within a for loop the sales for each item is calculated by function calling and stored into the array named itemSale and along with the total sales is calculated.
(iv) The show function displays the entire information in table format. Here precision is set as described in the test case mentioned in the case study. After calling the show function from main, number of items are checked using a for loop. If found 100 or greater than 100 appropriate message is displayed along with the item number mentioned in the table.
Screenshot of the Code: