/*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none} Problem 9 (Dotable Array Questions) Consid... [FREE SOLUTION] | 91Ó°ÊÓ

91Ó°ÊÓ

(Dotable Array Questions) Consider a \(2-\mathrm{by}-3\) integer array t. a) Write a declaration for t. b) How many rows does t have? c) How many columns does t have? d) How many elements does t have? e) Write the names of all the elements in row 1 of \(t\) f) Write the names of all the elements in column 2 of t. g) Write a statement that sets the element of t in the first row and second column to zero. h) Write a series of statements that initialize each element of t to zero. Do not use a loop. i) Write a nested for statement that initializes each element of to zero. j) Write a statement that inputs the values for the elements of trom the keyboard. k) Write a series of statements that determine and print the smallest value in array t. l) Write a statement that displays the elements in row 0 of t. \(\mathrm{m}\) ) Write a statement that totals the elements in column 3 of t. n) Write a series of statements that prints the array t in neat, tabular format. List the column subscripts as headings across the top and list the row subscripts at the left of each row.

Short Answer

Expert verified
a) int t[2][3]; b) 2 rows; c) 3 columns; d) 6 elements; e) t[1][0], t[1][1], t[1][2]; f) t[0][1], t[1][1]; g) t[0][1] = 0; h) t[0][0] = 0, ..., t[1][2] = 0; i) for (int i=0; i<2; i++) { for (int j=0; j<3; j++) { t[i][j] = 0; } } j) Use input statements in a loop; k) Determine the smallest value with comparisons; l) Print t[0][0], t[0][1], t[0][2]; m) int sum = t[0][2] + t[1][2]; n) Use nested loops with print statements.

Step by step solution

01

Declare the array

Declare a 2-by-3 integer array named 't'. In most programming languages, this can be done by specifying the type of the elements, the name of the array, and the size of each dimension. For example, in C++/Java: `int t[2][3];`.
02

Determine the number of rows

Count the number of rows in array 't'. Since it is a 2-by-3 array, it has 2 rows.
03

Determine the number of columns

Count the number of columns in array 't'. Since it is a 2-by-3 array, it has 3 columns.
04

Calculate the number of elements

Multiply the number of rows by the number of columns to find the total number of elements in 't'. For a 2-by-3 array, there are 2 * 3 = 6 elements.
05

Identify elements in row 1

List the names of all elements in row 1 of 't' considering it to be a zero-indexed array. The elements are 't[1][0]', 't[1][1]', and 't[1][2]'.
06

Identify elements in column 2

List the names of all elements in column 2 of the array 't'. The elements are 't[0][2]' and 't[1][2]'.
07

Set a specific element to zero

Set the element of 't' in the first row and second column to zero. If the array is zero-indexed, use the statement `t[0][1] = 0;`.
08

Initialize elements to zero without a loop

Assign the value zero to each element of 't' without using a loop. In a 2-by-3 array, this would look like: `t[0][0] = 0; t[0][1] = 0; t[0][2] = 0; t[1][0] = 0; t[1][1] = 0; t[1][2] = 0;`.
09

Initialize elements to zero with a nested loop

Write a nested `for` loop to initialize all elements of 't' to zero. It will have one loop iterating over rows and another over columns, each setting the current element to zero.
10

Input values from the keyboard

Write a series of statements or a loop structure to input values from the keyboard into the 't' array. Ensure that each element is addressed correctly.
11

Find and print the smallest value

Initialize a variable to hold the smallest value. Compare each element of 't' with this variable and replace the variable's value if a smaller element is found. Print the smallest value after all elements are compared.
12

Display elements in row 0

Write a statement that iterates through the elements in the zeroth row of 't' and prints them. This can typically be done with a loop over the columns in row 0.
13

Total the elements in column 3

Write a statement that sums the elements in the third column of 't'. Since it's a 2-by-3 array, this would involve adding 't[0][2]' and 't[1][2]'.
14

Print the array in tabular format

Use nested loops to iterate through the rows and columns, printing each element in a structured format. Print the column indices at the top and row indices at the start of each row for clarity.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with 91Ó°ÊÓ!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Array Declaration in C++
In C++, an array is a collection of items stored at contiguous memory locations and accessed using an index. When you declare an array, you're creating a variable that can hold several values of the same type. This is crucial for managing data sets efficiently within a program.

An array declaration involves the following components:
  • Type: Indicates the data type of the elements (e.g., int, float).
  • Name: The name of the array variable.
  • Size: Specifies the number of elements the array will hold.
For example, declaring a single-dimensional array of integers named 'numbers' with space for 10 elements looks like this: int numbers[10];

Following the textbook exercise, a two-dimensional array 't' with two rows and three columns is declared as: int t[2][3];. This creates a grid or matrix of integers where you can store values in a tabular format. Each element in this array can be uniquely identified with a pair of indices – the first for the row and the second for the column.
Nested Loops in C++
Nested loops are a fundamental concept in C++ when dealing with situations that require repetition inside another repetition. They are commonly used with multidimensional arrays to perform operations like initialization, inputting values, or displaying contents.

In a nested loop structure, usually, the outer loop represents rows, while the inner loop represents columns. Here's a simple example of a nested loop that initializes each element of a 2-by-3 array to zero:
for(int i = 0; i < 2; i++) {    for(int j = 0; j < 3; j++) {        t[i][j] = 0;    }}
This series of loops runs through each row (i) and column (j), setting the value at that position to zero. Nested loops are a powerful tool but should be used judiciously as they can create complexity and increase the execution time of a program if not handled correctly.
Multidimensional Arrays
Multidimensional arrays are arrays that contain more than one level of indices to access data. In C++, these are basically arrays of arrays. The most common type is the two-dimensional array, which can be visualized as a table with rows and columns.

For instance, array 't' from our exercise can be imagined as a table with two rows and three columns. Here's how you can access the element on the first row and second column: t[0][1].

A major application of multidimensional arrays is representing matrices in mathematics or other grid-based data structures. They also come in handy for organizing complex data that's logically structured in a tabular format.

A common mistake for beginners is to forget that C++ arrays are zero-indexed, meaning that indexing starts at zero. Thus, for a 2-by-3 array like 't', valid indices range from 't[0][0]' (top-left) to 't[1][2]' (bottom-right). To efficiently work with these arrays, nested loops are indispensable tools.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

(Fill in the Blanks) Fill in the blanks in each of the following: a) The names of the four elements of array p (int p[4];) are ___, ___ , ___ and ___. b) Naming an array, stating its type and specifying the number of elements in the array is called __ the array. c) By convention, the first subscript in a two-dimensional array identifies an element's ___ and the second subscript identifies an element's ___. d) An m-by-n array contains __ rows, __ columns and ___ elements. c) The name of the element in row 3 and column 5 of array \(d\) is __.

( The Sieve of Eratosthenes) A prime integer is any integer that is evenly divisible only by itself and 1\. The Sieve of Eratosthenes is a method of finding prime numbers. It operates as follows: a) Create an array with all elements initialized to 1 (true). Array elements with prime subscripts will remain \(1 .\) All other array elements will eventually be set to zero. You'll ignore elements 0 and 1 in this exercise. b) Starting with array subscript 2 , every time an array element is found whose value is 1 loop through the remainder of the array and set to zero every element whose subscript is a multiple of the subscript for the clement with value 1 . For array subscript \(2,\) all elements beyond 2 in the array that are multiples of 2 will be set to zero (subscripts 4,6 \(8,10, \text { etc. }) ;\) for array subscript \(3,\) all elements beyond 3 in the array that are multiples of 3 will be set to zero (subscripts \(6,9,12,15,\) etc.); and so on. When this process is complete, the array elements that are still set to one indicate that the subscript is a prime number. These subscripts can then be printed. Write a program that uses an array of 1000 elements to determine and print the prime numbers between 2 and \(999 .\) Ignore element 0 of the array.

(Duplicate Elimination) Use a one-dimensional array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100 , inclusive. As each number is read, validate it and store it in the array only if it isn't a duplicate of a number already read. After reading all the values, display only the unique values that the user entered. Provide for the "worst case" in which all 20 numbers are different. Use the smallest possible array to solve this problem.

(Double Array Initialization) Label the elements of a 3 -by- 5 two-dimensional array sales to indicate the order in which they're set to zero by the following program segment: for ( row = 0; row < 3; ++row ) for ( column = 0; column < 5; ++column ) sales[ row ][ column ] = 0;

( Sales Summary ) Use a two-dimensional array to solve the following problem. A company has four salespeople ( 1 to 4 ) who sell five different products ( 1 to 5 ). Once a day, each salesperson passes in a slip for cach different 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 of the slips for last month is available. Write a program that will read all this information for last month's sales (one salesperson's data at a time) and summarize the total sales by salesperson by product. All totals should be stored in the two-dimensional array sales. After processing all the information for last month, print the results in tabular format with cach of the columns representing a particular salesperson and each of the rows 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 printout should include these cross totals to the right of the totaled rows and to the bottom of the totaled columns.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.