I have the following program; I can get the answers to write to an out file but
ID: 3538216 • Letter: I
Question
I have the following program; I can get the answers to write to an out file but can not get the array itself to print. I need the syntax to get it to do this for only the array with a tab to seperate the values into a readable format.
#include <iostream>
#include <fstream>
using namespace std;
void read(int a[15][15])
{
int i,j;
ifstream infile("TwoDim.txt");
for(i=0;i<15;i++){
for(j=0;j<15;j++){
infile>>a[i][j];
}
}
return;
}
int gettotal(int a[15][15])
{
int s=0,i,j;
for(i=0;i<15;i++){
for(j=0;j<15;j++){
s+=a[i][j];
}
}
return s;
}
int getaverage(int a[15][15])
{
int s = gettotal(a);
return s/225;
}
int getrowtotal(int a[15][15],int b)
{
int i,s=0;
for(i=0;i<15;i++){
s+=a[b][i];
}
return s;
}
int getcoltotal(int a[15][15],int b)
{
int i,s=0;
for(i=0;i<15;i++){
s+=a[i][b];
}
return s;
}
int gethighestinrow(int a[15][15],int b)
{
int p,i;
p=a[b][0];
for(i=1;i<15;i++){
if(a[b][i]>p) p=a[b][i];
}
return p;
}
int gethighestincol(int a[15][15],int b)
{
int p,i;
p=a[0][b];
for(i=1;i<15;i++){
if(a[i][b]>p) p=a[i][b];
}
return p;
}
int getlowestinrow(int a[15][15],int b)
{
int p,i;
p=a[b][0];
for(i=1;i<15;i++){
if(a[b][i]<p) p=a[b][i];
}
return p;
}
int getlowestincol(int a[15][15],int b)
{
int p,i;
p=a[0][b];
for(i=1;i<15;i++){
if(a[i][b]<p) p=a[i][b];
}
return p;
}
void printarray(int a[15][15])
{
int i,j;
for(i=0;i<15;i++){
for(j=0;j<15;j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return;
}
void highestsum(int a[15][15])
{
int i,j,s;
s=a[0][0]+a[0][1]+a[0][2]+a[0][3];
for(i=0;i<15;i++){
for(j=0;j<15;j++){
if(i<12 && j<12){
if(s<a[i][j]+a[i][j+1]+a[i][j+2]+a[i][j+3]) s=a[i][j]+a[i][j+1]+a[i][j+2]+a[i][j+3];
if(s<a[i][j]+a[i+1][j]+a[i+2][j]+a[i+3][j]) s=a[i][j]+a[i+1][j]+a[i+2][j]+a[i+3][j];
if(s<a[i][j]+a[i+1][j+1]+a[i+2][j+2]+a[i+3][j+3]) s=a[i][j]+a[i+1][j+1]+a[i+2][j+2]+a[i+3][j+3];
}
}
}
for(i=0;i<15;i++){
for(j=0;j<15;j++){
if(i<12 && j<12){
if(s==a[i][j]+a[i][j+1]+a[i][j+2]+a[i][j+3])
cout<<a[i][j]<<" "<<a[i][j+1]<<" "<<a[i][j+2]<<" "<<a[i][j+3]<<" "<<endl;
if(s==a[i][j]+a[i+1][j]+a[i+2][j]+a[i+3][j])
cout<<a[i][j]<<" "<<a[i+1][j]<<" "<<a[i+2][j]<<" "<<a[i+3][j]<<" "<<endl;
if(s==a[i][j]+a[i+1][j+1]+a[i+2][j+2]+a[i+3][j+3])
cout<<a[i][j]<<" "<<a[i+1][j+1]<<" "<<a[i+2][j+2]<<" "<<a[i+3][j+3]<<" "<<endl;
}
}
}
return;
}
int main()
{
ofstream outFile ("outfile.txt");
int a[15][15],i,s=0;
read(a);
printarray(a);
for(i=5;i<=10;i++){
cout<<"sum of "<<i<<" row: "<<getrowtotal(a,i)<<endl;
outFile <<"sum of "<<i<<" row: "<<getrowtotal(a,i)<<endl;
}
for(i=2;i<=8;i++){
cout<<"sum of "<<i<<" col: "<<getcoltotal(a,i)<<endl;
outFile<<"sum of "<<i<<" col: "<<getcoltotal(a,i)<<endl;
}
for(i=1;i<15;i+=2){
cout<<"biggest of "<<i<<" row "<<gethighestinrow(a,i)<<endl;
outFile<<"biggest of "<<i<<" row "<<gethighestinrow(a,i)<<endl;
}
for(i=0;i<15;i+=2){
cout<<"biggest of "<<i<<" col "<<gethighestincol(a,i)<<endl;
outFile<<"biggest of "<<i<<" col "<<gethighestincol(a,i)<<endl;
}
for(i=7;i<=12;i++){
s+=getcoltotal(a,i);
}
cout<<"col total 7 to 12 : "<<s<<endl;
outFile<<"col total 7 to 12 : "<<s<<endl;
s=0;
for(i=9;i<=14;i++){
s+=gethighestincol(a,i);
}
cout<<"col highest sum 9 to 14 : "<<s<<endl;
outFile<<"col highest sum 9 to 14 : "<<s<<endl;
s=0;
for(i=0;i<=6;i++){
s+=getlowestinrow(a,i);
}
cout<<"row lowest sum 0 to 6 : "<<s<<endl;
outFile<<"row lowest sum 0 to 6 : "<<s<<endl;
cout<<"number that add to highest sum: "<<endl;
outFile<<"number that add to highest sum: "<<endl;
highestsum(a);
return 0;
}