In C++ PLEASE USING FILES AND PROVIDE AN ALGORITHM ON HOW YOU SOLVED IT Lab: Usi
ID: 3906412 • Letter: I
Question
In C++ PLEASE USING FILES AND PROVIDE AN ALGORITHM ON HOW YOU SOLVED IT
Lab: Using 2 dimensional arrays
Create a two-dimensional ARRAY that contains at least 5 rows and 5 columns
(NOTE: The array may NOT be square - that is, NOT int array1 [5][5] , but int array2 [7][7] ,
but int array3[5][7] is FINE.)
Design and Implement a program that will read all values from a file for your array.
You are to:
(1) INPUT the values in your file by ROW and then
(2) OUTPUT the values by COLUMN.
PLEASE NOTE THE OUTPUT SHOULD LOOK LIKE THE DIMENSIONS OF THE AREA.
EXAMPLE:
If the data is: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
THE OUTPUT WOULD LOOK LIKE if I HAD INPUT BY ROW and OUTPUT BY ROW!!!:
1 2 3 4 5
6 7 8 9 10
11 12 3 14 15
16 17 18 19 20
21 22 23 24 25
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int arr[5][5];
ifstream in("input.txt");
if(in.is_open()) {
for(int i = 0; i < 5; ++i) {
for(int j = 0; j < 5; ++j) {
in >> arr[i][j];
}
}
for(int i = 0; i < 5; ++i) {
for(int j = 0; j < 5; ++j) {
cout << arr[j][i] << " ";
}
cout << endl;
}
in.close();
}
return 0;
}