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)