In: Computer Science
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;
#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