1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
//prg for 2darray
#include<iostream>
#define Num_Rows 2
#define Num_Columns 2
using namespace std;
//functions
int getTotal (int[][Num_Columns]);
double getAverage (int);
int getRowTotal(int[][Num_Columns],int);
int getColumnTotal(int[][Num_Columns],int);
double getAverage(int);
int getHighestInRow(int[][Num_Columns],int);
int getLowestInRow(int[][Num_Columns],int);
//main
int Main ()
{
//variables
int a,b,value,ch;
int num[Num_Rows][Num_Columns] = {15,30,45,60};
cout<< "The elements for this matrix are as follows:\n"<<endl;
for(a=0;a<Num_Rows;a++)
{
for(b=0;b<Num_Columns;b++)
cout<<num[a][b]<<"\t";
cout<<endl;
}
// user makes choice
for(;;)
{
cout<<"\nPlease choose one of the following options: "<<endl;
cout<<"1. Find Total:\n";
cout<<"2. Find Average:\n";
cout<<"3. Find Row Total:\n";
cout<<"4. Find Column total:\n";
cout<<"5. Find Highest Value in a row:\n";
cout<<"6. Find lowest value in a row:\n";
cout<<"7. Exit\n:";
cout<<"Please enter your selection: ";
cin>>ch;
//switch for function
switch(ch)
{
case 1: cout<<"Total: "<<getTotal(num)<<endl;
break;
case 2: value= getTotal(num);
cout<<"Average: "<<getAverage(value)<<endl;
break;
case 3: cout<<"Please enter a row number: ";
cin>>a;
if(a<0||a>Num_Rows)
{
cout<<"Selection must have between 0 and"<<Num_Rows<<" Please make another selection:\n";
break;
}
else
cout<<"the value of row "<<a<<"is: "<<getRowTotal(num,a)<<endl;
break;
case 4: cout<<"Please enter a column number: ";
cin>>a;
if(a<0||a>Num_Columns)
{
cout<<"Selection must have between 0 and"<<Num_Columns<<" Please make another selection:\n";
break;
}
else
cout<<"the value of coulmn "<<a<<"is: "<<getColumnTotal(num,a)<<endl;
break;
case 5: cout<<"Please enter a row number: ";
cin>>a;
if(a<0||a>Num_Rows)
{
cout<<"Selection must have between 0 and"<<Num_Rows<<" Please make another selection:\n";
break;
}
else
cout<<"Highest value in row "<<a<<"is: "<<getHighestInRow(num,a)<<endl;
break;
case 6: cout<<"Please enter a row number: ";
cin>>a;
if(a<0||a>Num_Rows)
{
cout<<"Selection must have between 0 and"<<Num_Rows<<" Please make another selection:\n";
break;
}
else
cout<<"Lowest value in row "<<a<<"is: "<<getLowestInRow(num,a)<<endl;
break;
case 7: system("pause");
return 0;
default: cout<<"Please make a correct selection\n";
}
}
}
//function for total of all values
int getTotal(int row[][Num_Columns])
{
//variables
int a,b,value=0;
for(a=0;a<Num_Rows;a++)
for(b=0;b<Num_Columns;b++)
value+=row[a][b];
return value;
}
//function for average
double getAverage(int totalValues)
{
double avg;
avg=totalValues/(Num_Rows*Num_Columns);
return avg;
}
//function for row totals
int getRowTotal(int num[][Num_Columns],int n)
{
//variables
int a,value=0;
for(a=0; a<Num_Columns;a++)
value+=num[n][a];
return value;
}
//function for Column totals
int getColumnTotal(int num[][Num_Columns],int n)
{
//variables
int a,value=0;
for(a=0; a<Num_Rows;a++)
value+=num[a][n];
return value;
}
//function for Highest value in row
int getHighestInRow(int num[][Num_Columns],int n)
{
//variables
int a,value=0;
value=num[n][0];
for(a=1;1<Num_Columns;a++)
if(num[n][a]>value)
value=num[n][a];
return value;
}
//function for lowest value in row
int getLowestInRow(int num[][Num_Columns],int n)
{
//variables
int a,value=0;
value=num[n][0];
for(a=1;1<Num_Columns;a++)
if(num[n][a]<value)
value=num[n][a];
return value;
}
|