The program will: a. Read monthly sales into a dynamically allocated array b. In
ID: 3758396 • Letter: T
Question
The program will:
a. Read monthly sales into a dynamically allocated array
b. Input the size of the array from the user
c. Call a function (called yearTotal) that will find the yearly sum (the sum of all the sales)
d. Print the yearly sum in the main function.
e. Call another function (called yearAverage) that will find the average.
f. Print the average in the main function.
2. OPTIONAL +5 BONUS POINTS a. If you want some extra credit, print out some ASCII art before reading in the monthly sales of a snowman.
Sample Output:
Explanation / Answer
import java.util.*;
class monthlysales
{
double yeartotal(double[] a, int n)
{
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+a[i];
}
return(sum);
}
double yeartotal(double[] a, int n)
{
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+a[i];
}
avg=sum/(double)n;
return(avg);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double a[]=new int[50];
System.out.println("Please input the number of monthly sales to be input : ");
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
System.out.println("Please input the sales for month "+i +" ");
a[i]=sc.nextDouble();
}
monthlysales m=new monthlysales();
double totalsales=yeartotal( a,n);
double yearAverage=yearAverage( a,n);
System.out.println("The total sales for the year is "+totalsales);
System.out.println("The average Monthly sales is "+yearAverage);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.