Write a program,that will read monthly sales into a dynamically allocated array.
ID: 3712696 • Letter: W
Question
Write a program,that will read monthly sales into a dynamically allocated array. The program will input the size of the array from the user. It will call a function that will find the yearly sum (the sum of all thesales). It will also call another function that will find the average.
#include<iostream>
using namespace std;
void yearTotal(float * sales, int saleSize, float & sum); // function that calculates yearlytotal
void yearAverage(float sum, int saleSize, float& avg);
// function that calculates average
int main()
{
// Complete program here
}
a) Use the sample run test data below to test your program.
b) Make sure your program handles 0 sales being entered, and negative sales amounts.
Explanation / Answer
PLEASE REFER BELOW CODE
#include<iostream>
using namespace std;
void yearTotal(float * sales, int saleSize, float & sum) // function that calculates yearlytotal
{
sum = 0.0;
for(int i = 0; i < saleSize; i++)
sum += sales[i];
}
void yearAverage(float sum, int saleSize, float& avg)
{
avg = sum / saleSize;
}
int main()
{
int saleSize;
float *sales,sum,avg;
cout<<"Enter the array size : ";
cin>>saleSize;
if(saleSize == 0)
{
cout<<"Cannot create zero size array"<<endl;
return 0;
}
else
{
sales = new float[saleSize];
cout<<"Enter your data now :"<<endl<<endl;
for(int i = 0; i < saleSize; i++)
{
cout<<"Enter "<<(i+1)<<" Monthly data ";
cin>>sales[i];
}
}
yearTotal(sales, saleSize, sum);
yearAverage(sum, saleSize, avg);
cout<<"year Total = "<<sum<<endl;
cout<<"Average = "<<avg;
return 0;
}
PLEASE REFER BELOW OUTPUT
Enter the array size : 6
Enter your data now :
Enter 1 Monthly data 20
Enter 2 Monthly data 10
Enter 3 Monthly data 30
Enter 4 Monthly data 45
Enter 5 Monthly data 21
Enter 6 Monthly data 40
year Total = 166
Average = 27.6667
Process returned 0 (0x0) execution time : 19.831 s
Press any key to continue.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.