Write a C++ data structure program to perform following
operations on Array using switch case.
(Note: Make class
Use Switch case for menu
Create single dimension array dynamically)
1) Linear Search in Double Dimension Array
2) Check whether Double Dimension Array is Sparse Matrix or NOT
#include<iostream>
#include<string>
using namespace std;
int main(){
int size;
int numberToSearch;
int counter=0;
double * numbers;
//Create a dynamic array of user defined size.
cout<<"Enter the size of the array: ";
cin>>size;
numbers=new double[size];
//Now take input in array from user.
for(int i=0;i<size;i++){
cout<<"Enter the number "<<(i+1)<<": ";
cin>>numbers[i];
}
int ch=-1;
while(ch!=3){
cout<<"1) Linear Search in Double Dimension Array\n";
cout<<"2) Check whether Double Dimension Array is Sparse Matrix or NOT\n";
cout<<"3. Exit\n";
cout<<"Your choice: ";
cin>>ch;
switch(ch){
case 1:
{
cout<<"\nEnter the number to search: ";
cin>>numberToSearch;
for(int i=0;i<size;i++){
if(numbers[i]==numberToSearch){
cout<<numbers[i]<< " index = "<<i<<"\n";
}
}
}
break;
case 2:
{
for(int i=0;i<size;i++){
if(numbers[i]==0){
counter++;
}
}
//A Sparse Matrix is a matrix(one-dimensional array) in which number of 0's
//is greater than the number of non-zero elements.
if(counter > ((int)size/2)){
cout<<"\nThe entered matrix is a sparse matrix\n\n";
}
else{
cout<<"\nThe entered matrix is not a sparse matrix\n\n";
}
}
break;
case 3:
break;
default:
cout<<"\nWrong menu item\n";
break;
}
}
delete[] numbers;
cin>>size;
return 0;
}
Comments
Leave a comment