Sottrarre agli elementi della diagonale primaria un valore "x" (matrice)
#include <iostream>
using namespace std;
// ----- Variabili globali ----- //
const int massimo=100;
// ----- /Variabili globali ----- //
// ----- Prototipi ----- //
int dimensione();
void caricamento_matrice(int [massimo][massimo], int, int);
void stampa_matrice(int [massimo][massimo], int, int);
void sottrazione(int [massimo][massimo], int, int);
// ----- /Prototipi ----- //
// ----- Funzioni ----- //
int dimensione()
{ int n;
cout<<"Inserisci il numero di righe e colonne: ";
cin>>n;
while (n>massimo)
{ cout<<"Errore. Inserisci il numero di righe: ";
cin>>n;
}
return n;
}
void caricamento_matrice(int m[massimo][massimo], int nr, int nc)
{ for (int i=0; i<nr; i++)
{ for (int j=0; j<nc; j++)
{ cout<<"Inserisci l'elemento della riga numero "<<i<<" e colonna numero "<<j<<": ";
cin>>m[i][j];
}
}
}
void stampa_matrice(int m[massimo][massimo], int nr, int nc)
{ for (int i=0; i<nr; i++)
{ for (int j=0; j<nc; j++)
{ cout<<m[i][j]<<"\t";
}
cout<<endl;
}
}
void sottrazione(int m[massimo][massimo], int nr, int nc)
{ int x;
cout<<"Inserisci l'elemento da sottrarre: ";
C++ - Sottrarre agli elementi della diagonale primaria un valore "x"
// Sottrarre agli elementi della diagonale primaria un valore "x" (matrice) #include <iostream> using namespace std; // ----- VARIABILI GLOBALI ----- // const int massimo=100; // ----- /VARIABILI GLOBALI ----- // // ----- PROTOTIPI ----- // int dimensione(); void caricamento_matrice(int [massimo][massimo], int, int); void stampa_matrice(int [massimo][massimo], int, int); void sottrazione(int [massimo][massimo], int, int); // ----- /PROTOTIPI ----- // // ----- FUNZIONI ----- // int dimensione() { int n; cout<<"Inserisci il numero di righe e colonne: "; cin>>n; while (n>massimo) { cout<<"Errore. Inserisci il numero di righe: "; cin>>n; } return n; } void caricamento_matrice(int m[massimo][massimo], int nr, int nc) { for (int i=0; i<nr; i++) { for (int j=0; j<nc; j++) { cout<<"Inserisci l'elemento della riga numero "<<i<<" e colonna numero "<<j<<": "; cin>>m[j]; } } } void stampa_matrice(int m[massimo][massimo], int nr, int nc) { for (int i=0; i<nr; i++) { for (int j=0; j<nc; j++) { cout<<m[j]<<"\t"; } cout<<endl; } } void sottrazione(int m[massimo][massimo], int nr, int nc) { int x; cout<<"Inserisci l'elemento da sottrarre: "; cin>>x; for (int i=0; i<nr; i++) { for (int j=0; j<nc; j++) { if (i==j) { m[j]=m[j]-x; } } } } // ----- /FUNZIONI ----- // // ----- PROGRAMMA PRINCIPALE ----- // int main() { int matrice[massimo][massimo]; int righe=dimensione(); int colonne=righe; cout<<endl; caricamento_matrice(matrice, righe, colonne); cout<<endl<<"Matrice caricata: "<<endl; stampa_matrice(matrice, righe, colonne); cout<<endl; sottrazione(matrice, righe, colonne); cout<<endl<<"Risultato: "<<endl; stampa_matrice(matrice, righe, colonne); return 0; } // ----- /PROGRAMMA PRINCIPALE ----- //