How to fill an array with some specific elements of a matrix?

Hi all, I'm a new member of the forum. : )
The Basics of Computer Science course that I'm attending is coming to an end and I'm exercising as much as possibile at home. I read the rules and I know that is not possibile to ask help for homeworks, but it's not really a homework, it's more a personal challenge, I chose this exercise, not the professor. : )
If it's still forbidden, I apologize, I didn't mean to break the rules.

Anyway, the goal of the program is to create a square matrix, calculate the arithmetic mean of the elements of the diagonal and then to create an array made of the elements that are bigger of the arithmetic mean.

I managed to write a good 90% of the total code, but I'm missing the array part...

Here is the code as it is now (please, ignore the comments in italian xD):

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <cstdlib>

using namespace std;

// Alias e dichiarazioni globali

const int MAX_RIG=20;

typedef int matrix[MAX_RIG][MAX_RIG];
typedef int array[MAX_RIG];

int rig; // Riempimento della matrice (quadrata ==> righe = colonne)
float media = 0;

// Prototipi di funzione

void leggi_mat(matrix &);
void stampa_mat(matrix);
void stampa_vett(array);
float calc_media(matrix, float &);
void vett_media(matrix, array &);

// Funzione principale

int main(){
    
    matrix mat;
    array vett;
    
    leggi_mat(mat);
    stampa_mat(mat);
    calc_media(mat, media);
    
    cout << "\n\nLa media dei valori della diagonale e': " << media;
    
    cout << "\n \n";
    
    vett_media(mat, vett);
    
    cout << "\n \n";

    cout << "\n \n";
    system("PAUSE"); // (please ignore this, is required by my professor. A.)
    return 0;

}

// Funzioni secondarie:

// Funzione per la lettura della matrice (matrix input, A.)

void leggi_mat(matrix &matrice){

      int i, j; // Contatori per il doppio ciclo for
      
      cout << "Creazione della matrice quadrata. \n";
      cout << "Inserisci il numero di righe e colonne (max. 20 elementi): ";
      cin >> rig;
      cout << "\n";
      for (i = 0; i < rig; i++)
      	for (j = 0; j < rig; j++){
      	    cout << "Inserisci l'elemento [" << i << "][" << j << "]: ";
      	    cin >> matrice[i][j];
      	}
}

// Funzione per la stampa della matrice (print matrix, A.)

void stampa_mat(matrix matrice){
     
     int i, j;
     
     cout << "\nMatrice (" << rig << "x" << rig << ")\n";
     for (i = 0; i < rig; i++) {
       for (j = 0; j < rig; j++)
      cout << matrice[i][j] << " ";
      cout << "\n";
      }
}

// Funzione per il calcolo della media (calculate the average, A.)

float calc_media(matrix matrice, float &tmp_media){
      
      int i, j, somma = 0;
      tmp_media = 0;
      matrice[rig][rig];
      
      for (i = 0; i < rig; i++)
        for (j = 0; j < rig; j++)
            if (i == j)
            somma = somma + matrice[i][j];
            
      tmp_media = float(somma)/rig;
      
      return tmp_media;
}

// Funzione per la creazione del vettore (array creation, A.)

void vett_media(matrix matrice, array &vettore){
    
    int i, j, n = 0; // n ==> Riempimento del vettore
    matrice[rig][rig];
    vettore[rig];
    
    for (i = 0; i < rig; i++)
      for (j = 0; j < rig; j++)
          if (matrice[i][j] > media){
          vettore[n] = matrice[i][j];
          n++;
          }
    
     for (n = 0; n < rig; n++)
         cout << vettore[n] << " ";
}

/* Funzione per la stampa del vettore (print array, A.)

void stampa_vett(array vettore){
     
     int i;
     for (i = 0; i < rig; i++)
     cout << vettore[i] << " ";
} */


