Question

In: Computer Science

Write a program in C to perform the following: Generates an array of 10 double random...

Write a program in C to perform the following:

  • Generates an array of 10 double random values between 1.0 and 100.0 – assume these are prices for 10 items that a store sells
  • Generates an array of 10 integer random values between 0 and 200 – assume that these are the number of items/units (first array) sold
  • Generate another array called “itemSale” which would contain 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 100, notify the user that this item (Item number from table below) needs to be reordered.
  • Display the following table:

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

  1. Must use arrays
  2. Must create a functions to generate random numbers for each array
  3. Must use function to calculate the sale of each item
  4. Must use appropriate loop to traverse the arrays

Solutions

Expert Solution

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:


Related Solutions

1. (50 pts) Write a C program that generates a 2D array-of-double and finds the indexes...
1. (50 pts) Write a C program that generates a 2D array-of-double and finds the indexes of the largest value stored in the 2D array. Specific requirements: (1) Your main function defines the 2D array a. You will need to prompt the user to specify the size of the 2D array b. You will need to prompt the user to put in numbers to initialize the array (2) Write a function to display the array that is visualized as rows...
In C programming: Write a program that initializes an array-of-double and then copies the contents of...
In C programming: Write a program that initializes an array-of-double and then copies the contents of the array into another arrays. To make the copy, use a function with array notation. This function takes two arguments the name of the target array and the number of elements to be copied. That is, the function calls would look like this, given the following declarations: double source[5] ={1.1, 2.2, 3.3., 4.4, 5.5}; double target1[5]; double target2[5]; copyarr(source, target1, 5);
Write a C++ program that 1) generates a vector containing 10 different random integers with values...
Write a C++ program that 1) generates a vector containing 10 different random integers with values between 1 and 100, then 2) calculates the average value in that vector in floating point format with 1 decimal place. Output the vector values and the average value to cout. Your program output should look like this: Vector values: 3, 78, 55, 37, 8, 17, 43, 60, 94, 1 Average value: 39.6
*Write in C* Write a program that generates a random Powerball lottery ticket number . A...
*Write in C* Write a program that generates a random Powerball lottery ticket number . A powerball ticket number consists of 5 integer numbers ( between 1-69) and One power play number( between 1-25).
Write a program in C++ coding that generates a random number between 1 and 500 and...
Write a program in C++ coding that generates a random number between 1 and 500 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Count the number...
C++. Write a program that generates a random number between 5 and 20 and asks the...
C++. Write a program that generates a random number between 5 and 20 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses the user makes until...
Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
Random Number Guessing Game C++. Write a program that generates a random number between 5 and...
Random Number Guessing Game C++. Write a program that generates a random number between 5 and 20 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses...
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT