Program: losujący tablicę dwuwymiarową 5×5, po czym sumuje wszystkie liczby w wierszach jak i kolumnach.
Dokładana jest “kolejna kolumna” jak i “dodatkowy wiersz”, gdzie podliczane są wartości z tablicy dwuwymiarowej.
Kompilator: Dev C++
Kod programu:
//Sumowanie kolumn i wierszy w tablicy dwuwymiarowej
#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;
int main(int argc, char *argv[])
{
int Tab[5][5];
int Wiersz[5];
int Kolumny[5];
int los, suma_all;
suma_all = 0;
for(int j = 0; j < 5; j++)
Wiersz[j] = 0;
for(int j = 0; j < 5; j++)
Kolumny[j] = 0;
srand(time(NULL));
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
los = rand() % 10;
Tab[i][j] = los;
Wiersz[i] = Wiersz[i] + los;
Kolumny[j] = Kolumny[j] + los;
}
}
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++)
cout<<Tab[i][j]<<"\t";
cout<<Wiersz[i];
suma_all = suma_all + Wiersz[i];
cout<<endl;
}
for(int j = 0; j < 5; j++){
cout<<Kolumny[j]<<"\t";
suma_all = suma_all + Kolumny[j];
}
cout<<suma_all<<endl;
cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}