Question

In: Computer Science

In the main function, declare a two-dimensional matrix array 5 by 5, read data from a...

  1. In the main function, declare a two-dimensional matrix array 5 by 5, read data from a file named “data.txt”. Suppose the data in the file are integers between 0 and 9. Write your main function to print out the left bottom below the diagonal of the matrix and keep the triangle shape. Note the numbers on the diagonal are not included in the output. An example is given as follows. (25 points)

Suppose the original matrix is

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

The output should be like as follows.

1

1 2

1 2 3

1 2 3 4

#include <iostream>

#include <iomanip>

#include <fstream>

using namespace std;

const int NUM = 5;

int main()

{

      // I - Declaring a five by five array

      /* II - Read data from data.txt and use them to create

             the matrix in the previous step*/

/* III – print out the left bottom below the diagonal of the matrix and keep the triangle shape. Note the numbers on the diagonal are not included in the output.

       read.close();

       return 0;

Solutions

Expert Solution


#include <iostream>
#include<iomanip>
#include<fstream> 
#include<string>
using namespace std; 
const int NUM=5;

int main() 
{ 
    string a[NUM][NUM];
        // filestream variable file 
        fstream file; 
        string word, read; 

        // filename of the file 
        read = "data.txt"; 

        // opening file 
        file.open(read.c_str()); 

        // extracting words from the file 
        for(int i=0;i<5;i++)
        {
                for(int j=0;j<5;j++)
                {
                        while (file >> word) 
                { 
                    a[i][j]=word;
                    break;
                } 
                }
        }
        
        cout<<"array: "<<endl;
        for(int i=0;i<5;i++)
        {
                for(int j=0;j<5;j++)
                {
                        if(i==j)
                          break;
                        else
                          cout<<a[i][j]<<" ";
                }
                cout<<endl;
        }
        file.close();
        return 0; 
} 

1. First we have declared an 2D string array which will store the content of file data.txt (data.txt is a file stored in desktop which contain the above matrix). We have declared string type array because the content in data.txt was of string type only.

string a[NUM][NUM];

2. Then we have made a filestream variable file which will refer to data.txt. Then we have declared a string type variable word which will read each word from the file and there is one more string type variable read which will stores the name of the file which we want to read.

fstream file;
   string word, read;

3. Then we have assigned read variable the name of the file which we want it to read.

read = "data.txt";

4. Then we have opened the file using the file variable which will refer to our file now. Here the c_str() member function returns a const char * pointer to the string.

  file.open(read.c_str());

5. Then we have started 2 for loops because string array is a 2D array. Inside the second for loop we have used a while loop which will read each word of the file. After it reads 1 word we assign that word to the current index of string array. Then we have used the break statement because we want only one word to go to a particular string array's index.

for(int i=0;i<5;i++)
   {
       for(int j=0;j<5;j++)
       {
           while (file >> word)
   {
   a[i][j]=word;
   break;
   }
       }
   }

6. Now the matrix which was present in the file is copied to our string array.

7. Next we have printed the matrix. But we are printing only those values from matrix which which comes before the main diagonal of matrix. For main diagonal both the indexes i and j are same so we have iterated just before we reach the main diagonal. In this way we get all values which lie below the diagonal elements excluding the diagonal elements.

cout<<"array: "<<endl;
   for(int i=0;i<5;i++)
   {
       for(int j=0;j<5;j++)
       {
           if(i==j)
           break;
           else
           cout<<a[i][j]<<" ";
       }
       cout<<endl;
   }

8. In the end we have closed the file.

file.close();

OUTPUT


Related Solutions

in C++ In the main function, declare a two-dimensional matrix array 5 by 5, read data...
in C++ In the main function, declare a two-dimensional matrix array 5 by 5, read data from a file named “data.txt”. Suppose the data in the file are integers between 0 and 9. Write your main function to print out the left bottom below the diagonal of the matrix and keep the triangle shape. Note the numbers on the diagonal are not included in the output. An example is given as follows. (25 points) Suppose the original matrix is 1...
1.Declare a two-dimensional array of Strings namedchessboard.
1. Declare a two-dimensional array of Strings named chessboard.2. Declare a two-dimensional array of integers named tictactoe.3. Declare and create a two-dimensional array of chars,tictactoe, with 3 rows, each with 3 elements.4. Create a two-dimensional array of ints, plan, with 2 rows, and and 3 columns and initialize the first row to 8, 20, 50 and the second row to 12, 30, 75. Use member initializer syntax.
Write a C++ function that takes a two dimensional dynamic array (matrix) and its sizes and...
Write a C++ function that takes a two dimensional dynamic array (matrix) and its sizes and returns true if the matrix is an upper matrix and returns false otherwise. Remark: A matrix M is an upper matrix if it is a square matrix (row size = column size) and every element M[i][j] = 0 for all i > j. That is all the elements of the matrix below the main diagonal are zero.
Provide code samples for the following in C#a. Declare a two-dimensional array of integers names...
Provide code samples for the following in C#a. Declare a two-dimensional array of integers names intArray17.b. Create a loop to calculate the sum of every element in the first column.c. Create a loop to calculate the sum of every element in the array.
In C# - Provide code samples for the following: Declare a two-dimensional array of integers names...
In C# - Provide code samples for the following: Declare a two-dimensional array of integers names intArray17. Create a loop to calculate the sum of every element in the first column. Create a loop to calculate the sum of every element in the array.
This program needs to be in Java Exercise on Single Dimensional Arrays Declare an array reference...
This program needs to be in Java Exercise on Single Dimensional Arrays Declare an array reference variable arrayInt for an array of integers. Create the array of size 100, and assign it to arrayInt. (2 points) Write a method to populate the array with Random numbers between 1 to 25. Signature of the method is: populateArray( int[] ) which returns nothing. Call this method from main with arrayInt--> populateArray(arrayInt). (2 points) Write a method to print an array. Signature of...
in java Implement a function print2Darray(int[][] array) to print a formatted 4x4 two dimensional integer array....
in java Implement a function print2Darray(int[][] array) to print a formatted 4x4 two dimensional integer array. When the array contains {{10, 15, 30, 40},{15, 5, 8, 2}, {20, 2, 4, 2},{1, 4, 5, 0}}, Your output should look like: {10 15 30 40} {15 5 8 2}{ 20 2 4 2}{ 1450} Now, implement another function print2DList(ArrayList<ArrayList<Integer>> list) to print a formatted 2D list.
Write Matrix Addition 2 D (dimensional) Array program in c++.
Write Matrix Addition 2 D (dimensional) Array program in c++.
MATLAB: Write a function called max_number that takes an at most two-dimensional matrix A as its...
MATLAB: Write a function called max_number that takes an at most two-dimensional matrix A as its sole input. The function returns the largest element of A. You are not allowed to use the built-in max function.
Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0.
This program is for C.Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0. Combine the elements of A + B to create two- dimensional array C = A + B. Display array A, B and C to the screen for comparison. (Note a[0] + b[0] = c[0], a[1] + b[1] = c[1], etc.)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT