Question

C++ Code! Represent the following matrix using a two-dimensional array and write a nested for loop...

C++ Code! Represent the following matrix using a two-dimensional array and write a nested for loop to initialize the elements (do not initialize the array in the declaration):

10  9   8
7   6   5
4 3   2

Homework Answers

Answer #1

Here is code:

#include <iostream>

using namespace std;

int main()
{
int ROWS = 3,COLS = 3;
int arr[ROWS][COLS];
int counter = 10; // counter assign the values
// loops rows
for(int i = 0 ; i < ROWS ; i++)
{
// loops cols
for(int j = 0 ; j < COLS ; j++ )
{
arr[i][j] = counter--;
}
}
//prints the matrix
cout << "Matrix array is :" << endl;
for(int i = 0 ; i < ROWS ; i++)
{
for(int j = 0 ; j < COLS ; j++ )
{
cout << arr[i][j] << " ";
}
cout << endl;
}

return 0;
}

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
write a Nested loop in C# to print out the following output using windows form. 0...
write a Nested loop in C# to print out the following output using windows form. 0 0 1 0 2 4 0 3 6 9 0 4 8 16 32
Write a method that checks whether all elements in a two-dimensional array are identical. This is...
Write a method that checks whether all elements in a two-dimensional array are identical. This is what I have so far. int[][] arr1 = {{2, 2, 8}, {1, 6, 4}, {3, 9, 8}, {5, 6, 1}};        int[][] arr2 = {{7, 7, 7}, {7, 7, 7}, {7, 7, 7}, {7, 7, 7}};               if(matchingVal(array1)) {            System.out.println("Values in arr1 are the same.");        }               if(matchingVal(array2)) {           ...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns the index of the first occurance of the smallest value in the array. Your function must be able to process all the elements in the array. Create a function prototype and function definition (after the main function). Your main function should declare a 100 element integer array. Prompt the user for the number of integers to enter and then prompt the user for each...
Python Code Use (for loops) inside another for loop (a nested loop) to do the following:...
Python Code Use (for loops) inside another for loop (a nested loop) to do the following: Print Hello 30 times followed by Bye 10 times. Do that whole thing 15 times. (for a total of 600 lines of output) #Hint: First write the code that prints 30 "Hello" and then 10 "Bye". Think of that as two separate tasks, each of which requires its own for loop. THEN, once you get that working, put all of that inside ANOTHER loop...
Write a nested FOR loop (using two for loops & two fprintf statements to generate the...
Write a nested FOR loop (using two for loops & two fprintf statements to generate the following pattern: ***** **** *** ** *
java Write a single Java statements to accomplish each of the following: a) Displaythevalueoftheseventhelementofcharacterarraych. b) Considering...
java Write a single Java statements to accomplish each of the following: a) Displaythevalueoftheseventhelementofcharacterarraych. b) Considering “Scanner in = new Scanner (System.in);”, input a value into element 5 of one-dimensional double array nums. c) Initialize each of the four elements of the one-dimensional integer array test to 7. d) Declare and create an array called table as a float array that has four rows and three columns. e) Considering the following ArrayList declaration, insert “test” at the fourth position (index...
Write a code in c++ using linear insertion following the steps below. Comment your work. 1....
Write a code in c++ using linear insertion following the steps below. Comment your work. 1.    Ask the user for the name of a file containing data. If it does not exist, the program should display an error, then ask for a new file name. Entering an asterisk (*) as the first and only character on a line should terminate the program. 2.     You can use a statically-allocated one-dimensional array of doubles for this with length 100. You...
Using for loop and if statement, write a MATLAB code to plot a graph for x(t)...
Using for loop and if statement, write a MATLAB code to plot a graph for x(t) as a function of time t in the range 0 to 12 in increment of 0.01 ?(?) = 1: 0 ≤ ? ≤ 1 2? − 1 1 ≤ ? ≤ 2 3 2 ≤ ? ≤ 3 −2.5? + 10.5 3 ≤ ? ≤ 5 −2 5 ≤ ? ≤ 6 4/3 ? − 10 6 ≤ ? ≤ 9 2 9 ≤...
(SubtractionTable.java) Write a program that displays the following subtraction table. Notes: You must use a nested...
(SubtractionTable.java) Write a program that displays the following subtraction table. Notes: You must use a nested loop. There are spaces at the beginning of each row.                                                                                                                 2 - 1 = 1                                                                                                   3 - 1 = 2   3 - 2 = 1                                                                                   4 - 1 = 3   4 - 2 = 2   4 - 3 = 1                                                                   5 - 1 = 4   5 - 2 = 3   5 - 3 = 2   5 - 4 = 1  ...
Write a C++ program to perform the following tasks     a) Declare an integer array of...
Write a C++ program to perform the following tasks     a) Declare an integer array of size 1000.     b) Initialize the array with random values between 1 and 9.     c) Write the code to find and print, how many 1’s occur in the array.