Question

so I want to ask the user for a matrix size int x = rows int...

so I want to ask the user for a matrix size

int x = rows

int y = columns

for int x = 3 and int y = 3

I want to print

1 2 3

4 5 6

7 8 9

and for each dimension I would print number the start from 1 to the end

so for int x = 4 and int y = 2

I would print

1 2

3 4

and such.

Homework Answers

Answer #1

// C++ program to input matrix size and print the elements in the matrix starting from 1

#include <iostream>

using namespace std;

int main()

{

       int rows, cols;

       // input of rows and columns of the matrix

       cout<<"Enter number of rows and columns of the matrix (separated by a space) : ";

       cin>>rows>>cols;

       int start = 1; // initialize the start element

       // loop over the number of rows

       for(int i=0;i<rows;i++)

       {

             // loop over the number of columns for each row

             for(int j=0;j<cols;j++)

             {

                    // print the start

                    cout<<start<<" ";

                    start++; // increment the start

             }

             cout<<endl; // add a new line after the columns of ith row are printed

       }

       return 0;

}

//end of program

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
so I want to ask the user for a matrix size coding language is java. int...
so I want to ask the user for a matrix size coding language is java. int x = rows int y = columns for int x = 3 and int y = 3 I want to print 1 2 3 4 5 6 7 8 9 and for each dimension I would print number the start from 1 to the end so for int x = 4 and int y = 2 I would print 1 2 3 4 and...
Question: Write a C++ program to ask the user to give you the number of rows...
Question: Write a C++ program to ask the user to give you the number of rows and number of columns, then make the multiplication table by using nested loop. For example, if the user enters 3 for rows and 4 for columns then the multiplication table would be like the following: 1    2   3     4 1   1 2 3 4 2   2    4      6    8 3    3 6 9 12
A.6 ... static int x = 1; int y = x * 2; void t1() {...
A.6 ... static int x = 1; int y = x * 2; void t1() {                 y++;                 cout << "x: " << x << " | y: " << y << endl;                 y += 1;                 x -= -1; } void t2() {                 int* x = &y;                 cout << "x: " << x << " | y: " << y << endl; } void t3() {                 int y = x;                 static int x...
PYTHON Ask the user for a value N (0 ≤ N < 10) Create a 2-D...
PYTHON Ask the user for a value N (0 ≤ N < 10) Create a 2-D list in an N X N structure with integers 1-(N*N) Print the list created Reverse the list such that (1) each list in the 2D list is reversed and (2) the order of each list in the outer list is reversed (see example) Print the reversed list Example Execution What size 2D list would you like to create? N> 3 The original list is:...
Flowchart + Python. Ask the user for a value. Then, ask the user for the number...
Flowchart + Python. Ask the user for a value. Then, ask the user for the number of expressions of that value. Use While Loops to make a program. So then, it should be so that 5 expressions for the value 9 would be: 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 Flowcharts and Python Code. Not just Python code.
Questions on C++, I am not 100% sure on answers, so need to clarify. Simple explanation...
Questions on C++, I am not 100% sure on answers, so need to clarify. Simple explanation next to the question would be nice as well. In mathematic notation, which is equivalent to the following statement when writing a pseudocode? x = 4y A. x == 4y B. x := 4y C. x -> 4y D. 4y = x Given the following pseudocode, the final value of x is __. Start int x = 5; for i in 1 to 3...
."Ask the user to input a number.  You must use an input dialog box for this input....
."Ask the user to input a number.  You must use an input dialog box for this input. Be sure to convert the String from the dialog box into an integer (int). The program needs to keep track of the smallest number the user entered as well as the largest number entered. Use a Confirm dialog box to ask the user if they want to enter another number. If yes, repeat the process. If no, output the smallest and largest number that...
1. _____________ means operating an array, such as object or list, or doing operations element by...
1. _____________ means operating an array, such as object or list, or doing operations element by element.        Vectorization        Standardrization 2. What would be the output of the range function: start= 10 stop = 20 step = 2 range (start, stop, step) 3. The below code x=2 for x in range(x+1,x+8): print(x, end=" ") will print 4. Which of the below numpy attributes describe the number of rows and number of the columns in the array   ...
Give the output of the program using Call-By-Reference: int i, a[3]; void f (int x, int...
Give the output of the program using Call-By-Reference: int i, a[3]; void f (int x, int y){ x = (x*y) mod 3; y = y – x; } main(){ i = 0; a[0] = 1; a[1] = 2; a[2] = 0; f(i, a[i]); print(“%d %d %d %d\n”, i, a[0], a[1], a[2]); f(a[i], a[i]); print(“%d %d %d\n”, a[0], a[1], a[2]); }
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in airplane const int COLS = 4; void menu(); //displays options void displaySeats(char[][COLS]); void reserveSeat(char [ROWS][COLS]); int main() { int number=0; //holder variable char seatChar[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { seatChar[i][j] = '-'; } } int choice; //input from menu bool repeat = true; //needed for switch loop while (repeat...