Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> using namespace std; #define array_size 11 float add_values(

ID: 3703077 • Letter: #

Question


#include <iostream> using namespace std; #define array_size 11
float add_values(float the_array[array_size] );// this function now can use the array float edit_values(float the_array[array_size]); float the_array[array_size]; int counter=1; char firstname[100]; char lastname[100]; char day[100]; int student_id; int ID; int track_value; float print_display(float the_array[array_size],int student_id,char lastname[100],char firstname[100],char day[100]); float sort_values(float the_array[array_size]); float reset_array(float the_array[array_size]); float search_value(float the_array[array_size],int k,int track_value); float stack_values(float the_array[array_size]); float display_values(float the_array[array_size]); void enter_student(char firstname[100],char day[100], char lastname[100], int student_id); int leave_program(); int reset=1; main()
{ char number[]={'a','b','c','d','e','f','g','h','i','j','o'}; char input; cout<<"Welcome! what do you want to do? Enter the letter you wish to perform"<<endl; cout<<"(a): Add a value"<<endl; cout<<"(b): Edit a value"<< endl; cout<<"(c): Display values"<<endl; cout<<"(d): Display statistics"<<endl; cout<<"(e): Exit from program"<< endl; cout<<"(f): Reset value entries"<<endl; cout<<"(g): Search for stored value"<<endl; cout<<"(h): Sort stored values"<<endl; cout<<"(i): Display specified range"<<endl; cout<<"(j): Input student information"<<endl; cout<<"Enter the program number here: "; cin>>input; //should the user input an invalid option, a loop will begin asking for a proper value if((input!='a')&&(input!='b')&&(input!='c')&&(input!='d')&&(input!='e')&&(input!='f')&&(input!='g') &&(input!='h')&&(input!='i')&&(input!='j')){ while((input!='a')&&(input!='b')&&(input!='c')&&(input!='d')&&(input!='e')&&(input!='f')&&(input!= 'g')&&(input!='h')&&(input!='i')&&(input!='j')){ cout<<"that option is invalid, please try again"; cin>>input; if((input=='a')||(input=='b')||(input=='c')||(input=='d')||(input=='e')||(input=='f')||(input=='g')||(input=='h')||(input=='i')||(input=='j')){ break;} } }
if(input==number[0]){//add value is selected
add_values(the_array);//function call
cout<<"the amount of numbers in the array is :"<<counter<<endl; cout<<"Returning to Main Menu"<<endl; cout<<endl; return(main());} else;
if(input==number[1]){//edit value is selected cout<<"you want to edit a value"<<endl; cout<<"calling function now"<<endl; edit_values(the_array); } else; //print function is selected if(input==number[2]){ cout<<"Displaying your saved values has been selected"<<endl; cout<<"you entered a total of ("<<counter<<" )values"<<endl; print_display(the_array,student_id,lastname,firstname,day); } else; //sorting is selected if(input==number[3]){ cout<<"You have chosen to sort your values"<<endl; cout<<"you have entered "<<counter<<" values"<<endl; sort_values(the_array);}
if(input==number[4]){//exit from the program has been selected cout<<"You have chosen to exit the program"<<endl; cout<<"Thanks for your time!"<<endl; leave_program();}
if(input==number[5]){//reset the array has been selected cout<<"You have chosen to reset the array."<<endl; reset_array(the_array); }
if(input==number[6]){//search value has been chosen cout<<"You have chosen to search the array for a value."<<endl; cout<<"Your values will first be sorted in ascending order before the search begins."<<endl; float k; int element; search_value(the_array,k,track_value);
if(element==-1){ cout<<"The value was not found."<<endl;} else; cout<<"Found value in slot."<<element; }
if(input==number[7]){//sorting in ascending or descending order cout<<" You have chosen to sort your saved values."<<endl; stack_values(the_array);}
if(input==number[8]){//display some data within a range cout<<"You have chosen to display some data"<<endl; display_values(the_array);}
if(input==number[9]){ cout<<" You have chosen to input some personal student information."<<endl; enter_student(firstname,day,lastname,student_id); return(main());}}
void enter_student(char firstname[100],char day[100], char lastname[100], int student_id){
cout<<"Please enter your first name:"<<endl; cin>>firstname; cout<<"Please enter your last name :"<<endl; cin>>lastname; cout<<"Please enter your student I.D :"<<endl; cin>>student_id; cout<<"Please enter the current day of the week"<<endl; cin>>day; ID=student_id;}
float display_values(float the_array[array_size]){//displays a range, single or all stored numbers
int number; cout<<"How many numbers would you like to display"<<endl; cout<<" (1)- A single values"<<endl; cout<<" (2)- Multiple values"<<endl; cout<<" (3)- All stored values"<<endl; cin>>number; if((number>3)||(number<1)) while((number>3)||(number<1)){ cout<<"That was an invalid option. Please try again."<<endl; cin>>number; if((number>0)&&(number<4)){ cout<<"Input accepted, moving forward now."<<endl; break;}} if(number==1){//displays a single value int g; cout<<"Which memory slot would you like to display."<<endl; cin>>g; if((g<1)||(g>10)){ while((g<1)||(g>10)){ cout<<" Sorry that slot does not exist in the array."<<endl; cout<<"Please try again."<<endl; cin>>g; if((g>1)&&(g<11)){ break;}}} cout<<"("<<the_array[g]<<") is saved to memory slot("<<g<<")."<<endl; return(main()); }
if(number==2){//display multiple int h,g; cout<<"Please select a range of memory slot you would like to display."<<endl; cout<<"Please enter the first and final slots you would like to search between."<<endl; cout<<"Initial slot= "; cin>>h; cout<<"Final slot= "; cin>>g; if(h>g){ while(h>g){ cout<<"That range unavailable work pick a new one"<<endl; cout<<"Initial="; cin>>h; cout<<"Final="; cin>>g; if(h<g){ break;} }} else; if((h>counter)||(g>counter)){ while((h>counter)||(g>counter)){ cout<<" One or both of your inputs is outside the available range. Please try again."<<endl; cout<<"Initial= "; cin>>h; cout<<"Final= "; cin>>g; if((h<counter)&&(g<counter)){ cout<<" That is a valid range."<<endl; break;}}} cout<<"You have selected the range of slot initial to be ("<<h<<") and the range final to be ("<<g<<")."<<endl; cout<<"Displaying your values now."<<endl; for(h;h<=g;h++){ cout<<"Slot("<<h<<")="<< the_array[h]<<endl;} return(main());} else; if(number==3){ cout<<"Displaying all number stored in array."<<endl; for(int i=1;i<=counter;i++){ cout<<"Slot("<<i<<")="<<the_array[i]<<endl;} return(main());}}
float stack_values(float the_array[array_size]){//ascending or descending int g; cout<<"Would you like to sort your values? "<<endl; cout<<" (1)- Ascending order."<<endl; cout<<" (2)-Desending order."<<endl; cin>>g; if(g==1){ cout<<"Sorting your numbers in an ascending order."<<endl; cout<<"This will alter the original order you inputed the values in."<<endl;//sort ascending order( bubble sort) int j; int i; for(i=0;i<=counter;i++){ for(j=i+1;j<=counter;j++){ float l; if(the_array[i]>the_array[j]){ l=the_array[i]; the_array[i]=the_array[j]; the_array[j]=l;}}} for(i=1;i<=counter;i++){ cout<<the_array[i]<<endl;} return(main());} if(g==2){ cout<<"Sorting your values in a descending order."<<endl;//sort ascending order( bubble sort) int j; int i; for(i=1;i<=counter;i++){ for(j=i;j<=counter;j++){ float l; if(the_array[i]<the_array[j]){ l=the_array[i]; the_array[i]=the_array[j]; the_array[j]=l;}}} for(i=1;i<=counter;i++){ cout<<the_array[i]<<endl;} return(main()); }}
float search_value(float the_array[array_size],int k,int track_value){//search and display the slot of a value int j=1; int z; int h; int binary_num; for(h=0;h<=counter;h++){// sorts number in ascending order for(z=h+1;z<=counter;z++){ float l; if(the_array[h]>the_array[z]){ l=the_array[h]; the_array[h]=the_array[z]; the_array[z]=l;}}} if((k<-1000)||(k>1000)){// if invalid number is entered while((k<-1000)||(k>1000)){ cout<<"That number is invalid. Please try again."<<endl; cin>>k; if((k>-1000)&&(k<10000)){ cout<<"That number is acceptable."<<endl; break;}}} //Binary search int first = 1; int mid = 0; int entered_number; bool found_value = false; int g; int y = counter; int length = counter; int last = counter; cout<<"Please enter the value you wish to search."<<endl; cin>>entered_number; while (first<=last&&!found_value) {if(first==1) {for(int counter=length-1;counter>0;counter--){ for(int h=0;h<y;h++){ if (the_array[h]>the_array[h+1]){ int temp; temp=the_array[h+1]; the_array[h+1]=the_array[h]; the_array[h]=temp;}} y--;}} mid=(first+last)/2; if(entered_number>the_array[mid]) {first= mid+1;} else if(entered_number<the_array [mid]) {last=mid-1;} else {cout<<" the number ("<< entered_number <<") was found in array slot ["<< mid <<"]"<<endl; found_value = true; return(main());} } cout<<"That values does not exist in the current array."<<endl; cout<<"Returning you to Main Menu."<<endl; return(main()); }
float reset_array(float the_array[array_size])//reset the array { cout<<"Resetting the array to it's default state."<<endl; for(int k=1;k<=counter; k++){ the_array[k]=0; counter=1; reset=1; } cout<<"Returning you to Main Menu now."<<endl; return(main()); }
float sort_values(float the_array[array_size])//sorting function.
{ cout<<"You have chosen to display some simple statistics of your array."<<endl; cout<<endl; int position; int i; float largest_value=-10000; float smallest_value=10000; for(i=1;i<=counter;i++){ if(the_array[i]>largest_value){ largest_value=the_array[i]; }} for(i=1;i<=counter;i++){ if(the_array[i]<smallest_value){ smallest_value=the_array[i]; } } float sum=0; for(int i=0;i<=counter;i++){ sum+=the_array[i]; } cout<<"the sum of the array is "<<sum<<endl; cout<<endl; cout<<"the average value of the array is = "<<sum/counter<<endl; cout<<"your largest value is "<<largest_value<<endl; cout<<"your smallest value is "<<smallest_value<<endl; return(main()); }
float add_values(float the_array[array_size] ){ //function for storing numbers in array int h=reset;//controls loop float x=0;//wil be assigned into a array slot cout<<"You have chosen to add values"<<endl; cout<<"Please enter a valid value, or, enter 9999 to exit this function and return to Main Menu."<<endl; cout<<"This function will automatically end after 10 values have been entered."<<endl; for( h;h<=10;h++){ cin>>x; if((x<-999)&&(x!=9999)){ while(x<-999){ cout<<"That is an invalid number."<<endl; cin>>x; if((x>=-999)&&(x<=999)){ cout<<"That is a valid number"<<endl; break;}}} else; if((x>999)&&(x!=9999)){ while(x>999){ cout<<"That is an invalid number."<<endl; cin>>x; if((x>=-999)&&(x<=999)){ cout<<"That is a valid number."<<endl; break; }}} else; if(x==9999){//kick out of function back to menu cout<<"You have entered("<<counter<<") number of values."<<endl; cout<<"Returning to Main Menu."<<endl; h=counter; return(main());} else; if((x>=-999)&&(x<=999)){//if the number is good the_array[h]=x;// storing the number entered into the array slot counter=h; reset=counter+1;} else; if(h==10){ the_array[h]=x;// storing the number entered into the array slot counter=h; reset=counter+1; break;} cout<<"Please enter another values."<<endl;} cout<<"Thank you for entering 10 values."<<endl; h=counter;}
int leave_program(){ cout<<"Thank you for using this program."<<endl;}
float edit_values(float the_array[array_size]){//edit value
int i; cout<<"Please enter the slot you would like to edit: "; cin>>i; if((i>10)||(i<1)){// if the number you enter is bad while((i>10)||(i<1)){ cout<<"That memory slot does not exist, please try again."<<endl; cin>>i; if((i>0)&&(i<=10)){ cout<<"That is a better number"<<endl; break;}}} cout<<"The value in slot "<<i<<" is "<<the_array[i]<<"."<<endl; cout<<"What would you like the new saved value to be?"<<endl; cin>>the_array[i]; if((the_array[i]>999)||(the_array[i]<-999)){ while((the_array[i]>999)||(the_array[i]<-999)){ cout<<"That value exceeds the limits that the array accepts. Please try again."<<endl; cin>>the_array[i]; if((the_array[i]>-999)&&(the_array[i]<999)){ cout<<"That is a valid number."<<endl; break;} return(main());} cout<<"The new value for slot "<<i<<" is: "<<the_array[i]<<endl; cout<<"Returning you to Main Menu."<<endl; return(main());}}

float print_display(float the_array[array_size],int student_id,char lastname[100],char
firstname[100],char day[100]){//print function
cout<<"You have entered Display values."<<endl; int banana; int limit; int yes_no=0; int yes_no2=0; int yes_no3=0; cout<<" For the following questions please input 1 for yes or 2 for no"<<endl; cout<<" Would you like to display your name?"<<endl; cin>>yes_no; cout<< "Would you like to display your student I.D?"<<endl; cin>>yes_no2; cout<<" would you like to display the day of the week?"<<endl; cin>>yes_no3; if(yes_no==1){ cout<<"Name:["<<firstname<<" "<<lastname<<"] "<<endl;} else; if(yes_no2==1){ cout<<" ID:["<<ID<<"] "<<endl;} else; if(yes_no3==1){ cout<<"Day:["<<day<<"]"<<endl;} else; limit=counter; for(banana=1;banana<=limit;banana++){ cout<<"the array slot "<<banana<<"is equal="<<the_array[banana]<<endl; } return(main()); } #include <iostream> using namespace std; #define array_size 11
float add_values(float the_array[array_size] );// this function now can use the array float edit_values(float the_array[array_size]); float the_array[array_size]; int counter=1; char firstname[100]; char lastname[100]; char day[100]; int student_id; int ID; int track_value; float print_display(float the_array[array_size],int student_id,char lastname[100],char firstname[100],char day[100]); float sort_values(float the_array[array_size]); float reset_array(float the_array[array_size]); float search_value(float the_array[array_size],int k,int track_value); float stack_values(float the_array[array_size]); float display_values(float the_array[array_size]); void enter_student(char firstname[100],char day[100], char lastname[100], int student_id); int leave_program(); int reset=1; main()
{ char number[]={'a','b','c','d','e','f','g','h','i','j','o'}; char input; cout<<"Welcome! what do you want to do? Enter the letter you wish to perform"<<endl; cout<<"(a): Add a value"<<endl; cout<<"(b): Edit a value"<< endl; cout<<"(c): Display values"<<endl; cout<<"(d): Display statistics"<<endl; cout<<"(e): Exit from program"<< endl; cout<<"(f): Reset value entries"<<endl; cout<<"(g): Search for stored value"<<endl; cout<<"(h): Sort stored values"<<endl; cout<<"(i): Display specified range"<<endl; cout<<"(j): Input student information"<<endl; cout<<"Enter the program number here: "; cin>>input; //should the user input an invalid option, a loop will begin asking for a proper value if((input!='a')&&(input!='b')&&(input!='c')&&(input!='d')&&(input!='e')&&(input!='f')&&(input!='g') &&(input!='h')&&(input!='i')&&(input!='j')){ while((input!='a')&&(input!='b')&&(input!='c')&&(input!='d')&&(input!='e')&&(input!='f')&&(input!= 'g')&&(input!='h')&&(input!='i')&&(input!='j')){ cout<<"that option is invalid, please try again"; cin>>input; if((input=='a')||(input=='b')||(input=='c')||(input=='d')||(input=='e')||(input=='f')||(input=='g')||(input=='h')||(input=='i')||(input=='j')){ break;} } }
if(input==number[0]){//add value is selected
add_values(the_array);//function call
cout<<"the amount of numbers in the array is :"<<counter<<endl; cout<<"Returning to Main Menu"<<endl; cout<<endl; return(main());} else;
if(input==number[1]){//edit value is selected cout<<"you want to edit a value"<<endl; cout<<"calling function now"<<endl; edit_values(the_array); } else; //print function is selected if(input==number[2]){ cout<<"Displaying your saved values has been selected"<<endl; cout<<"you entered a total of ("<<counter<<" )values"<<endl; print_display(the_array,student_id,lastname,firstname,day); } else; //sorting is selected if(input==number[3]){ cout<<"You have chosen to sort your values"<<endl; cout<<"you have entered "<<counter<<" values"<<endl; sort_values(the_array);}
if(input==number[4]){//exit from the program has been selected cout<<"You have chosen to exit the program"<<endl; cout<<"Thanks for your time!"<<endl; leave_program();}
if(input==number[5]){//reset the array has been selected cout<<"You have chosen to reset the array."<<endl; reset_array(the_array); }
if(input==number[6]){//search value has been chosen cout<<"You have chosen to search the array for a value."<<endl; cout<<"Your values will first be sorted in ascending order before the search begins."<<endl; float k; int element; search_value(the_array,k,track_value);
if(element==-1){ cout<<"The value was not found."<<endl;} else; cout<<"Found value in slot."<<element; }
if(input==number[7]){//sorting in ascending or descending order cout<<" You have chosen to sort your saved values."<<endl; stack_values(the_array);}
if(input==number[8]){//display some data within a range cout<<"You have chosen to display some data"<<endl; display_values(the_array);}
if(input==number[9]){ cout<<" You have chosen to input some personal student information."<<endl; enter_student(firstname,day,lastname,student_id); return(main());}}
void enter_student(char firstname[100],char day[100], char lastname[100], int student_id){
cout<<"Please enter your first name:"<<endl; cin>>firstname; cout<<"Please enter your last name :"<<endl; cin>>lastname; cout<<"Please enter your student I.D :"<<endl; cin>>student_id; cout<<"Please enter the current day of the week"<<endl; cin>>day; ID=student_id;}
float display_values(float the_array[array_size]){//displays a range, single or all stored numbers
int number; cout<<"How many numbers would you like to display"<<endl; cout<<" (1)- A single values"<<endl; cout<<" (2)- Multiple values"<<endl; cout<<" (3)- All stored values"<<endl; cin>>number; if((number>3)||(number<1)) while((number>3)||(number<1)){ cout<<"That was an invalid option. Please try again."<<endl; cin>>number; if((number>0)&&(number<4)){ cout<<"Input accepted, moving forward now."<<endl; break;}} if(number==1){//displays a single value int g; cout<<"Which memory slot would you like to display."<<endl; cin>>g; if((g<1)||(g>10)){ while((g<1)||(g>10)){ cout<<" Sorry that slot does not exist in the array."<<endl; cout<<"Please try again."<<endl; cin>>g; if((g>1)&&(g<11)){ break;}}} cout<<"("<<the_array[g]<<") is saved to memory slot("<<g<<")."<<endl; return(main()); }
if(number==2){//display multiple int h,g; cout<<"Please select a range of memory slot you would like to display."<<endl; cout<<"Please enter the first and final slots you would like to search between."<<endl; cout<<"Initial slot= "; cin>>h; cout<<"Final slot= "; cin>>g; if(h>g){ while(h>g){ cout<<"That range unavailable work pick a new one"<<endl; cout<<"Initial="; cin>>h; cout<<"Final="; cin>>g; if(h<g){ break;} }} else; if((h>counter)||(g>counter)){ while((h>counter)||(g>counter)){ cout<<" One or both of your inputs is outside the available range. Please try again."<<endl; cout<<"Initial= "; cin>>h; cout<<"Final= "; cin>>g; if((h<counter)&&(g<counter)){ cout<<" That is a valid range."<<endl; break;}}} cout<<"You have selected the range of slot initial to be ("<<h<<") and the range final to be ("<<g<<")."<<endl; cout<<"Displaying your values now."<<endl; for(h;h<=g;h++){ cout<<"Slot("<<h<<")="<< the_array[h]<<endl;} return(main());} else; if(number==3){ cout<<"Displaying all number stored in array."<<endl; for(int i=1;i<=counter;i++){ cout<<"Slot("<<i<<")="<<the_array[i]<<endl;} return(main());}}
float stack_values(float the_array[array_size]){//ascending or descending int g; cout<<"Would you like to sort your values? "<<endl; cout<<" (1)- Ascending order."<<endl; cout<<" (2)-Desending order."<<endl; cin>>g; if(g==1){ cout<<"Sorting your numbers in an ascending order."<<endl; cout<<"This will alter the original order you inputed the values in."<<endl;//sort ascending order( bubble sort) int j; int i; for(i=0;i<=counter;i++){ for(j=i+1;j<=counter;j++){ float l; if(the_array[i]>the_array[j]){ l=the_array[i]; the_array[i]=the_array[j]; the_array[j]=l;}}} for(i=1;i<=counter;i++){ cout<<the_array[i]<<endl;} return(main());} if(g==2){ cout<<"Sorting your values in a descending order."<<endl;//sort ascending order( bubble sort) int j; int i; for(i=1;i<=counter;i++){ for(j=i;j<=counter;j++){ float l; if(the_array[i]<the_array[j]){ l=the_array[i]; the_array[i]=the_array[j]; the_array[j]=l;}}} for(i=1;i<=counter;i++){ cout<<the_array[i]<<endl;} return(main()); }}
float search_value(float the_array[array_size],int k,int track_value){//search and display the slot of a value int j=1; int z; int h; int binary_num; for(h=0;h<=counter;h++){// sorts number in ascending order for(z=h+1;z<=counter;z++){ float l; if(the_array[h]>the_array[z]){ l=the_array[h]; the_array[h]=the_array[z]; the_array[z]=l;}}} if((k<-1000)||(k>1000)){// if invalid number is entered while((k<-1000)||(k>1000)){ cout<<"That number is invalid. Please try again."<<endl; cin>>k; if((k>-1000)&&(k<10000)){ cout<<"That number is acceptable."<<endl; break;}}} //Binary search int first = 1; int mid = 0; int entered_number; bool found_value = false; int g; int y = counter; int length = counter; int last = counter; cout<<"Please enter the value you wish to search."<<endl; cin>>entered_number; while (first<=last&&!found_value) {if(first==1) {for(int counter=length-1;counter>0;counter--){ for(int h=0;h<y;h++){ if (the_array[h]>the_array[h+1]){ int temp; temp=the_array[h+1]; the_array[h+1]=the_array[h]; the_array[h]=temp;}} y--;}} mid=(first+last)/2; if(entered_number>the_array[mid]) {first= mid+1;} else if(entered_number<the_array [mid]) {last=mid-1;} else {cout<<" the number ("<< entered_number <<") was found in array slot ["<< mid <<"]"<<endl; found_value = true; return(main());} } cout<<"That values does not exist in the current array."<<endl; cout<<"Returning you to Main Menu."<<endl; return(main()); }
float reset_array(float the_array[array_size])//reset the array { cout<<"Resetting the array to it's default state."<<endl; for(int k=1;k<=counter; k++){ the_array[k]=0; counter=1; reset=1; } cout<<"Returning you to Main Menu now."<<endl; return(main()); }
float sort_values(float the_array[array_size])//sorting function.
{ cout<<"You have chosen to display some simple statistics of your array."<<endl; cout<<endl; int position; int i; float largest_value=-10000; float smallest_value=10000; for(i=1;i<=counter;i++){ if(the_array[i]>largest_value){ largest_value=the_array[i]; }} for(i=1;i<=counter;i++){ if(the_array[i]<smallest_value){ smallest_value=the_array[i]; } } float sum=0; for(int i=0;i<=counter;i++){ sum+=the_array[i]; } cout<<"the sum of the array is "<<sum<<endl; cout<<endl; cout<<"the average value of the array is = "<<sum/counter<<endl; cout<<"your largest value is "<<largest_value<<endl; cout<<"your smallest value is "<<smallest_value<<endl; return(main()); }
float add_values(float the_array[array_size] ){ //function for storing numbers in array int h=reset;//controls loop float x=0;//wil be assigned into a array slot cout<<"You have chosen to add values"<<endl; cout<<"Please enter a valid value, or, enter 9999 to exit this function and return to Main Menu."<<endl; cout<<"This function will automatically end after 10 values have been entered."<<endl; for( h;h<=10;h++){ cin>>x; if((x<-999)&&(x!=9999)){ while(x<-999){ cout<<"That is an invalid number."<<endl; cin>>x; if((x>=-999)&&(x<=999)){ cout<<"That is a valid number"<<endl; break;}}} else; if((x>999)&&(x!=9999)){ while(x>999){ cout<<"That is an invalid number."<<endl; cin>>x; if((x>=-999)&&(x<=999)){ cout<<"That is a valid number."<<endl; break; }}} else; if(x==9999){//kick out of function back to menu cout<<"You have entered("<<counter<<") number of values."<<endl; cout<<"Returning to Main Menu."<<endl; h=counter; return(main());} else; if((x>=-999)&&(x<=999)){//if the number is good the_array[h]=x;// storing the number entered into the array slot counter=h; reset=counter+1;} else; if(h==10){ the_array[h]=x;// storing the number entered into the array slot counter=h; reset=counter+1; break;} cout<<"Please enter another values."<<endl;} cout<<"Thank you for entering 10 values."<<endl; h=counter;}
int leave_program(){ cout<<"Thank you for using this program."<<endl;}
float edit_values(float the_array[array_size]){//edit value
int i; cout<<"Please enter the slot you would like to edit: "; cin>>i; if((i>10)||(i<1)){// if the number you enter is bad while((i>10)||(i<1)){ cout<<"That memory slot does not exist, please try again."<<endl; cin>>i; if((i>0)&&(i<=10)){ cout<<"That is a better number"<<endl; break;}}} cout<<"The value in slot "<<i<<" is "<<the_array[i]<<"."<<endl; cout<<"What would you like the new saved value to be?"<<endl; cin>>the_array[i]; if((the_array[i]>999)||(the_array[i]<-999)){ while((the_array[i]>999)||(the_array[i]<-999)){ cout<<"That value exceeds the limits that the array accepts. Please try again."<<endl; cin>>the_array[i]; if((the_array[i]>-999)&&(the_array[i]<999)){ cout<<"That is a valid number."<<endl; break;} return(main());} cout<<"The new value for slot "<<i<<" is: "<<the_array[i]<<endl; cout<<"Returning you to Main Menu."<<endl; return(main());}}

float print_display(float the_array[array_size],int student_id,char lastname[100],char
firstname[100],char day[100]){//print function
cout<<"You have entered Display values."<<endl; int banana; int limit; int yes_no=0; int yes_no2=0; int yes_no3=0; cout<<" For the following questions please input 1 for yes or 2 for no"<<endl; cout<<" Would you like to display your name?"<<endl; cin>>yes_no; cout<< "Would you like to display your student I.D?"<<endl; cin>>yes_no2; cout<<" would you like to display the day of the week?"<<endl; cin>>yes_no3; if(yes_no==1){ cout<<"Name:["<<firstname<<" "<<lastname<<"] "<<endl;} else; if(yes_no2==1){ cout<<" ID:["<<ID<<"] "<<endl;} else; if(yes_no3==1){ cout<<"Day:["<<day<<"]"<<endl;} else; limit=counter; for(banana=1;banana<=limit;banana++){ cout<<"the array slot "<<banana<<"is equal="<<the_array[banana]<<endl; } return(main()); } The file badcode.cpp (remarkably) runs, although it does produce some erroneous results. It is a relatively basic first semester programming exercise where a console application is written to allow a user to enter their name, student id, day of the week a series of up to 10 integers. The user can then sort, search, display, edit and do statistical analysis (max, min, average) on the numbers. There are many structural and functional problems. You need to find and fix them, as well as explain your fix (mostly why it is a problem, but possibly how if it isn't obvious). Submit the fixed code along with additional comments as

Explanation / Answer

#include <iostream>

using namespace std;

#define array_size 11

float add_values(float );

// During the function call only the prototype with the data type of the variables inside the function should be mentioned.

float edit_values(float);

float the_array[array_size]; //Global variables declaration

int counter=1;     

char firstname[100];

char lastname[100];

char day[100];

int student_id;

int ID;

int track_value;

float print_display(float ,int ,char ,char,char);

// During the function call only the prototype with the data type of the variables inside the function should be mentioned.

float sort_values(float);

// During the function call only the prototype with the data type of the variables inside the function should be mentioned.

float reset_array(float);

// During the function call only the prototype with the data type of the variables inside the function should be mentioned.

float search_value(float,int,int);

float stack_values(float);

float display_values(float);

void enter_student(char,char,char,int);

int leave_program();

int reset=1;        //Global Variable Declaration

void main()

{

char number[]={'a','b','c','d','e','f','g','h','i','j','o'};

char input;

cout<<"Welcome! what do you want to do? Enter the letter you wish to perform"<<endl;

cout<<"(a): Add a value"<<endl;

cout<<"(b): Edit a value"<< endl;

cout<<"(c): Display values"<<endl;

cout<<"(d): Display statistics"<<endl;

cout<<"(e): Exit from program"<< endl;

cout<<"(f): Reset value entries"<<endl;

cout<<"(g): Search for stored value"<<endl;

cout<<"(h): Sort stored values"<<endl;

cout<<"(i): Display specified range"<<endl;

cout<<"(j): Input student information"<<endl;

cout<<"Enter the program number here: ";

cin>>input;

//should the user input an invalid option, a loop will begin asking for a proper value

if((input!='a')&&(input!='b')&&(input!='c')&&(input!='d')&&(input!='e')&&(input!='f')&&(input!='g')

&&(input!='h')&&(input!='i')&&(input!='j'))

{

while((input!='a')&&(input!='b')&&(input!='c')&&(input!='d')&&(input!='e')&&(input!='f')&&(input!=

'g')&&(input!='h')&&(input!='i')&&(input!='j'))

{

cout<<"that option is invalid, please try again";

cin>>input;

if((input=='a')||(input=='b')||(input=='c')||(input=='d')||(input=='e')||(input=='f')||(input=='g')||(input=='h')||(input=='i')||(input=='j'))

{

break;

}

}

}

if(input==number[0]){//add value is selected

{ //Opening braces should be added at the beginning of each condition

add_values(the_array);//function call

cout<<"the amount of numbers in the array is :"<<counter<<endl;

cout<<"Returning to Main Menu"<<endl;

cout<<endl;

return(main());

}

else if(input==number[1]) //else should not have semicolon at the end

{

//edit value is selected

cout<<"you want to edit a value"<<endl;

cout<<"calling function now"<<endl;

edit_values(the_array);

}

else if(input==number[2])

//else should not have semicolon at the end

//print function is selected

{

cout<<"Displaying your saved values has been selected"<<endl;

cout<<"you entered a total of ("<<counter<<" )values"<<endl;

print_display(the_array,student_id,lastname,firstname,day);

}

else if(input==number[3])

//else should not have semicolon at the end

//sorting is selected

{

cout<<"You have chosen to sort your values"<<endl;

cout<<"you have entered "<<counter<<" values"<<endl;

sort_values(the_array);

}

else if(input==number[4])

//else should be added always before the if statement to check for the successive conditional statement

{//exit from the program has been selected

cout<<"You have chosen to exit the program"<<endl;

cout<<"Thanks for your time!"<<endl;

leave_program();

}

else if(input==number[5])

//else should be added always before the if statement to check for the successive conditional statement

{//reset the array has been selected

cout<<"You have chosen to reset the array."<<endl;

reset_array(the_array);

}

else if(input==number[6])

//else should be added always before the if statement to check for the successive conditional statement

{

//search value has been chosen

cout<<"You have chosen to search the array for a value."<<endl;

cout<<"Your values will first be sorted in ascending order before the search begins."<<endl;

float k;

int element;

search_value(the_array,k,track_value);

if(element==-1)

{

cout<<"The value was not found."<<endl;

}

else;

cout<<"Found value in slot."<<element;

}

else if(input==number[7])

//else should be added always before the if statement to check for the successive conditional statement

{

//sorting in ascending or descending order

cout<<" You have chosen to sort your saved values."<<endl;

stack_values(the_array);

}

else if(input==number[8])

//else should be added always before the if statement to check for the successive conditional statement

{

//display some data within a range

cout<<"You have chosen to display some data"<<endl;

display_values(the_array);

}

else if(input==number[9])

{

cout<<" You have chosen to input some personal student information."<<endl;

enter_student(firstname,day,lastname,student_id);

return(main());

}

}

void enter_student(char firstname[100],char day[100], char lastname[100], int student_id)

{

cout<<"Please enter your first name:"<<endl;

cin>>firstname;

cout<<"Please enter your last name :"<<endl;

cin>>lastname;

cout<<"Please enter your student I.D :"<<endl;

cin>>student_id;

cout<<"Please enter the current day of the week"<<endl;

cin>>day;

ID=student_id;

}

float display_values(float the_array[array_size])

{//displays a range, single or all stored numbers

int number;

cout<<"How many numbers would you like to display"<<endl;

cout<<" (1)- A single values"<<endl;

cout<<" (2)- Multiple values"<<endl;

cout<<" (3)- All stored values"<<endl;

cin>>number;

if((number>3)||(number<1))

while((number>3)||(number<1))

{

cout<<"That was an invalid option. Please try again."<<endl;

cin>>number;

if((number>0)&&(number<4))

{

cout<<"Input accepted, moving forward now."<<endl;

break;

}

}

if(number==1)

{

//displays a single value

int g;

cout<<"Which memory slot would you like to display."<<endl;

cin>>g;

if((g<1)||(g>10))

{

while((g<1)||(g>10))

{

cout<<" Sorry that slot does not exist in the array."<<endl;

cout<<"Please try again."<<endl;

cin>>g;

if((g>1)&&(g<11))

{

break;

}

}

}

cout<<"("<<the_array[g]<<") is saved to memory slot("<<g<<")."<<endl;

return(main());

}

if(number==2)

{

//display multiple

int h,g;

cout<<"Please select a range of memory slot you would like to display."<<endl;

cout<<"Please enter the first and final slots you would like to search between."<<endl;

cout<<"Initial slot= ";

cin>>h;

cout<<"Final slot= ";

cin>>g;

if(h>g)

{

while(h>g)

{

cout<<"That range unavailable work pick a new one"<<endl;

cout<<"Initial=";

cin>>h;

cout<<"Final=";

cin>>g;

if(h<g)

{

break;

}

}

}

else;

if((h>counter)||(g>counter)){

while((h>counter)||(g>counter)){

cout<<" One or both of your inputs is outside the available range. Please try again."<<endl;

cout<<"Initial= ";

cin>>h;

cout<<"Final= ";

cin>>g;

if((h<counter)&&(g<counter)){

cout<<" That is a valid range."<<endl;

break;}}}

cout<<"You have selected the range of slot initial to be ("<<h<<") and the range final to be ("<<g<<")."<<endl;

cout<<"Displaying your values now."<<endl;

for(h=0;h<=g;h++) //Always the value to be initialised within the for statement

{

cout<<"Slot("<<h<<")="<< the_array[h]<<endl;}

return(main());

}

else;

if(number==3)

{

cout<<"Displaying all number stored in array."<<endl;

for(int i=1;i<=counter;i++){

cout<<"Slot("<<i<<")="<<the_array[i]<<endl;

}

return(main());}}

float stack_values(float the_array[array_size]){//ascending or descending

int g;

cout<<"Would you like to sort your values? "<<endl;

cout<<" (1)- Ascending order."<<endl;

cout<<" (2)-Desending order."<<endl;

cin>>g;

if(g==1){

cout<<"Sorting your numbers in an ascending order."<<endl;

cout<<"This will alter the original order you inputed the values in."<<endl;//sort ascending order( bubble sort)

int j;

int i;

for(i=0;i<=counter;i++){

for(j=i+1;j<=counter;j++){

float l;

if(the_array[i]>the_array[j]){

l=the_array[i];

the_array[i]=the_array[j];

the_array[j]=l;}}}

for(i=1;i<=counter;i++){

cout<<the_array[i]<<endl;}

return(main());}

if(g==2){

cout<<"Sorting your values in a descending order."<<endl;//sort ascending order( bubble sort)

int j;

int i;

for(i=1;i<=counter;i++){

for(j=i;j<=counter;j++){

float l;

if(the_array[i]<the_array[j]){

l=the_array[i];

the_array[i]=the_array[j];

the_array[j]=l;}}}

for(i=1;i<=counter;i++){

cout<<the_array[i]<<endl;}

return(main());

}}

float search_value(float the_array[array_size],int k,int track_value){//search and display the slot of a value

int j=1;

int z;

int h;

int binary_num;

for(h=0;h<=counter;h++){// sorts number in ascending order

for(z=h+1;z<=counter;z++){

float l;

if(the_array[h]>the_array[z]){

l=the_array[h];

the_array[h]=the_array[z];

the_array[z]=l;}}}

if((k<-1000)||(k>1000)){// if invalid number is entered

while((k<-1000)||(k>1000)){

cout<<"That number is invalid. Please try again."<<endl;

cin>>k;

if((k>-1000)&&(k<10000)){

cout<<"That number is acceptable."<<endl;

break;}}}

//Binary search

int first = 1;

int mid = 0;

int entered_number;

bool found_value = false;

int g;

int y = counter;

int length = counter;

int last = counter;

cout<<"Please enter the value you wish to search."<<endl;

cin>>entered_number;

while (first<=last&&!found_value)

{if(first==1)

{for(int counter=length-1;counter>0;counter--){

for(int h=0;h<y;h++){

if (the_array[h]>the_array[h+1]){

int temp;

temp=the_array[h+1];

the_array[h+1]=the_array[h];

the_array[h]=temp;}}

y--;}}

mid=(first+last)/2;

if(entered_number>the_array[mid])

{first= mid+1;}

else if(entered_number<the_array [mid])

{last=mid-1;}

else

{cout<<" the number ("<< entered_number <<") was found in array slot ["<< mid <<"]"<<endl;

found_value = true;

return(main());}

}

cout<<"That values does not exist in the current array."<<endl;

cout<<"Returning you to Main Menu."<<endl;

return(main());

}

float reset_array(float the_array[array_size])//reset the array

{

cout<<"Resetting the array to it's default state."<<endl;

for(int k=1;k<=counter; k++){

the_array[k]=0;

counter=1;

reset=1;

}

cout<<"Returning you to Main Menu now."<<endl;

return(main());

}

float sort_values(float the_array[array_size])//sorting function.

{

cout<<"You have chosen to display some simple statistics of your array."<<endl;

cout<<endl;

int position;

int i;

float largest_value=-10000;

float smallest_value=10000;

for(i=1;i<=counter;i++){

if(the_array[i]>largest_value){

largest_value=the_array[i];

}}

for(i=1;i<=counter;i++){

if(the_array[i]<smallest_value){

smallest_value=the_array[i];

}

}

float sum=0;

for(int i=0;i<=counter;i++){

sum+=the_array[i];

}

cout<<"the sum of the array is "<<sum<<endl;

cout<<endl;

cout<<"the average value of the array is = "<<sum/counter<<endl;

cout<<"your largest value is "<<largest_value<<endl;

cout<<"your smallest value is "<<smallest_value<<endl;

return(main());

}

float add_values(float the_array[array_size] ){ //function for storing numbers in array

int h=reset;//controls loop

float x=0;//wil be assigned into a array slot

cout<<"You have chosen to add values"<<endl;

cout<<"Please enter a valid value, or, enter 9999 to exit this function and return to Main Menu."<<endl;

cout<<"This function will automatically end after 10 values have been entered."<<endl;

for( h;h<=10;h++){

cin>>x;

if((x<-999)&&(x!=9999)){

while(x<-999){

cout<<"That is an invalid number."<<endl;

cin>>x;

if((x>=-999)&&(x<=999)){

cout<<"That is a valid number"<<endl;

break;}}}

else;

if((x>999)&&(x!=9999)){

while(x>999){

cout<<"That is an invalid number."<<endl;

cin>>x;

if((x>=-999)&&(x<=999)){

cout<<"That is a valid number."<<endl;

break;

}}}

else;

if(x==9999){//kick out of function back to menu

cout<<"You have entered("<<counter<<") number of values."<<endl;

cout<<"Returning to Main Menu."<<endl;

h=counter;

return(main());}

else;

if((x>=-999)&&(x<=999)){//if the number is good

the_array[h]=x;// storing the number entered into the array slot

counter=h;

reset=counter+1;}

else;

if(h==10){

the_array[h]=x;// storing the number entered into the array slot

counter=h;

reset=counter+1;

break;}

cout<<"Please enter another values."<<endl;}

cout<<"Thank you for entering 10 values."<<endl;

h=counter;}

int leave_program(){

cout<<"Thank you for using this program."<<endl;}

float edit_values(float the_array[array_size]){//edit value

int i;

cout<<"Please enter the slot you would like to edit: ";

cin>>i;

if((i>10)||(i<1)){// if the number you enter is bad

while((i>10)||(i<1)){

cout<<"That memory slot does not exist, please try again."<<endl;

cin>>i;

if((i>0)&&(i<=10)){

cout<<"That is a better number"<<endl;

break;}}}

cout<<"The value in slot "<<i<<" is "<<the_array[i]<<"."<<endl;

cout<<"What would you like the new saved value to be?"<<endl;

cin>>the_array[i];

if((the_array[i]>999)||(the_array[i]<-999)){

while((the_array[i]>999)||(the_array[i]<-999)){

cout<<"That value exceeds the limits that the array accepts. Please try again."<<endl;

cin>>the_array[i];

if((the_array[i]>-999)&&(the_array[i]<999)){

cout<<"That is a valid number."<<endl;

break;}

return(main());}

cout<<"The new value for slot "<<i<<" is: "<<the_array[i]<<endl;

cout<<"Returning you to Main Menu."<<endl;

return(main());}}

float print_display(float the_array[array_size],int student_id,char lastname[100],char

firstname[100],char day[100]){//print function

cout<<"You have entered Display values."<<endl;

int banana;

int limit;

int yes_no=0;

int yes_no2=0;

int yes_no3=0;

cout<<" For the following questions please input 1 for yes or 2 for no"<<endl;

cout<<" Would you like to display your name?"<<endl;

cin>>yes_no;

cout<< "Would you like to display your student I.D?"<<endl;

cin>>yes_no2;

cout<<" would you like to display the day of the week?"<<endl;

cin>>yes_no3;

if(yes_no==1){

cout<<"Name:["<<firstname<<" "<<lastname<<"] "<<endl;}

else;

if(yes_no2==1){

cout<<" ID:["<<ID<<"] "<<endl;}

else;

if(yes_no3==1){

cout<<"Day:["<<day<<"]"<<endl;}

else;

limit=counter;

for(banana=1;banana<=limit;banana++){

cout<<"the array slot "<<banana<<"is equal="<<the_array[banana]<<endl;

}

return(main());

}