/*! 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 4 (Double Array Questions) Answer ... [FREE SOLUTION] | 91Ó°ÊÓ

91Ó°ÊÓ

(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 91Ó°ÊÓ!

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

(True or False) State whether the following are true or false. If the answer is \(f a l s e\), explain why. a) An array can store many different types of values. b) An array subscript should normally be of data type float. c) If there are fewer initializers in an initializer list than the number of elements in the array, the remaining elements are initialized to the last value in the initializer list. d) It's an error if an initializer list has more initializers than there are elements in the array. e) An individual array element that is passed to a function and modified in that function will contain the modified value when the called function completes execution.

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

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

(Bubble Sort Enbancements) The bubble sort described in Exercise 7.11 is inefficient for large arrays. Make the following simple modifications to improve the performance of the bubble sort: a) After the first pass, the largest number is guaranteed to be in the highest-numbered element of the array; after the second pass, the two highest numbers are "in place," and so on. Instead of making nine comparisons on every pass, modify the bubble sort to make eight comparisons on the second pass, seven on the third pass, and so on. b) The data in the array may already be in the proper order or near-proper order, so why make nine passes if fewer will suffice? Modify the sort to check at the end of each pass if any swaps have been made. If none have been made, then the data must already be in the proper order, so the program should terminate. If swaps have been made, then at least one more pass is needed.

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

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.