In: Computer Science
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main() {
// declaring variables
int arr[5][5];
int trans_arr[5][5];
int data[25];
int num;
int k = 0;
int even = 0;
int sum_even_col = 0;
int sum_all_even_col = 0;
int less_twenty = 0;
fstream file;
// opening file
file.open("data.txt");
// reading data into array
while (file >> num) {
data[k] = num;
k++;
}
// storing data into 5x5 array
k = 0;
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 5; j++) {
arr[i][j] = data[k];
k++;
}
}
// counting number of even numbers
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 5; j++) {
if(arr[i][j] % 2 == 0) {
even += 1;
}
}
}
cout << "Number of even numbers in the array: " << even << "\n" << endl;
// calculating sum of all even column
for(int i = 0; i < 5; i += 2) {
for(int j = 0; j < 5; j++) {
sum_even_col += arr[i][j];
}
sum_all_even_col += sum_even_col;
cout << "Sum of the column " << i << " is: " << sum_even_col << endl;
}
cout << "Sum of all even columns is: " << sum_all_even_col << "\n" << endl;
// count numbers less than 20
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 5; j++) {
if(arr[i][j] < 20) {
less_twenty += 1;
}
}
}
cout << "Number of numbers in the array which are less than 20 are: " << less_twenty << "\n" << endl;
// display matrix
cout << setw(15) << "Matrix:" << endl;
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 5; j++) {
if(j == 4) {
cout << arr[i][j];
break;
}
cout << arr[i][j] << setw(5);
}
cout << endl;
}
// transpose the matrix
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 5; j++) {
trans_arr[i][j] = arr[j][i];
}
}
// display transpose
cout << setw(20) << "\nTranspose Matrix:" << endl;
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 5; j++) {
if(j == 4) {
cout << trans_arr[i][j];
break;
}
cout << trans_arr[i][j] << setw(5);
}
cout << endl;
}
}


FOR HELP PLEASE COMMENT.
THANK YOU.