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
|
//TwoDimensionalArray.cpp
//Output data from two-dimensional array
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
void printArray(const int array[][4],const int r,const int c);
void printOddNumbers(const int array[][4],const int r,const int c);
void printEvenNumbers(const int array[][4],const int r,const int c);
int main(){
const int ROW=5;
const int COL=4;
int option;
int array[ROW][COL]={{4,-3,4,2},{-2,1,4,3},{5,3,6,-4},{3,2,7,5},{2,2,-9,6}};
do{
cout<<"\nI have a two-dimensional array of numbers"
<<"\nPress 1 to show odd numbers"
<<"\nPress 2 to show even numbers"
<<"\nPress 3 to print the whole array"
<<"\nPress 0 to quit."<<endl;
cin>>option;
while(option>3||option<0){
cout<<"\nPress 1 to show odd numbers"
<<"\nPress 2 to show even numbers"
<<"\nPress 3 to print the whole array"
<<"\nPress 0 to quit."<<endl;
cin>>option;
}//end while
switch(option){
case 1:
cout<<endl;
printOddNumbers(array,ROW,COL);
break;
case 2:
cout<<endl;
printEvenNumbers(array,ROW,COL);
break;
case 3:
cout<<endl;
printArray(array,ROW,COL);
break;
case 0:
break;
default:
break;
}//end switch
}while(option!=0);
return 0; //indicates success
}//end main
void printArray(const int array[][4],const int r,const int c){
for(int row=0;row<r;row++){
for(int col=0;col<c;col++)
cout<<array[row][col]<<' ';
cout<<endl;
}//end outer for
}//end function printArray
void printOddNumbers(const int array[][4],const int r,const int c){
for(int row=0;row<r;row++){
for(int col=0;col<c;col++){
if(array[row][col]%2==0)
continue;
else
cout<<array[row][col]<<' ';
}//end inner for
cout<<endl;
}//end outer for
}//end function printOddNumbers
void printEvenNumbers(const int array[][4],const int r,const int c){
for(int row=0;row<r;row++){
for(int col=0;col<c;col++){
if(array[row][col]%2==0)
cout<<array[row][col]<<' ';
else
continue;
}//end inner for
cout<<endl;
}//end outer for
}//end function printEvenNumbers
|