(Double Array Questions) Answer the following questions regarding an array called table: a) Declare the array to be an integer array and to have 3 rows and 3 columns. Assume that the constant variable arraysize has been defined to be 3 b) How many elements does the array contain? c) Use a for statement to initialize each element of the array to the sum of its subscripts. Assume that the integer variables \(i\) and \(j\) are declared as control variables. d) Write a program segment to print the values of each element of array table in tabular format with 3 rows and 3 columns. Assume that the array was initialized with the declaration int table[ arraySize ][ arraySize ] = { { 1, 8 }, { 2, 4, 6 }, { 5 } }; and the integer variables i and j are declared as control variables. Show the output.

Short Answer

Expert verified
a) int table[arraysize][arraysize]; b) 9 elements c) for(i = 0; i < arraysize; i++) { for(j = 0; j < arraysize; j++) { table[i][j] = i + j; } } d) The output will be a 3x3 table reflecting the initial values and any uninitialized elements will be set to zero.

Step by step solution

01

- Declaring the array

Declare the array with the name 'table' as an integer 2-dimensional array with 3 rows and 3 columns using the provided constant variable 'arraysize'. In C-like languages, you would write: int table[arraysize][arraysize];
02

- Calculating the total number of elements

Calculate the total number of elements in the array by multiplying the number of rows by the number of columns. Since the array is a square with 'arraysize' rows and 'arraysize' columns, you have a total of arraysize * arraysize elements.
03

- Initializing the elements

Write a nested for loop to iterate over the array indexing variables 'i' and 'j' and set each element of 'table' to the sum of its indices. The code will look like this: for(i = 0; i < arraysize; i++) { for(j = 0; j < arraysize; j++) { table[i][j] = i + j; } }
04

- Printing the array elements

Write a nested for loop similar to the one in the previous step but this time to print the values of each element in the array 'table' in a 3x3 tabular format. You'll also need to handle the formatting to make sure elements are properly aligned. The code will look like this: for(i = 0; i < arraysize; i++) { for(j = 0; j < arraysize; j++) { printf('%d ', table[i][j]); } printf(''); }

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 Vaia!

Key Concepts

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

2D Arrays in C++
In C++, a 2D array is essentially an array of arrays, often used to represent a matrix or a grid. 2D arrays are declared with two dimensions, the first specifying the number of rows and the second the number of columns. Easy to visualize as a table, a 2D array is structured in rows and columns, just like a chessboard or an excel sheet.

For example, if we want to declare a 2D array called 'table' that consists of 3 rows and 3 columns, we would write it as:
int table[3][3]; 
Here, we've specifically used the number 3, but if a constant variable like 'arraysize' is defined, you can use it to set both dimensions to ensure consistent array sizes.
In the provided exercise, since 'arraysize' equals 3, our declaration would look like this:
int table[arraysize][arraysize]; 
This declaration sets up our grid-like structure where 'arraysize' indicates both the number of rows and columns.
Nested For Loops in C++
Whenever we work with 2D arrays, nested for loops become our essential tool. These are simply loops within loops that allow us to iterate over each row and then each column within that row. This is similar to traversing each cell in our table grid one by one.

Let's look at how we would initialize our array 'table' using nested loops:
for(int i = 0; i < arraysize; i++) {   for(int j = 0; j < arraysize; j++) {     table[i][j] = i + j;   }}
This code will set each element of 'table' to the sum of its indices. The outer for loop iterates through each row, while the inner loop iterates through each column of the current row. It's like going through each box of a checkers board, row by row and within those rows, square by square.
Array Indexing in C++
Array indexing is how we access elements within our array using their location. In a 2D array, the location of an element is specified by two indices: one for the row and one for the column. The indices usually start from 0 in C++, so the first element is at index [0][0], the next element in the same row but next column is at [0][1], and so forth.

Considering 'table' as our example 2D array, the expression 'table[i][j]' refers to the element in the 'i-th' row and 'j-th' column. In the initialization loop, 'i' and 'j' are the control variables used to index into 'table'. In the provided exercise, each element of 'table' is set to the sum of its row index and column index, demonstrating a direct application of array indexing in array manipulation.
When printing the array, we again make use of indexing within a nested loop structure to access each element and print it, resulting in a formatted output on screen.

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

(Bucket Sort) A bucket sort begins with a one-dimensional array of positive integers to be sorted and a two-dimensional array of integers with rows subscripted from 0 to 9 and columns subscripted from 0 to \(n-1\), where \(n\) is the number of values in the array to be sorted. Each row of the two- dimensional array is referred to as a bucket. Write a function bucketsort that takes an integer array and the array size as arguments and performs as follows: a) Place each value of the one-dimensional array into a row of the bucket array based on the value's ones digit. For example, 97 is placed in row 7,3 is placed in row 3 and 100 is placed in row \(0 .\) This is called a "distribution pass." b) Loop through the bucket array row by row, and copy the values back to the original array. This is called a "gathering pass." The new order of the preceding values in the onedimensional array is 100,3 and 97 c) Repeat this process for cach subsequent digit position (tens, hundreds, thousands, etc.). On the second pass, 100 is placed in row 0,3 is placed in row 0 (because 3 has no tens digit) and 97 is placed in row \(9 .\) After the gathering pass, the order of the values in the one-dimensional array is 100,3 and \(97 .\) On the third pass, 100 is placed in row 1,3 is placed in row zero and 97 is placed in row zero (after the 3). After the last gathering pass, the original array is now in sorted order. Note that the two-dimensional array of buckets is 10 times the size of the integer array being sorted. This sorting technique provides better performance than an insertion sort, but requires much more memory. The insertion sort requires space for only one additional element of data. This is an example of the space-time trade-off: The bucket sort uses more memory than the insertion sort, but performs better. This version of the bucket sort requires copying all the data back to the original array on each pass. Another possibility is to create a second two-dimensional bucket array and repeatedly swap the data between the two bucket arrays.

(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;

(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.

(Airline Reservations System) A small airline has just purchased a computer for its new automated reservations system. You've been asked to program the new system. You are to write a program to assign seats on each flight of the airline's only plane (capacity: 10 seats). Your program should display the following menu of alternatives-Please type 1 for "First Class" and Please type 2 for "Economy". If the person types 1 , your program should assign a seat in the first class section (seats \(1-5\) ). If the person types 2 , your program should assign a seat in the economy section (seats \(6-10\) ). Your program should print a boarding pass indicating the person's seat number and whether it's in the first class or economy section of the plane. Use a one-dimensional array to represent the seating chart of the plane. Initialize all the elements of the array to false to indicate that all seats are empty. As each seat is assigned, set the corresponding elements of the array to true to indicate that the seat is no longer available. Your program should, of course, never assign a seat that has already been assigned. When the first class section is full, your program should ask the person if it's acceptable to be placed in the economy section (and vice versa). If yes, then make the appropriate seat assignment. If no, then print the message "Next flight leaves in 3 hours."

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.

Sign-up for free