Friday, November 21, 2014

Four by Four Array

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
const int ROW = 4;
const int COL = 4;
unsigned seed = time(0);
srand(seed);
int FourbyFour[ROW][COL] = {{1 + rand() % 10, 1 + rand() % 10, 1 + rand() % 10, 1 + rand() % 10},
{1 + rand() % 10, 1 + rand() % 10, 1 + rand() % 10, 1 + rand() % 10},
{1 + rand() % 10, 1 + rand() % 10, 1 + rand() % 10, 1 + rand() % 10},
{1 + rand() % 10, 1 + rand() % 10, 1 + rand() % 10, 1 + rand() % 10}};
int sum;
cout << endl;

for (int x = 0; x < ROW; x++)
{
for (int y = 0; y < COL; y++)
{
cout << "  " << setw(2) << FourbyFour[x][y] << "  ";
}

cout << endl;
}

sum = (FourbyFour[0][0] + FourbyFour[1][1] + FourbyFour[2][2] + FourbyFour[3][3]);

cout << endl;
cout << "Diagonally, the sum of the numbers is  " << sum << "." << endl;
cout << endl;

return 0;
}

No comments:

Post a Comment