Matrix 5x5

Hi everyone,

I just started studying programming at University level and this is the first course.

I just encountered the task below and I'm getting a little bit confused to say the least.

The task is the following "
The C++ program below reads from the file matrix.txt integers table inserting it in the 5x5 table (matrix [5][5]). The program prints the matrix on screen, calculates the sum of the elements and prints the sum on screen making use of functions print_matrix () and count_sum(). Your task is to make up the functions in question. The values situated on the rows are separated by space."

Does this mean I need to have the file "matrix.txt" or where am I supposed to get the values to the matrix from?

The result should look like following:

Matrix:
11 2 4 5 6
9 8 7 6 5
1 2 3 4 5
11 22 33 44 55
1 2 4 5 0
Sum of elements: 255

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
26
27
28
29

#include <iostream>
#include <fstream>
using namespace std;

void print_matrix(int matrix[5][5]);
int calculate_sum(int matrix[5][5]);

int main(void)
{
  int matrix[5][5];
  int sum;
  ifstream file("matrix.txt");
  if (!file){
        cout << "File cannot be opened!";
  }
  else {
    for (int y=0; y<5;y++){
          for (int x=0;x<5;x++){
            file >> matrix[y][x];
          }
    }
    file.close();
    cout << "Matrix:" << endl;
    print_matrix(matrix);
    sum = count_sum(matrix);
    cout << "Sum of elements: " << sum << endl;
  }
} 
Does this mean I need to have the file "matrix.txt"

Yes. You are given the values. Simply save them to a file named matrix.txt.
you should have been provided with the input file.
if you were not, you can make one in a simple text editor like notepad in windows, just put some values in it and keep going.
Thanks guys.

Well actually I am only supposed to provide the functions and upload them to my schools system so basically I shouldn't even need to have the .txt file .

The code I wrote in my original message was handed to me and now my task is to create functions void rint_matrix(int matrix[5][5]); and int calculate_sum(int matrix[5][5]);

The whole task seems a bit weird to me tbh :D
I shouldn't even need to have the .txt file .

You will need the file to test your program.

The assignment is not really weird. The purpose is to make sure you understand how to navigate a 2D array.
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <fstream>

using namespace std;

void print_matrix(int matrix[5][5]);
int calculate_sum(int matrix[5][5]);

int main(void)
{
    int matrix[5][5];
    int sum;
    ifstream file("matrix.txt");
    if (!file)
    {
        cout << "*** File cannot be opened!\n";
        exit(1);
    }
    else
    {
        for (int y=0; y<5;y++)
        {
            for (int x=0;x<5;x++)
            {
                file >> matrix[y][x];
                cout << y << ',' << x << '=' << matrix[y][x] << '\n';
            }
        }
        file.close();
        cout << "Matrix:" << endl;
        print_matrix(matrix);
        sum = calculate_sum(matrix);
        cout << "Sum of elements: " << sum << endl;
    }
}

void print_matrix(int matrix[5][5])
{
    cout << "1 Fill this part out with a program\n";
}

int calculate_sum(int matrix[5][5])
{
    int sum{0};
    
    cout << "2 Fill this part out with a program\n";
    
    return sum;
}


matrix.txt
11 2 4 5 6
9 8 7 6 5
1 2 3 4 5
11 22 33 44 55
1 2 4 5 0
Last edited on
For both of these functions, you need a nested loop. Have a look at how this is done for reading from the file (L18-22 OP) and modify what's done in the inner-loop as needed.
Topic archived. No new replies allowed.