Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

(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

(Polling) The Internet and the web are enabling more people to network, join a cause, voice opinions, and so on. The presidential candidates in 2008 used the Internet intensively to get out their messages and raise money for their campaigns. In this exercise, you'll write a simple polling program that allows users to rate five social-consciousness issues from 1 (least important) to 10 (most important). Pick five causes that are important to you (e.g., political issues, global environmental issues). Use a one-dimensional array topics (of type string) to store the five causes. To summarize the survey responses, use a 5 -row, 10 -column two-dimensional array responses (of type int), each row corresponding to an element in the topics array. When the program runs, it should ask the user to rate each issue. Have your friends and family respond to the survey. Then have the program display a summary of the results, including: a) \(A\) tabular report with the five topics down the left side and the 10 ratings across the top, listing in each column the number of ratings received for each topic. b) To the right of each row, show the average of the ratings for that issue. c) Which issue received the highest point total? Display both the issue and the point total. d) Which issue received the lowest point total? Display both the issue and the point total.

(Palindromes) A palindrome is a string that is spelled the same way forward and backward. Examples of palindromes include "radar" and "able was i ere i saw elba." Write a recursive function testPalindrome that returns true if a string is a palindrome, and false otherwise. Note that like an array, the square brackets ( [] ) operator can be used to iterate through the characters in a string.

(Single Array Questions) Write single statements that perform the following one-dimensional array operations: a) Initialize the 10 elements of integer array counts to zero. b) Add 1 to each of the 15 elements of integer array bonus. c) Read 12 values for double array month 7 yTemperatures from the keyboard. d) Print the 5 values of integer array bestscores in column format.

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

(Print a String Backuard) Write a recursive function stringReverse that takes a string and a starting subscript as arguments, prints the string backward and returns nothing. The function should stop processing and return when the end of the string is encountered. Note that like an array the square brackets ([]) operator can be used to iterate through the characters in a string.

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