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

c++ Objectives: Use an array Use a menu to call various functions Use multiple s

ID: 3703659 • Letter: C

Question

c++

Objectives:

Use an array

Use a menu to call various functions

Use multiple sorts and searches

Instructions:

Code a program with methods to search and sort an array, along with a print menu method and a dispatch method.  Also set up a 100 position integer array and an integer variable, called size, globally (before all methods are coded). Then the array and size does not have to be passed to any of the methods. (The alternative approach would be to define the array in the dispatch method after reading in a size. Then array name and size would be passed to all methods.) The menu should look like the following:

0. Exit

Get the size needed for today’s use of the array.

Fill an array with random numbers from 1-100. Do not use “srand” command in this lab so all the arrays have the same random, generated numbers.

Print the array with position numbers.

Sort the array in ascending sequence

Sort the array in descending sequence – use a different algorithm from step 4.

Sequential search of the array for a target – target should be passed to method and the method returns the found location or -1.

Binary search of the array for a target – target should be passed to method and the method returns the found location or -1.

Run:

The program should start with a request for a size from the user. (Type in 15.) Run the lab in this order:

Ask the user for the size of the array (1)

Fill the array with random values (2)

Print the array (3) (List the position number and the contents of the array position)

Sequentially search the array for a number in the array and then one not in the array, printing the appropriate messages of “Found in position ____” and “Not Found” (6, 6)

Sort the array into ascending sequence (4)

Print the array (3)

Do a binary search of the array for a number in the array and then for a value not in the array, printing the appropriate messages of “Found in position ____” and “Not Found” (7, 7)

Sort the array into descending sequence (5) (Use a different sort algorithm from code 4)

Finally, print the array again (3)

The main method will look like this: (everything else will be done in other methods)

int main ()

{

printMenu();

cout << “Type in a choice “ << endl;

cin >> choice;

while (choice != 0)

{

dispatch (choice); // one big switch statement

printMenu();

cout << “Type in a choice “ << endl;

cin >> choice;

}

return 0;

}

For the two search methods (choice 6 and 7), the program must ask for the target and then read in a target before calling the search method. After calling the search method, the program must determine if the search returned an integer (print “Found in position ______”) or the search returned a -1 (print “Not Found”).

Explanation / Answer

#include<iostream>
#include<cstdlib>

void printMenu();
void sizeofarray();
void fillrandom();
void print();
int binsearch(int);
int seqsearch(int);
void sortdesc();
void sortasc();


using namespace std;
int array[100], size;

int main ()
{
int choice, result;

printMenu();

while(choice!=-1)
{
cout <<" Type in a choice or -1 to stop : ";
cin >> choice;
while ((choice <1 || choice >7) && choice !=-1)
{
cout<<"Enter a choice between 1, 7 or -1 to exit : ";
cin>>choice;
}

if(choice == -1)
{cout<<"goodbye ";}
if(choice == 1)
{
sizeofarray();
}
else if(choice == 2)
{
fillrandom();
}
else if(choice == 3)
{
print();
}
else if(choice == 4)
{
sortasc();
}
else if(choice == 5)
{
sortdesc();
}
else if(choice == 6)
{
cout<<"Enter the number to be searched using sequential search : ";
cin>>result;
result=seqsearch(result);
if(result == -1)
{cout<<"Not found ";}
else
{cout<<"Found in position "<<result<<" ";}
}
else if(choice == 7)
{
cout<<"Enter the number to be searched using binary search : ";
cin>>result;
result=binsearch(result);
if(result == -1)
{cout<<"Not found ";}
else
{cout<<"Found in position "<<result<<" ";}
}

}

return 0;
}

void sortasc()
{
int i,j,temp;

for(j=0;j<size;j++)
{
for(i=0;i<size-1;i++)
{
if(array[i]>array[i+1])
{
temp=array[i];
array[i]=array[i+1];
array[i+1]=temp;
}
}
}
cout<<"Sorted array in ascending order ";
}

void sortdesc()
{
int b[100],i,j,max,k;

for(i=0;i<size;i++)
{b[i]=array[i];}

for(i=0;i<size;i++)
{
max=b[0];k=0;
for(j=0;j<size;j++)
{
if(b[j]>max)
{
max=b[j];
k=j;
}
}
array[i]=max;
b[k]=-1;
}
cout<<"Sorted array in descending order ";
}

int seqsearch(int n)
{
int i;

for(i=0;i<size;i++)
{
if(array[i] == n)
{return i;}
}

if(i == size)
{return -1;}
}

int binsearch(int n)
{
int first=0, last=size-1, middle;

while(first <= last)
{
middle = (first+last)/2;

if ( array[middle] < n )
{first = middle + 1;}
else if ( array[middle] == n )
{return middle;}
else
{last = middle - 1;}
}

return -1;
}

void print()
{
int i;

cout<<"Content of the array are: ";
cout<<"Index Number ";

for(i=0;i<size;i++)
{cout<<i<<" "<<array[i]<<" ";}
cout<<" ";
}

void fillrandom()
{
int i;

for(i=0;i<size;i++)
{
array[i]=rand()%101;
while(array[i]==0)
{array[i]=rand()%101;}
}
cout<<"Filled array with random numbers ";
}


void sizeofarray()
{
cout<<"Enter size of the array (between 1 and 100): ";
cin>>size;

while(size<1 || size>99)
{
cout<<"Error size exceeded 100 or is below 1 ";
cout<<"Enter size of the array (between 1 and 100): ";
cin>>size;
}
}

void printMenu()
{
cout<<"Operations ";
cout<<"1.Get the size of the array(max 100). ";
cout<<"2.Fill an array with random numbers from 1-100. ";
cout<<"3.Print the array with position numbers. ";
cout<<"4.Sort the array in ascending sequence - Bubble Sort ";
cout<<"5.Sort the array in descending sequence - Selection Sort. ";
cout<<"6.Sequential search of the array for a target. ";
cout<<"7.Binary search of the array for a target. ";
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote