In: Computer Science
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 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;
}
//Working Code
//Remember while printing the left bottom triangle you need to exclude diagonal elements thats the reason we start from i=1 while printing
#include <fstream>
#include<iostream>
using namespace std;
int main() {
//initialie the array size
int arr[5][5];
ifstream is("a.txt");
int cnt= 0;
int x;
//Construct 5*5 matrix
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
is>>x;
arr[i][j]=x;}}
// Print Left bottom triangle without including diagonal
for(int i=1;i<5;i++){
for(int j=0;j<i;j++){
cout<<arr[i][j];}
cout<<endl;
}
is.close();
}
//attached pic of working code and its output as expected