Answer to Question #257416 in C++ for sanskar

Question #257416

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


1
Expert's answer
2021-10-28T01:27:54-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS