Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //---------------------------------------------------
- // Purpose: A simple menu-based program for processing
- // an array of integers in many different ways.
- // Each array operation is implemented in a
- // separate function and called in the main program.
- //
- // Author: Heather R
- //---------------------------------------------------
- #include <cstdlib>
- #include <iostream>
- #include <cmath>
- using namespace std;
- //function prototypes
- void randomize(int array[], const int size, const int range);
- void print(int array[], const int size);
- int get_command();
- int find_min(int data[], const int size);
- int find_max(int data[], const int size);
- int num_prime(int data[], const int size);
- int find_sum(int data[], const int size);
- int find_sum_squared(int data[], const int size);
- int num_even(int data[], const int size);
- int num_odd(int data[], const int size);
- int search(int data[], const int size);
- bool is_sorted(int data[], const int size);
- int sort(int data[], const int size);
- bool is_sorted(int data[], const int size)
- {
- // check if array is in sorted order
- bool sorted = true;
- for (int i=1; i<size; i++)
- {
- if (data[i-1] > data[i])
- sorted = false;
- }
- return sorted;
- }
- int find_sum_squared(int data[], const int size)
- {
- int squared_sum = 0;
- for (int index = 0; index <size; index++)
- {
- squared_sum += ( data[index]* data[index]);
- }
- return squared_sum;
- }
- //------------------------------------
- int num_even(int data[], const int size)
- {
- int num_even = 0;
- for (int index = 0; index < size; index++)
- {
- if (data[index] % 2 == 0)
- num_even +=1;
- }
- return num_even;
- }
- //------------------------------------
- int find_max(int data[], const int size)
- {
- int location = 0;
- // Find max value
- int max = data[0];
- for (int index = 0; index < size; index++)
- {
- if (data[index] > max)
- {max = data[index];
- location = index;}
- }
- return location;
- }
- //---------------------------------------------------
- int find_min(int data[], const int size)
- {
- int location = 0;
- int min = data[0];
- for (int index = 0; index < size; index++)
- {
- if (data[index] < min)
- {min = data[index];
- location = index;}
- }
- return location;
- }
- //---------------------------------------------------
- int num_prime(int data[], const int size)
- {
- //Declare variable for the amount of values that are not prime
- int not_prime = 0;
- int Num_Prime = 0;
- bool Prime = true;
- for (int index = 0; index < size; index++)
- {
- if (data[index] == 1)
- {Prime = false;
- not_prime++;}
- if ((data[index] > 2) && (data[index] % 2 == 0))
- {Prime = false;
- not_prime++;}
- if ((data[index] > 3) && (data[index] % 3 == 0))
- {Prime = false;
- not_prime++;}
- if ((data[index] > 5) && (data[index] % 5 == 0))
- {Prime = false;
- not_prime++;}
- if ((data[index] > 7) && (data[index] % 7 == 0))
- {Prime = false;
- not_prime++;}
- Num_Prime = (size - not_prime);
- }
- return Num_Prime;
- }
- //---------------------------------------------------
- int Search(int data[], int size)
- {
- int i = 0;
- int location = 0;
- int desired = 0;
- cout << "Enter the value you'd like to check ";
- cin >>desired;
- // Search array using divide and conquer
- bool found = false;
- for (int i=0; i<size; i++)
- {
- if (data[i] == desired)
- {found = true;
- location = i;
- break;}
- else
- i= -1;
- }
- return i;
- }
- //---------------------------------------------------
- int sort(int data[], const int size)
- {
- int Index, Pos, SmallPos, Val, SmallVal;
- for (Index = 0; Index < size; Index++)
- {
- // Find smallest value in unsorted part
- SmallPos = Index;
- for (Pos = Index; Pos < size; Pos++)
- if (data[Pos] < data[SmallPos])
- SmallPos = Pos;
- // Swap value to end of sorted part
- SmallVal = data[SmallPos];
- data[SmallPos] = data[Index];
- data[Index] = SmallVal;
- cout << data[Index] << endl;
- }
- }
- //---------------------------------------------------
- void randomize(int array[], const int size, const int range)
- {
- for (int index = 0; index < size; index++)
- array[index] = random() % range;
- }
- //---------------------------------------------------
- void print(int array[], const int size)
- {
- for (int index = 0; index < size; index++)
- cout << array[index] << " ";
- cout << endl;
- }
- int find_sum(int array[], const int size)
- {
- int sum = 0;
- for (int index = 0; index <size; index++)
- { sum += array[index];
- }
- return sum;
- }
- //---------------------------------------------------
- int get_command()
- {
- cout << "\nWelcome to Array Processing Program (APP 1.0).\n";
- cout << " Your command options are:\n";
- cout << " 0 - quit the program.\n";
- cout << " 1 - print the array.\n";
- cout << " 2 - randomize the array.\n";
- cout << " 3 - find the location of minimum value in array.\n";
- cout << " 4 - find the location of maximum value in array.\n";
- cout << " 5 - find sum of all values in array.\n";
- cout << " 6 - find sum of squares of all values in array.\n";
- cout << " 7 - count number of even numbers in array.\n";
- cout << " 8 - count number of odd numbers in array.\n";
- cout << " 9 - count number of prime numbers in array.\n";
- cout << " 10 - search the array for a specified value.\n";
- cout << " 11 - check to see if the array is sorted in ascending order.\n";
- cout << " 12 - sort the array in ascending order using selection sort.\n";
- cout << "\nEnter command: ";
- int command = 0;
- cin >> command;
- return command;
- }
- //---------------------------------------------------
- int main()
- {
- // Declare local variables
- const int DATA_SIZE = 20;
- int data[DATA_SIZE] = {0};
- int range = 0;
- int command = -1;
- int min = 0;
- int max = 0;
- int sum = 0;
- int sum2 = 0;
- int count = 0;
- int location = -1;
- bool sorted = false;
- // Loop processing user commands
- while (command != 0)
- {
- command = get_command();
- switch (command)
- {
- case 0:
- cout << "Have a nice day.\n";
- break;
- case 1:
- print(data, DATA_SIZE);
- break;
- case 2:
- cout << "Enter desired range: ";
- cin >> range;
- randomize(data, DATA_SIZE, range);
- cout << "Array has been randomized.\n";
- break;
- case 3:
- min = find_min(data, DATA_SIZE);
- cout << "The location of the Minimum value = " << min << endl;
- break;
- case 4:
- max = find_max(data, DATA_SIZE);
- cout << "The location of the Maximum value = " << max << endl;
- break;
- case 5:
- sum = find_sum(data, DATA_SIZE);
- cout << "Sum of values = " << sum << endl;
- break;
- case 6:
- sum2 = find_sum_squared(data, DATA_SIZE);
- cout << "Sum of values squared = " << sum2 << endl;
- break;
- case 7:
- count = num_even(data, DATA_SIZE);
- cout << "Count of even numbers = " << count << endl;
- break;
- case 8:
- count = DATA_SIZE - num_even(data, DATA_SIZE);
- cout << "Count of odd numbers = " << count << endl;
- break;
- case 9:
- count = num_prime(data, DATA_SIZE);
- cout << "Count of prime numbers = " << count << endl;
- break;
- case 10:
- location = Search(data, DATA_SIZE);
- if (location == -1)
- cout << "The value was not found in the array.\n";
- else
- cout << "The value was found at location " << location << ".\n";
- break;
- case 11:
- sorted = is_sorted(data, DATA_SIZE);
- if (sorted)
- cout << "The array IS in sorted order.\n";
- else
- cout << "The array is NOT in sorted order.\n";
- break;
- case 12:
- sort(data, DATA_SIZE);
- cout << "Array has been sorted.\n";
- break;
- default:
- cout << "Error, invalid command.\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement