DOC PREVIEW
WVU CS 110 - Using Arrays

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

An ApplicationJoe’s Gift Cart Monthly Product SalesSlide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Input the product names and sales for EACH product.Slide 11Calculate the total sales for each productSlide 13Calculate the total sales for each month.Slide 15Print the reportSlide 17Slide 18An ApplicationUsing ArraysJoe’s Gift CartMonthly Product SalesJoe’s Gift Cart sells 5 gift products. Joe wants us to develop a program to track the number of each product sold for the first six months of this year. At the end of the period, Joe wants us to show:•The product name and total number sold for the period. •He also wants to know the total number of products sold by month.•He wants the report to look like:Product Name Jan Feb Mar April May June total salesballs 10 15 20 33 10 33 121hats 5 11 11 11 11 11 60total 15 25 31 44 21 44•We will need to store the following data items.•Five product names•Six months of sales figures for the five products (30 values)•Total sales for each product (5 values)•Total products sold for each month (6 values)•We can declare a variable for each value to be stored•Or•We can use arrays100 90 75 110 60 1208012379200582555BallsHatsBearsT-ShirtsMugsProduct NamesSales per monthTotal Product SalesTotal sales per month•To store the names we need a 5 X 1 array of Strings. Or simply a 5 element array•To store the monthly totals we need a 1 X 6 array of integers. Or simply a 6 element array•To store the total sales for each product, we need a 5 X 1 array of integers. Or simply a 5 element array•To store the products sold for each month we need a 5 X 6 array of integers.Final int numProducts = 5;Final int numMonths = 6;int[][] salesArray = new int[numProducts] [numMonths];String[] productNames = new String[numProducts];int[] monthlySalesTotals = new int[numMonths];int[] productSalesTotals = new int[numProducts];•The calculations we need to perform are:•Input the product names and sales for EACH product.•Calculate the total sales for each product•Calculate the total sales for each month.•Print the reportInput the product names and sales for EACH product.•We need to loop through all 5 products. Then for each product•We need to enter the products name•Loop through each month, entering sales figures for each month/* Enter the product names, and sales */for (i=0; i< numProducts; i++) { System.out.println("Please enter the products name: "); productNames[ i ] = input.nextLine(); for (j=0; j < numMonths; j++) { System.out.println("Please enter the sales for Product " + productNames[ i ] + " for month " + (j+1) + " "); salesArray[ i ][ j ] = input.nextInt(); } // end inner loop input.nextLine();}// end outter loopCalculate the total sales for each product•For each product:•Loop through each month and sum the sales.•Store the sum in the productSalesTotal element for that product. Product 1 in element 0, etc/* calculate the total sales for each product */ for (i=0; i< numProducts; i++) { sum = 0; for (j=0; j< numMonths; j++) { sum += salesArray[ i ][ j ];} // end inner loopproductSalesTotals[ i ] = sum;} //Calculate the total sales for each month.•For each month•Add together the sales for each product•Store the sum in the element of the array monthlySalesTotals for that month. January = 0, February=2, etc/* calculate total sales for each month */for (j = 0; j < numMonths; j++) { sum = 0; for (i =0; i < numProducts; i++ ) { sum += salesArray[ i ][ j ]; }// end inner loop monthlySalesTotals[ j ] = sum; } // end outter loopPrint the report•Print Headings•For each product•Print the product name•Print the sales for each month•Print the total sales for that product•Print the totals sales for each monthSystem.out.printf("%10s \t%8s \t%8s \t%8s \t%8s \t%8s \t%8s \t%8s\n", "Product", “January", "February", "March", "April", "May", "June", "Total");for (i=0; i< numProducts; i++) {System.out.printf("%10s", productNames[ i ]);for(j=0; j < numMonths; j++) {System.out.printf("\t%8d", salesArray[ i ][ j ]);}// end inner loopSystem.out.printf("\t%8d \n", productSalesTotals[ i ]);}//end outter loopSystem.out.printf("%10s", "Total"); for (j=0; j< numMonths; j++)


View Full Document

WVU CS 110 - Using Arrays

Download Using Arrays
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Using Arrays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Using Arrays 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?