2D Arrays - C++ Label all outputs 1. Read in data for a 15 x 15 2d array (data f
ID: 3665435 • Letter: 2
Question
2D Arrays - C++
Label all outputs
1. Read in data for a 15 x 15 2d array (data found here: http://pastebin.com/6f3eEBXB)
2. Print Array
3. Compute and print average for all values in the 2-d array
4. Compute and print totals for each row for rows 5 through 10
5. Compute and print totals for each column for columns 2 through 8
6. Find the biggest numbers in each odd numbered rows.
7. Find the smallest number in each even numbered columns.
8. Sum all the values for columns 7 through 12 (one number answer)
9. Sum all the values for rows 10 through 14 (one number answer)
10. Sum together the biggest numbers from each column 2 through 8 (one number)
11. Sum together the smallest numbers from each row 0 through 6 (one number)
12. Find the four adjacent numbers that sum to the largest sum in the array. (Four adjacent numbes can be in a row, column, or one of the diagonals (8 possible directions)
Restrictions: Can only use these functions -
1. A readIn function to read the values into the 2-d array (fill columns, then rows)
2. A RowTotal function that accepts the 2d array, an integer (row) and returns the total for that row.
3. A ColTotal function that accepts the 2d array, an integer (column) and returns the total for that column.
4. A HighestInRow function that accepts the 2d array, row nbr returns the highest number in the row.
5. A HighestInCol function that accepts the 2d array, col nbr returns highest number in column.
6. A LowestInRow function that accepts the 2d array, row nbr returns the lowest number in the row.
7. A LowestInCol function that accepts the 2d array, col nbr returns lowest number in column.
8. A Print function that reads the array one row per line.
9. Write a function (or functions) that will find the 4 adjacent numbers (horizontal, vertical, or diagonal) in the array that sums to the largest value. The four numberes have to be adjacent values in one of 8 directions.
Explanation / Answer
1.Function to read the array A:
void Read(int A[][20], int N, int M)
{
for(int R=0;R<N;R++)
for(int C=0;C<M;C++)
{
cout<<"(R<<','<<")?";
cin>>A[R][C];
}
}
2.Function to display content of a two dimensional array A:
void Display(int A[][20],int N, int M)
{
for(int R=0;R<N;R++)
{
for(int C=0;C<M;C++)
cout<<setw(10)<<A[R][C];
cout<<endl;
}
}
3.Function to find & display average of rows & sum of cols. of a 2 dim. array A
void SumRowCol(int A[15][15], int N, int M)
{
for(int R=0;R<N;R++)
{
int SumR=0;
for(int C=5;C<10;C++)
SumR+=A[R][C];
cout<<"Row average("<<R<<")="<<SumR/5<<endl;
}
for(int R=0;R<N;R++)
{
int SumR=0;
for(int C=2;C<8;C++)
SumR+=A[R][C];
cout<<"Column average("<<R<<")="<<SumR<<endl;
}
}
4.function to find highest and lowest values in a 2d array.
void findMaxnMin(double x[][COLS])
{
int max=x[0][0], min= x[0][0];
int i,j;
for (i=0; i<ROWS; i++)
for (j=0; j<COLS; j++)
{
if(x[i][j]> max)
max= x[i][j];
if(x[i][j]<min)
min= x[i][j];
}
}
complete c++ program:
#include<stdio.h>
#include<stdlib.h>
int readIn(int m, int n, int arr[][n])
{
int i,j,avg=0;
FILE *fp,*fp1;
fp=fopen("girish.txt","r");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
fscanf(fp,"%d ",&arr[j][i]);
avg=avg+arr[j][i];
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",arr[j][i]);
}
}
printf("The avg is %d ",avg/225);
fclose(fp);
return 0;
}
int RowTotal(int m, int n, int arr[][n])
{
int i,j;
for(i=5;i<=10;i++)
{
int rowtotal=0;
for(j=1;j<n;j++)
{
rowtotal=rowtotal+arr[i][j];
}
printf("The %d th row total is %d ",i,rowtotal);
}
return 0;
}
int RowsTotal(int m, int n, int arr[][n])
{
int i,j;
int rowtotal=0;
for(i=10;i<=14;i++)
{
for(j=0;j<n;j++)
{
rowtotal=rowtotal+arr[i][j];
}
}
printf("The row total from 10 thru 14 is %d ",i,rowtotal);
return 0;
}
int ColsTotal(int m, int n, int arr[][n])
{
int i,j;
int coltotal=0;
for(i=7;i<=12;i++)
{
for(j=0;j<n;j++)
{
coltotal=coltotal+arr[j][i];
}
}
printf("The col total from 7 thru 12 is is %d ",coltotal);
return 0;
}
int ColTotal(int m, int n, int arr[][n])
{
int i,j;
for(j=2;j<=8;j++)
{
int coltotal=0;
for(i=0;i<n;i++)
{
coltotal=coltotal+arr[i][j];
}
printf("The %d th col total is %d ",j,coltotal);
}
return 0;
}
int HighestInRow(int m, int n, int arr[][n])
{
int i,j,max;
for(i=0;i<m;i++)
{
if(i%2==0)
{
max=arr[i][0];
for(j=1;j<n;j++)
{
if(arr[i][j]>max)
max=arr[i][j];
}
printf("The max in row %d is %d ",i,max);
}
}
return 0;
}
int HighestInCol(int m, int n, int arr[][n])
{
int i,j,max,maxcoltot=0;
for(i=2;i<=8;i++)
{
max=arr[0][i];
for(j=0;j<n;j++)
{
if(arr[j][i]>max)
max=arr[j][i];
}
maxcoltot=maxcoltot+max;
}
printf("The maxcoltotal for cols 2 through 8 is %d ",maxcoltot);
return 0;
}
int LowestInRow(int m, int n, int arr[][n])
{
int i,j,min,minrowtot=0;
for(i=0;i<=6;i++)
{
min=arr[i][0];
for(j=2;j<n;j++)
{
if(arr[i][j]>min)
min=arr[i][j];
}
minrowtot=minrowtot+min;
}
printf("The minrowtotal for rows 0 through 6 is %d ",minrowtot);
return 0;
}
int LowestInCol(int m, int n, int arr[][n])
{
int i,j,min;
for(i=0;i<m;i++)
{
if(i%2!=0)
{
min=arr[1][i];
for(j=1;j<n;j++)
{
if(arr[j][i]>min)
min=arr[j][i];
}
printf("The max in row %d is %d ",j,min);
}
}
return 0;
}
int main()
{
int arr[16][16],m,n;
m=16;n=16;
readIn(m, n, arr);
RowTotal(m, n, arr);
ColTotal(m, n, arr);
HighestInRow(m, n, arr);
LowestInRow(m, n, arr);
HighestInCol(m, n, arr);
LowestInCol(m, n, arr);
RowsTot(m, n, arr);
ColsTot(m, n, arr);
return 0;
}
12.largest sum of 4 adjacent numbers:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.