As you can see I commented the whole function to print the array, because I tested it and the output isn't really what I expected. xd I'm pretty sure that something is wrong in the for cycle limit / varray maximum value but I can't manage to solve it. Can you give me any help, please? I'd really appreciate it, Thanks! : D

(I'm sorry for any language error BTW, I tried to be as much correct as possible! xD)
I'm afraid italian repels users from helping you. I't obscure what the function does from its name.

I think using local languages in your projects is not good because you hardly would be able to share it with community.
By the way, what do the lines do?
1
2
    matrice[rig][rig];
    vettore[rig];
The size for your array typedef is too small, it should be at least MAX_RIG*MAX_RIG-MAX_RIG
Anyway, the error is not in the printing function ( as long as rig is the array size )
I'm sorry for the language, I translated the source in English, hope it's more readable now. xd

Here's the code:
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <cstdlib>

using namespace std;

// Alias and global declarations

const int MAX_ROW=20;

typedef int matrix[MAX_ROW][MAX_ROW];
typedef int array[MAX_ROW * MAX_ROW - MAX_ROW];

int row; // Matrix input limit (square matrix ==> row = columns)
float average = 0;

// Functions' Prototypes

void read_mat(matrix &);
void print_mat(matrix);
void print_array(array);
float calc_average(matrix, float &);
void array_average(matrix, array &);

// Main Function

int main(){
    
    matrix mat;
    array arr;
    
    read_mat(mat);
    print_mat(mat);
    calc_average(mat, average);
    
    cout << "\n\nThe average mean of elements on the diagonal is: " << average;
    
    cout << "\n \n";
    
    array_average(mat, arr);
    
    cout << "\n \n";

    cout << "\n \n";
    system("PAUSE");
    return 0;

}

// Other functions:

// Matrix Input Function

void read_mat(matrix &matrix0){

      int i, j; // Variables needed by the for cycle
      
      cout << "Square matrix creation. \n";
      cout << "Insert the number of rows and columns (20 elements max.): ";
      cin >> row;
      cout << "\n";
      for (i = 0; i < row; i++)
      	for (j = 0; j < row; j++){
      	    cout << "Insert the element [" << i << "][" << j << "]: ";
      	    cin >> matrix0[i][j];
      	}
}

// Matrix Output Function

void print_mat(matrix matrix0){
     
     int i, j;
     
     cout << "\nMatrix (" << row << "x" << row << ")\n";
     for (i = 0; i < row; i++) {
       for (j = 0; j < row; j++)
      cout << matrix0[i][j] << " ";
      cout << "\n";
      }
}

// Avereage mean calculation Function

float calc_average(matrix matrix0, float &tmp_average){
      
      int i, j, sum = 0;
      tmp_average = 0;
      matrix0[row][row];
      
      for (i = 0; i < row; i++)
        for (j = 0; j < row; j++)
            if (i == j)
            sum = sum + matrix0[i][j];
            
      tmp_average = float(sum)/row;
      
      return tmp_average;
}

// Array creation Function

void array_average(matrix matrix0, array &array0){
    
    int i, j, n = 0; // n ==> Array limit required by the for cycle
    matrix0[row][row];
    array0[row*row - row];
    
    for (i = 0; i < row; i++)
      for (j = 0; j < row; j++)
          if (matrix0[i][j] > average){
          array0[n] = matrix0[i][j];
          n++;
          }
    
     for (n = 0; n < row; n++)
         cout << array0[n] << " ";
}


I changed the array maximum size as suggested by Bazzy. Also, I eliminated the array print function because I noticed I included it into the array creation function. xD

And here, is the problem. As you can see testing the program, the output is always good, except for the array. Example:

Square matrix creation.
Insert the number of rows and columns (20 elements max.): 3

Insert the element [0][0]: 1
Insert the element [0][1]: 2
Insert the element [0][2]: 3
Insert the element [1][0]: 4
Insert the element [1][1]: 5
Insert the element [1][2]: 6
Insert the element [2][0]: 7
Insert the element [2][1]: 8
Insert the element [2][2]: 9

Matrix (3x3)
1 2 3
4 5 6
7 8 9


The average mean of elements on the diagonal is: 5

6 7 8


As you can see the average is correctly calculated (1 + 5 + 9 = 15 / 3 = 5) but the array is missing one element (the 9). Also, I suspect that with different input the error may be even worse... Where I'm making a mistake? Thank you all for your help, I really appreciate it. : )

P.S.:

@melkiy: That two lines declare two internal variables for the function based on the row value inserted by the user.

@Bazzy: "rig" (now "row") was the old array size, now replaced by (row * row - row). I tried the function with that, it gives me the 9 (as expected) but also two random values. I imagine the array is too big (and that only with "row" is too small). I'm a little bit confused, maybe it's also the hour! xDD
Last edited on
"rig" (now "row") was the old array size, now replaced by (row * row - row). I tried the function with that, it gives me the 9 (as expected) but also two random values. I imagine the array is too big (and that only with "row" is too small). I'm a little bit confused, maybe it's also the hour! xDD
row*row-row is the maximum number of values the array can have for that matrix.
You need to keep track of the amount of numbers which are actually less than the average and use that in the loop to display only the values you need
Yes, it was so simple, how stupid was I!
Here's the full working code if someone's interested:

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <cstdlib>

using namespace std;

// Alias and global declarations

const int MAX_ROW=20;

typedef int matrix[MAX_ROW][MAX_ROW];

int row; // Matrix input limit (square matrix ==> row = columns)
float average = 0;

// Functions' Prototypes

void read_mat(matrix &);
void print_mat(matrix);
float calc_average(matrix, float &);
void array_average(matrix);

// Main Function

int main(){
    
    matrix mat;
    
    read_mat(mat);
    print_mat(mat);
    calc_average(mat, average);
    
    cout << "\n\nThe average mean of elements on the diagonal is: " << average;
    
    cout << "\n";
    
    array_average(mat);
    
    cout << "\n \n";
    system("PAUSE");
    return 0;

}

// Other functions:

// Matrix Input Function

void read_mat(matrix &matrix0){

      int i, j; // Variables needed by the for cycle
      
      cout << "Square matrix creation. \n";
      cout << "Insert the number of rows and columns (20 elements max.): ";
      cin >> row;
      cout << "\n";
      for (i = 0; i < row; i++)
      	for (j = 0; j < row; j++){
      	    cout << "Insert the element [" << i << "][" << j << "]: ";
      	    cin >> matrix0[i][j];
      	}
}

// Matrix Output Function

void print_mat(matrix matrix0){
     
     int i, j;
     
     cout << "\nMatrix (" << row << "x" << row << ")\n";
     for (i = 0; i < row; i++) {
       for (j = 0; j < row; j++)
      cout << matrix0[i][j] << " ";
      cout << "\n";
      }
}

// Avereage mean calculation Function

float calc_average(matrix matrix0, float &tmp_average){
      
      int i, j, sum = 0;
      tmp_average = 0;
      matrix0[row][row];
      
      for (i = 0; i < row; i++)
        for (j = 0; j < row; j++)
            if (i == j)
            sum = sum + matrix0[i][j];
            
      tmp_average = float(sum)/row;
      
      return tmp_average;
}

// Array creation Function

void array_average(matrix matrix0){
    
    int i, j, n = 0; // n ==> Array limit required by the for cycle
    int array[(row*row) - 1];
    
    for (i = 0; i < row; i++)
      for (j = 0; j < row; j++)
          if (matrix0[i][j] > average){
          array[n] = matrix0[i][j];
          n++;
          }
      
     if (n == 0)
          cout << "There aren't elements bigger then the average.";
     else {
     cout << "These elements are bigger then the average: ";
     for (i = 0; i < n; i++)
         cout << array[i] << " ";
     }
}


Thank you a lot guys, you really helped me out of this! xd
Topic archived. No new replies allowed.