### Transposes

I need to write code to provide a 8 x 8 array of random numbers between 0 and 99. and displays the array and calculates the average of the array. Determine average of a user selected row or column. Transposes (element [0,0] to element [7,7] etc.) the array elements positions, then displays the array. This is my attempted code.

#include <iostream>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main(void)
{
int avg,navg;
int arr[8][8];
avg=0;
for( int i = 0 ; i < 8; i++ )
{
for( int j = 0 ; j < 8; j++ )
{
arr[i][j] = rand() % 100;
cout << arr[i][j] << " ";
avg = avg + arr[i][j];
navg= avg/64;

cout<< "average "<< navg <<endl;
cout << "Please enter your row or column. R before number for row and C before number for column. Number start at 0 and go through 7 " << endl;

char rowOrCollum;
int index;
scanf( "%c %i", &rowOrCollum, &index );
int i,j;
if (rowOrCollum == 'C' || 'c')
{
avg = 0;
for (i=0; i<8; i++)
{
avg = avg + arr[index][ i ];
navg= avg/8;
}
}
if (rowOrCollum == 'R' || 'r')
{
avg = 0;
for (j=0; j<8; j++)
{
avg = avg + arr[index][ j ];
navg= avg/8;
}
}
cout << navg<< endl;

int avg,navg;
int arr[8][8];
avg=0;
for( int i = 0 ; i < 8; i++ )
{
for( int j = 0 ; j < 8; j++ )
{
arr[i][j] = rand() % 100;
cout << arr[i][j] << " ";
char a,b,c,d;
cout << "input x and y value"<< endl;
cin >> i;
cin >> j;
cin >> c;
cin >> d;
a=arr[i][j];
b=arr[c][d];
arr[i][j]=b;
arr[c][d]=a;
}
cout << endl;

system ("pause");
return (0);
}

thanks.
Last edited on
Well, the code would be easier to read when formatted with the relevant tags,
[code]your code here [/code] - select using `<>` from the format menu on the right.

To fit within the page width, I split the string at line 24, otherwise the code is unaltered.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677`` ``````#include #include #include #include #include using namespace std; int main(void) { int avg,navg; int arr[8][8]; avg=0; for( int i = 0 ; i < 8; i++ ) { for( int j = 0 ; j < 8; j++ ) { arr[i][j] = rand() % 100; cout << arr[i][j] << " "; avg = avg + arr[i][j]; navg= avg/64; cout<< "average "<< navg <> i; cin >> j; cin >> c; cin >> d; a=arr[i][j]; b=arr[c][d]; arr[i][j]=b; arr[c][d]=a; } cout << endl; system ("pause"); return (0); }``````

Without considering whether the code achieves the intended goal, first we need to fix the coding errors. There are several closing braces `}` missing.
When you compiled this code, the compiler would have issued messages which are intended to help with identifying such problems.

It looks like the for-loops at lines 13 and 15 each require corresponding closing braces at line 21. The indentation suggests that line 20 `navg= avg/64;` belongs inside the loops, but I'd guess it might be better placed after the end of the two loops.

At lines 52 and 53 the existing variables avg,navg and arr are redeclared, which is an error. Those lines can be deleted.

Again at lines 55 and 57 there are two for loops with opening brace `{` but only a single closing brace at line 71.

When you have fixed those errors, the code should at least compile, and you can go on to test whether it works according to the specification.
Topic archived. No new replies allowed.