ravi1986 -1 Newbie Poster

write a programme that creates a two dimentional array initialised with test data. use 4 rows and 5 columndatatype. the programme should have the following functions:
1.getTotal:-
this function should accept a two dimentional array as its argument and return the total of all the values in the array
2. getAverage
3. getRowTotal
4. getColumnTotal
5. getHighestInRow
6 getLowestInRow
Here is the answer for this program please see in the bottom for help I'm looking for.

# include <iostream>
#define NROW 4
#define NCOL 5
using namespace std;
int getTotal(int[][NCOL]);
int getRowTotal(int[][NCOL],int);
int getColumnTotal(int[][NCOL],int);
double getAverage(int);
int getLowestInRow(int[][NCOL],int);
int getHighestestInRow(int[][NCOL],int);
int main ()
{int i,j,choice;
int num[NROW][NCOL]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20},val;
cout<<"The Matrix\n";
for(i=0;i<NROW;i++)
   {for(j=0;j<NCOL;j++)
       cout<<num[i][j]<<"\t";
    cout<<endl;
    }
for(;;)
{cout<<"Choose what you would like to do\n";
cout<<"1  get Total of all values in matrix: \n";
cout<<"2  get Average of all values in matrix:\n";
cout<<"3  get a Row Total\n";
cout<<"4  get a Column Total\n";
cout<<"5  get the Highest value In a Row\n";
cout<<"6  get the Lowest value In a Row\n";
cout<<"7  Exit\n";
scanf("%d",&choice);
  switch(choice)
     {case 1: cout<<"Total= "<<getTotal(num)<<endl;
              break;
      case 2: val=getTotal(num);
              cout<<"The average is "<<getAverage(val)<<endl;
              break;
      case 3: cout<<"Enter row : ";
              cin>>i;
              if(i<0||i>=NROW)
                   {cout<<"Error-must be between 0 and "<<NROW<<"\n";
                   break;
                   }
              else
                  cout<<"Row "<<i<<" total is "<<getRowTotal(num,i)<<endl;
              break;
      case 4: cout<<"Enter column: ";
              cin>>i;
              if(i<0||i>=NCOL)
                   {cout<<"Error-must be between 0 and "<<NCOL<<"\n";
                    break;
                   }
              else
                   cout<<"Column "<<i<<" total is "<<getColumnTotal(num,i)<<endl;
              break;  
      case 5: cout<<"Enter row : ";
              cin>>i;
              if(i<0||i>=NROW)
                  {cout<<"Error-must be between 0 and "<<NROW<<"\n";
                   break;
                   }
              else
                   cout<<"Highest element in row "<<i<<" is "<<getHighestestInRow(num,i)<<endl;
              break;
      case 6: cout<<"Enter which row : ";
              cin>>i;
              if(i<0||i>=NROW)
                  {cout<<"Error-must be between 0 and "<<NROW<<"\n";
                   break;
                   }
              else
                   cout<<"Lowest element in row "<<i<<" is "<<getLowestInRow(num,i)<<endl;
              break;
      case 7: system("pause");
              return 0;                
      default: cout<<"Error!! Try Again\n\n";
      }
}

}

int getTotal(int r[][NCOL])
{int i,j;
int val=0;
for(i=0;i<NROW;i++)
     for(j=0;j<NCOL;j++)
   val+=r[i][j];
return val;
}
int getHighestestInRow(int num[][NCOL],int n)
{int i;
int val=0;
val=num[n][0];
for(i=1;i<NCOL;i++)
   if(num[n][i]>val)
      val=num[n][i];
return val;
}
double getAverage(int t)
{return (double)t/(NROW*NCOL);
}
int getLowestInRow(int num[][NCOL],int n)
{int i;
int val=0;
val=num[n][0];
for(i=1;i<NCOL;i++)
   if(num[n][i]<val)
      val=num[n][i];
return val;
}
int getRowTotal(int num[][NCOL],int n)
{int i;
int val=0;
for(i=0;i<NCOL;i++)
   val+=num[n][i];
return val;
}
int getColumnTotal(int num[][NCOL],int n)
{int i;
int val=0;
for(i=0;i<NROW;i++)
   val+=num[i][n];
return val;
}

Now how could I change the program with the same challenge using a pointer to the data type (*ptr) and the calculated locations.
AND
The same challenge using a pointer to a pointer to the data type(**ptr).
For all of these, PASS any size information needed into the function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.