Question

In: Computer Science

The following matrix is stored in an array with variable name matrixTest. (? −? ? ?...

The following matrix is stored in an array with variable name matrixTest.

(? −?

? ?

? ? ) i. Write the code to initialize the array.

ii. Write the code to display the content of matrixTest.

*IN C++

Solutions

Expert Solution

The following matrix is stored in an array with variable name matrixTest.

(? −?

? ?

? ? )

i. Write the code to initialize the array.

Answer

Since given is a matrix we can store matrix using two dimensional arrays.Multidimensional arrays are   array of arrays. Data in multidimensional arrays are stored in tabular form.

Initializing Two – Dimensional Arrays: There are two ways in which a Two-Dimensional array can be initialized.

First Method:

#include <iostream>
using namespace std;

int main()
{
    // initializing the array with given numbers which are stored in a matrix with 3 rows and 2 columns
  
      int x[3][2] = {2,-5,4,0,9,1};
    return 0;
}

Second Method or Better Method:

#include <iostream>
using namespace std;

int main()
{
    // initializing the array with given numbers which are stored in a matrix with 3 rows and 2 columns
    int matrixTest[3][2]={{2,-5}, {4,0}, {9,1}}; 

    return 0;
}

ii. Write the code to display the content of matrixTest.

Answer

#include <iostream>
using namespace std;

int main()
{
    // initializing the array with given numbers which are stored in a matrix with 3 rows and 2 columns
    int matrixTest[3][2]={{2,-5}, {4,0}, {9,1}}; 
    // Displaying content of matrixTest
    // accessing the content using the row indexes and column indexes.
    // first for loop is for accessing rows 
    for (int i = 0; i < 3; i++) 
    { 
        // second for is for accessing columns
        for (int j = 0; j < 2; j++) 
        { 
            cout << "Element at x[" << i 
                 << "][" << j << "]: "; 
            cout << matrixTest[i][j]<<endl; 
        } 
    } 
    return 0;
}

Output:


Related Solutions

use cout to print the address at which the following array is stored in memory long...
use cout to print the address at which the following array is stored in memory long double computers[24] a. print the adress of the last element b. print the address of the tenth element. c. print the address of the first element in the array
MATLAB Create a 2 × 3 matrix variable mat. Pass this matrix variable to each of...
MATLAB Create a 2 × 3 matrix variable mat. Pass this matrix variable to each of the following functions and make sure you understand the result: flip, fliplr, flipud, and rot90. In how many different ways can you reshape it? Note: (in addition to the functions listed, rotate the matrix 180 degrees clockwise)
declare a struct named matrix and typedef the struct to type name Matrix. A mathematical matrix...
declare a struct named matrix and typedef the struct to type name Matrix. A mathematical matrix is a two-dimensional, rectangular data structure. The matrix struct should have three fields (in this order): an unsigned variable named "rows", an unsigned variable named "columns", and a pointer to a pointer to double (i.e. double**) named "data". (Code #including matrix.h should be able to declare Matrix variables.) In matrix.c, implement the create_matrix function. The Matrix should be filled with 0.0 values. data[i] should...
On the following code that contains a method which returns a new matrix (2-d array) with...
On the following code that contains a method which returns a new matrix (2-d array) with same number of rows and columns as has its parameter (matrix). Each entry in the returned array should equal the result of multiplying the entry at the same row and column in matrix by the value of scalar. code: package edu.buffalo.cse116; /** * Class which contains a method which takes in a 2-dimensional array and a scalar value and returns their product. */ public...
A manufacturing company produces products. The following product information is stored: product name, product ID and...
A manufacturing company produces products. The following product information is stored: product name, product ID and quantity on hand. These products are made up of many components. Each component can be supplied by one or more suppliers. The following component information is kept: component ID, name, description, suppliers who supply them, and products in which they are used. Show table names, primary keys, attributes for each table, relationships between the entities including all constrains required. Assumptions in your database design:...
Activity 12: Array What is an Array? An array is a special variable, which can hold...
Activity 12: Array What is an Array? An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: var car1 = "Saab"; var car2 = "Volvo"; var car3 = "BMW"; However, what if you want to loop through the cars and find a specific one? And what if you had not...
Assume that you have an array of 100 elements representing the grades students are stored in...
Assume that you have an array of 100 elements representing the grades students are stored in memory. Suppose the grades are in IEEE single precision format. Write a MIPS program to compute the average of the students grades and store the result in register $f0. Assume the array base address is stored in register $s0 and a floating point value of 100 is stored in memory with it address given in register $s2.
Consider sorting n numbers stored in array A by first finding the smallest element of A...
Consider sorting n numbers stored in array A by first finding the smallest element of A and exchanging it with the element in A[1]. Then find the second smallest element of A and exchange it with A[2]. Continue in this manner for the first n-1 elements of A. a) (10 points) This algorithm is known as the selection sort. Write the pseudo code for this algorithm. b) (10 points) What loop invariant does this algorithm maintain? Note: You do not...
Write a method that, given an array of grades stored as double values, computes and return...
Write a method that, given an array of grades stored as double values, computes and return their mean.    Write another method that will return an array of the mean grade obtained by an arbitrary number of student.    This method will take a 2-dimensional array of double values in which each row corresponds to the grade vector    for a different student. You can compute the mean for each row as you did before...    Once you are done,...
Write a MARIE assembly language program that will read an “array” of positive decimal values stored...
Write a MARIE assembly language program that will read an “array” of positive decimal values stored in memory and output the smallest value. Use variable addr to store the location of the first data item. Use variable length to store the number of items in your array. Your code should be organized such that adding an additional array item would only involve adding the data line (ie. 021 dec 400) and updating the length variable (ie. length, dec 5). You...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT