Mix of many 2D ARRAY Programs in one with Menu Driven Options and User Defined Functions in C++
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<iomanip.h>
//Prototyping of Functions
void FillArray(int [][10], int);
void DisplayArray(int [][10], int);
void LeftUpperHalf(int [][10], int);
void RightUpperHalf(int [][10], int);
void LeftLowerHalf(int [][10], int);
void RightLowerHalf(int [][10], int);
void main()
{
int ch,n,arr[10][10];
do
{
clrscr();
cout<<"Menu of 2D Array Programs\n";
cout<<"=========================\n";
cout<<"0. Exit\n";
cout<<"1. Fill 2D Array\n";
cout<<"2. Display Filled Array\n";
cout<<"3. Display Left Upper Half with Sum\n";
cout<<"4. Display Right Upper Half with Sum\n";
cout<<"5. Display Left Lower Half with Sum\n";
cout<<"6. Display Right Lower Half with Sum\n";
cout<<"Enter your choice: ";
cin>>ch;
clrscr();
switch(ch)
{
case 1: cout<<"Enter Size of Array (Max. 10) : ";
cin>>n;
FillArray(arr,n); break;
case 2: DisplayArray(arr,n); break;
case 3: LeftUpperHalf(arr,n); break;
case 4: RightUpperHalf(arr,n); break;
case 5: LeftLowerHalf(arr,n); break;
case 6: RightLowerHalf(arr,n); break;
}
getch();
}while(ch);
}
void FillArray(int a[][10], int n)
{
int i, j;
randomize();
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=random(21)+5;
}
}
cout<<"\nThe Values in 2D Array have been filled....\n";
}
void DisplayArray(int a[][10], int n)
{
int i,j;
cout<<"\nThe Values in Array are";
cout<<"\n=========================\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<setw(5)<<a[i][j];
}
cout<<endl;
}
}
void LeftUpperHalf(int a[][10], int n)
{
int i,j,sum=0;
cout<<"\nThe Left Upper Half of Array";
cout<<"\n============================\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(j<n-i)
{
cout<<setw(5)<<a[i][j];
sum=sum+a[i][j];
}
else
{
cout<<setw(5)<<" ";
}
}
cout<<endl;
}
cout<<"\nThe Sum of Above Values is:"<<sum<<endl;
}
void RightUpperHalf(int a[][10], int n)
{
int i,j,sum=0;
cout<<"\nThe Right Upper Half of Array";
cout<<"\n=============================\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(j>=i)
{
cout<<setw(5)<<a[i][j];
sum=sum+a[i][j];
}
else
{
cout<<setw(5)<<" ";
}
}
cout<<endl;
}
cout<<"\nThe Sum of Above Values is:"<<sum<<endl;
}
void RightLowerHalf(int a[][10], int n)
{
int i,j,sum=0;
cout<<"\nThe Right Lower Half of Array";
cout<<"\n=============================\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(j>=n-i-1)
{
cout<<setw(5)<<a[i][j];
sum=sum+a[i][j];
}
else
{
cout<<setw(5)<<" ";
}
}
cout<<endl;
}
cout<<"\nThe Sum of Above Values is:"<<sum<<endl;
}
void LeftLowerHalf(int a[][10], int n)
{
int i,j,sum=0;
cout<<"\nThe Left Lower Half of Array";
cout<<"\n=============================\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(j<=i)
{
cout<<setw(5)<<a[i][j];
sum=sum+a[i][j];
}
else
{
cout<<setw(5)<<" ";
}
}
cout<<endl;
}
cout<<"\nThe Sum of Above Values is:"<<sum<<endl;
}
No comments
Post your comments