Question

In: Computer Science

[PLEASE USE C++] Write a function to read values of a number of rows, number of...

[PLEASE USE C++] Write a function to read values of a number of rows, number of columns, 2 dimensional (2D) array elements and display the 2D array in a matrix form. Input 2 3 1 4 5 2 3 0 Where, First line of represents the number of rows. Second line of input represents the number of columns. Third line contains array elements of the 1st row and so on. Output 1 4 5 2 3 0 where There must be single space between 2 numbers in the row. There should not be any space after the last number in the row. e.g. in the 2nd row, there should not be any space after number 0. Assume that, Row and column values are integers within the range [1 to 100]. 2D Array elements are within the range [-2147483648 t o 2147483647].

Solutions

Expert Solution

The code snippet for the question provided is provided below:

#include<iostream>

using namespace std;

int main()
{
        int r, c, i, j;
        cin>>r;
        cin>>c;
    int matrix[r][c];
        for(i=0; i<r; i++)
        {
                for(j=0; j<c; j++)
                {
                        cin>>matrix[i][j];
                }
        }
        cout<<"\nThe Matrix representation of 2D array :\n";
        for(i=0; i<r; i++)
        {
                for(j=0; j<c; j++)
                {
                        cout<<matrix[i][j]<<" ";
                }
                cout<<"\n";
        }
        return 0;
}

In this program the integer variables declared are r, c, i ,j and 2D array matrix[][].

r variable denotes the rows in the matrix

c variable denotes the columns in the matrix.

i and j are used to loop through different indices in the matrix.

The first for loop block is to insert the values into the 2D array.

And the second for loop block was to print the 2D array in matrix form

The screenshot of the program after running and the ouput generated are attached for your reference:

The IDE used to code is codeBlocks.

The screenshots of ouput generated by providing the values given in the question are:

Hope this helps!!!


Related Solutions

Write a C++ function that reads a .csv file(file contains rows of string(name) and number) into...
Write a C++ function that reads a .csv file(file contains rows of string(name) and number) into a vector and loop through that vector and find the max number.
(C++ only please) Write a function called maximum that takes an array of double values as...
(C++ only please) Write a function called maximum that takes an array of double values as a parameter and returns the largest value in the array. The length of the array will also be passed as a parameter. (Note that the contents of the array should NOT be modified.) Write a function called printReverse that takes an array of characters and the length of the array as parameters. It should print the elements of the array in reverse order. The...
PLease use c++ Write a checkbook balancing program. The program will read in, from the console,...
PLease use c++ Write a checkbook balancing program. The program will read in, from the console, the following for all checks that were not cashed as of the last time you balanced your checkbook: the number of each check (int), the amount of the check (double), and whether or not it has been cashed (1 or 0, boolean in the array). Use an array with the class as the type. The class should be a class for a check. There...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
*Code in C* Write a function that checks if a number is a perfect cube. Write...
*Code in C* Write a function that checks if a number is a perfect cube. Write another function that calculates the integer cubic root. Under the main program: Prompt the user to input a number Tell the user if the number is a perfect cube or not Print the cubic root if the inputted number is a perfect cube.
In c++ Write a recursive driver function that will replace each of the odd values in...
In c++ Write a recursive driver function that will replace each of the odd values in a stack with the cube of the value.
Write a C++ program that will read in the number of nodes (less than 10) and...
Write a C++ program that will read in the number of nodes (less than 10) and a adjacency relation representing a graph. The program will create an adjacency matrix from the adjacency relation. The program will then print the following items: 1. Print the adjacency matrix 2. Determine if there are any isolated nodes and print them 3. Determine if an Euler path exists Sample run output Please input the number of nodes: 6 Please input the adjacency relation: {(1,2),(1,5),(2,1),(2,3),(3,2),(3,4),(4,3),(4,5),(5,1),(5,4)}...
Write a C++ program that will read in the number of nodes (less than 10) and...
Write a C++ program that will read in the number of nodes (less than 10) and a adjacency relation representing a graph. The program will create an adjacency matrix from the adjacency relation. The program will then print the following items: 1. Print the adjacency matrix 2. Determine if there are any isolated nodes and print them 3. Determine if an Euler path exists Sample run (to make program output more clear, I have put it in boldface): Please input...
Write a C++ program will read in the number of nodes and a binary relation representing...
Write a C++ program will read in the number of nodes and a binary relation representing a graph. The program will create an adjacency matrix from the binary relation. The program will then print the following : 1. The adjacency matrix 2. Determine if there are any isolated nodes and print them 3. Determine if an Euler path exists and said so. The sample run of the program is as follows. The output should just like this: It starts by...
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and...
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and other files, loops, conditionals, data types, assignment.   Calculating fuel economy. This program will use exceptions and stream errors to make a robust application that gets the number of miles and gallons each time the user fuels their car. It will put those values into vectors. Once the user wants to quit enter values, it will calculate the fuel economy. Create GetMiles() function that returns...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT