Code:
// Edit the code in this file so that it does the following:
//
// In the main function:
// + Display the menu.
// + Input a character from the user representing a menu choice.
// + Use a switch statement to selectively execute code satisfying the
// + user's choice.
// + Give the user a chance to enter another choice.
// + If the user enters '0', 'q' or 'Q' for a choice the program should
// + exit.
//
// *NOTE:
// -- Code that needs to be added or corrected is in RED
// -- Call all functions using arguments indicated by the function
// prototypes and definitions. Do not place code in main() that
// should be in another function definition.
// This section of the handout serves to illustrate certain aspects of
// C++. You should study this section. Many of the functions contain
// code that is not correct (dummy code). This code should be
// corrected. Compile and run the code. Some of what you learn in this
// section will apply to the assignment, which is described at the end
// of this handout. You will use the functions developed in this
// section in your assignment. This section of the handout will serve
// as the basis of your assignment. You may copy this section to your
// assignment source file.
// Topics covered:
// reference parameters
// passing array to function as a pointer
// pointer variables
// call by reference with pointer arguments
// call by reference with reference arguments
// switch statement
// character mode menu
// dynamic memory allocation for array
// pointer arithmetic
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
// For function prototypes we only need specify the data types to be
// passed to the functions. If any parameter names are mentioned, they
// are ignored by the compiler.
void fill(int ax[], int nx);
void display(int* pax, int nx);
void max_min (int* pax, int nx, int& maxx, int& minx);
long sort(int* pax, int nx);
long sum(int* pax, int nx);
long search(int ax[], int nx, int snumx, int& posx);
void menu();
float avg(int* , int nx); // only included data type for the first item
int div3(int* , int nx);
int mode (int* , int ); // only data types for both items
int main(void)
{
int const n=25;
int* a = new int[n]; // Dynamically allocate storage to array at run time.
srand(time(0)); // Randomizer uses current system time for randomize seed.
char choice; // Declaration of choice variable.
int snum, pos = -1;
do
{
// char choice; // why not declare choice here?
menu(); // display menu
cin >> choice; // get menu choice from user
switch(choice)
{
case '1':
fill(a,n);
break;
case '2':
cout <<'\n'<< "Displaying array a..." << endl;
display(a,n);
break;
case '3':
cout <<'\n'<< "The number of comparisons needed for "
<< "sorting the array is.. " << sort(a,n) << endl;
// sort(a,n) returns the number of comparisons
// needed to sort a[0]..a[n-1]
break;
case '4':
int max, min;
max_min(a, n, max, min);
cout <<'\n'<< "The max number is.. "<< max;
cout <<'\n'<< "The mimumum number is.. "<< min << endl;
break;
case '5':
// ask user for search number
cout << endl << "Please enter a search integer: ";
cin >> snum; // store search integer in snum
search(a,n,snum, pos); // return value of search should be stored
// for later use.
if (pos == -1)
{
// Tell the user snum was not found.
// Indicate the number of comparisons needed
// to determine that snum was NOT in the array a[0]..a[n-1].
// This number (the # of comparisons) is the
// return value from the search function (see implementation).
}
else
{
// snum was found in the array a[0]..a[n-1].
// Tell the user the number of comparisons needed
// and the position in the array (pos) where snum was found.
// i.e. a[pos] = snum
}
break;
case '6':
cout << "\n The sum is.. "
<< sum(a,n) << endl;
break;
case '7':
// call divisible by three algorithm
cout << "\n There are "
<< div3(a,n) << " numbers in the array"
<< " divisible by 3." << endl;
break;
case '8':
// call your code for avg
cout << "\n The AVERAGE of the.. " << n << " numbers is.. "
<< avg(a,n) << endl;
break;
case '9':
// call your code for mode
cout << "\n The MODE of the.. " << n << " numbers is.. "
<< mode(a,n) << endl;
break;
case '0': case 'Q': case 'q': // all three quit
cout <<'\n'<< "Thanks...."<< endl << endl << "goodbye." << endl;
break;
default:
cout <<'\n'<< "Wrong Key" << endl;
break;
} // end of switch
} while (choice != '0'|| choice!='Q' || choice!='q');
// ERROR IN LOGIC, BEWARE !!!!!!!
// toupper() or tolower() may be used to make the condition a
// little simpler. These functions may be found in ctype.h
return 0;
} // End of main()
void fill(int ax[], int nx)
{
for (int j=0; j<nx; j++)
{
ax[j]=1+ rand()%10; // generates random numbers between 1 and 10
}
}
void display(int* pax, int nx)
{
int r, j;
for (j=0; j<nx; j++)
{
cout << setw(7) << *(pax+j); // pointer arithmetic
// What will the next two statements do?
r = (j+1) % 10; // zero when (j+1) is multiple of ten
if (!r)
cout << endl; // if r==0 then
// !r is not zero (i.e. it's true)
}
cout << endl;
}
long sum(int* pax, int nx)
{
long s = *pax; // Initialize s to the first element of the array
s += *(pax+1); // Add to s *(pax+2); the second element
// Correct this function. Write the code to sum
// the number of integers (nx) in the array pointed
// to by pax. USE POINTER ARITHMETIC TO ADDRESS THE
// ARRAY.
return s;
}
long sort(int bx[], int nx)
{
long cnt=0;
// Code goes here for sorting the array.
// cnt should keep track of the number of
// comparisons necessary for sorting.
return cnt; // number of comparisons
}
void menu()
{
cout << endl;
cout << " 1. Fill array" << endl;
cout << " 2. Display array"<< endl;;
cout << " 3. Sort array" << endl;
cout << " 4. Largest and smallest" <<endl;
cout << " 5. Search routine" << endl;
cout << " 6. Sum the array" << endl;
cout << " 7. Integers divisible by 3" << endl;
cout << " 8. Average" << endl;
cout << " 9. Mode value" << endl;
cout << " 0. Quit" << endl;
cout << " Choice ----> ";
}
void max_min (int* ax, int nx, int& maxx, int& minx)
{
// Return the max (maxx) and min (minx) of the array as references.
}
long search(int* ax, int nx, int snumx, int& posx)
{
long cmp=0;
// Search for snumx in the array ax of size nx.
// Return the number of comparisons needed to find or not find
// the search integer through the function name. If you find snumx
// in the array, then store the value of the position in the reference
// variable posx.
//
// EX:
// If ax points to { 12 4 55 6 } and you were searching for 4
// then posx should be assigned the value 1 since 4
// is in the 1st position in the array (12 is in the 0th position)
return cmp; // number of comparisons
}
float avg(int* ax, int nx)
{
float f;
// Compute the average of the numbers in the array.
// CALL THE SUM FUNCTION THEN DIVIDE by NX (the size of the array).
return f;
}
int div3(int* ax, int nx)
{
int d3 = 0;
// Use the modulus operator ( % ) to determine
// how many integers there are in the array that are
// divisible by three.
// EX:
// If ax points to { 1 2 4 5 5 6 } then there is 1 integer divisible
// by three in the array.
return d3; // d3 is assigned the value 0. This is wrong.
}
int mode(int* ax, int nx)
{
int most = 0;
// Determine which number in the array appears most often (this is the mode).
// In case of ties return any of the mode values.
// EX:
// if ax = { 1 4 5 5 } then the mode is 5 (return 5)
// if ax = { 1 1 5 5 } then the mode is 5 and 1 (bimodal) (return 5 or 1)
return most;
}