Witam, miałem do utworzenia tablicę dwuwymiarową i policzyć sumę na przekątnych, sumę na obrzeżach z tym sobię poradziłem, ale nie wiem jak zmienić resztę elementów na 0.
Z góry dziękuje za pomoc
#include <iostream>
#include <cstdlib>
#include <ctime>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
srand( time( NULL ) );
/*deklaracja*/
int tab[7][7];
int i,j;
int p1=0;
int p2=0;
int g1=0;
int d1=0;
int b1=0;
int b2=0;
/*menu*/
system("color 1e");
cout<<"===================="<<endl;
cout<<"===================="<<endl;
cout<<"Zadanie domowe nr 24"<<endl;
cout<<"===================="<<endl;
cout<<"===================="<<endl;
cout<<endl;
/*Pętla z losowaniem rand - wyświetlanie wierszy i kolumn*/
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
tab[i][j]= rand() % 10;
cout<<tab[i][j];
}
cout<<endl;
/*Suma przekątnych*/
}
for(i=0;i<7;i++)
{
p1=p1+tab[i][i];
}
/*Suma drugiej przekątnej*/
for(i=0;i<7;i++)
{
p2=p2+tab[i][6-i];
}
/*Suma boku 1*/
for(i=0;i<1;i++)
{
for(j=0;j<7;j++)
b1=b1+tab[j][i];
}
/*Suma boku 2*/
for(i=6;i<7;i++)
{
for(j=0;j<7;j++)
b2=b2+tab[j][i];
}
/*Suma gory*/
for(i=0;i<7;i++)
{
for(j=0;j<1;j++)
g1=g1+tab[j][i];
}
/*Suma dolu 2*/
for(i=0;i<7;i++)
{
for(j=6;j<7;j++)
d1=d1+tab[j][i];
}
/*Wyniki*/
cout<<endl;
cout<<"Suma przekatnej to: "<<p1<<endl;
cout<<"Suma drugiej przekatnej to: "<<p2<<endl;
cout<<"Laczna suma przekatnych to: "<<p1+p2<<endl<<endl;
cout<<"Suma boku 1 to: "<<b1<<endl;
cout<<"Suma boku 2 to: "<<b2<<endl;
cout<<"Suma gory to: "<<g1<<endl;
cout<<"Suma dolu to: "<<d1<<endl;
cout<<"Suma obrzezy to: "<<b1+b2+g1+d1<<endl;
system("pause");
return 0;
}