Module 05 Assignment – Modularizing Your Simple Cash Register Modify the program
ID: 3902006 • Letter: M
Question
Module 05 Assignment – Modularizing Your Simple Cash Register Modify the program you created in Module 04, to create a searchable telephone, by doing the following: The code for the input portion of the program is to be in a function by itself. This function will add individuals and their telephone numbers to the appropriate arrays. The code that sorts the arrays will be in a separate function. The function should take a parameter that indicates whether to sort ascending or descending. The code for printing the data from the arrays will be in a separate function. The code for searching for an individual in the arrays is to be in a separate function. The program should give the user the following menu options: Input data Sort data Ascending Sort data Descending Print all data Search for an individual in the data. End program The program will call the appropriate function based on user choice and the program will return to the menu afterwards (i.e. a perpetual loop). The program will only end when the user chooses End Program.
Explanation / Answer
In the question, it is not mentioned anything about programming language. I am writing it in c++.
#include <iostream>
#include<string.h>
using namespace std;
struct Telephone
{
char name[20];
long int number;
};
Telephone data[100];
static int l=0;
void add_data()
{
cout<<"enter name and mobile number";
cin>>data[l].name>>data[l].number;
l++;
}
void search()
{
char name_search[20];
int found=0;
cout<<"enter name for which number to be searched";
cin>>name_search;
for (int k=0;k<l;k++)
{
if(strcmp(data[k].name,name_search)==1)
{
found=1;
cout<<"found"<<data[k].number;
}
}
if(found==0)
{
cout<<"not found";
}
}
void print_data()
{
for(int k=0;k<l;k++)
{
cout<<data[k].name<<" "<<data[k].number;
}
}
void sort()
{
int choice,i,j;
cout<<"before sorting";
print_data();
Telephone temp;
cout<<"enter 1 for ascending 2 for descending order";
cin>>choice;
if (choice==1)
{
for(i=0; i<l; i++)
{
for(j=i+1; j<l; j++)
{
if(data[i].number>data[j].number)
{
temp=data[i];
data[i]=data[j];
data[j]=temp;
}
}
}
}
else
{
for(i=0; i<l; i++)
{
for(j=i+1; j<l; j++)
{
if(data[i].number<data[j].number)
{
temp=data[i];
data[i]=data[j];
data[j]=temp;
}
}
}
}
cout<<"after sorting";
print_data();
}
int main()
{
int i;
while(1)
{
cout<<"enter 1 for add data 2 for sorting 3 search 4 print 5 exit";
cin>>i;
switch(i)
{
case 1:
add_data();
break;
case 2:
sort();
break;
case 3:
search();
break;
case 4:
print_data();
break;
case 5:
exit(1);
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.