CPSC150 EH18 1
CPSC150 EH18
Q1: Which statement below initializes array items to contain 3 rows and 2 columns?
a. int items[][] = { { 2, 4 }, { 6, 8 }, { 10, 12 } };.
b. int items[][] = { { 2, 6, 10 }, { 4, 8, 12 } };.
c. int items[][] = { 2, 4 }, { 6, 8 }, { 10, 12 };.
d. int items[][] = { 2, 6, 10 }, { 4, 8, 12 };.
Q2: Which of the following statements creates a multidimensional array with 3 rows, where the first row contains 1 value, the second row contains 4 items and the final row contains 2 items?
a. int items[][] = { { 1, null, null, null }, { 2, 3, 4, 5 }, { 6, 7, null, null } };.
b. int items[][] = { { 1 }, { 2, 3, 4, 5 }, { 6, 7 } };.
c. int items[][] = { { 1 }, { 2, 3, 4, 5 }, { 6, 7 }, {} );.
d. int items[][] = { { 1 }, { 4 }, { 2 } };.
Q3: Which of the following sets of statements creates a multidimensional array with 3 rows, where the first row contains 1 value, the second row contains 4 items and the final row contains 2 items?
a.
int items[][];
items = new int[ 3 ][ ? ];
items[ 0 ] = new int[ 1 ];
items[ 1 ] = new int[ 4 ];
items[ 2 ] = new int[ 2 ];
b.
int items[][];
items = new int[ 3 ][ ];
items[ 0 ] = new int[ 1 ];
items[ 1 ] = new int[ 4 ];
items[ 2 ] = new int[ 2 ];
c.
int items[][];
items = new int[ ? ][ ? ];
items[ 0 ] = new int[ 1 ];
items[ 1 ] = new int[ 4 ];
items[ 2 ] = new int[ 2 ];
d.
int items[][];
items[ 0 ] = new int[ 1 ];
items[ 1 ] = new int[ 4 ];
items[ 2 ] = new int[ 2 ];
Q4: Which set of statements totals the items in each row of two-dimensional array items, and displays each total?
a.
int total = 0;
for ( int row = 0; row < items.length; row++ )
{
total = 0;
for ( int column = 0; column < a[ row ].length; column++ )
total += a[ row ][ column ];
System.out.printf( "%d\n", total );
}
b.
int total = 0;
for ( int row = 0; row < items.length; row++ )
{
for ( int column = 0; column < a[ row ].length; column++ )
total += a[ row ][ column ];
System.out.printf( "%d\n", total );
}
c.
int total = 0;
for ( int row = 0; row < items.length; row++ )
{
for ( int column = 0; column < a[ column ].length; column++ )
total += a[ row ][ column ];
System.out.printf( "%d\n", total );
}
d.
int total = 0;
for ( int row = 0; row < items.length; row++ )
{
total = 0;
for ( int column = 0; column < a[ column ].length; column++ )
total += a[ row ][ column ];
System.out.printf( "%d\n", total );
}
Q5: Which set of statements traverses two-dimensional int array items?
a.
int total = 0;
for ( int subItems : items )
for ( int item : subItems )
total += item;
b.
int total = 0;
for ( int item: int subItems[] : items )
total += item;
c.
int total = 0;
for ( int subItems[] : items )
for ( int item : items )
total += item;
d.
int total = 0;
for ( int subItems[] : items )
for ( int item : subItems )
total += item;
Q6:
Use a two-dimensional array to solve the following program: A company has four salespeople (1 to 4) who sell five different products (1 to 5). Once a day, each salesperson passesin a slip for each type of product sold. Each slip contains the following:
a)The salesperson number
b)The product number
c) The total dollar value of that product sold that day
Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that the information from all the slips for last month is available. Write an application that will read all this information for last month’s sa;es amd summarize the total sales by salesperson and by products. All totals shouldbe stored in the two-dimensional array salles. After processing all the information for last month, display the results in tabular format, with each column representing a particular salesperson and each row representing a particular product. Cross-total each row to get the total sales of each product for last month. Cross-total each column to get the total sales by salesperson for last month. Your tabular output should include these cross-totals to the right of the tataled rows and to the bottom of the totaled columes.