Question

In: Computer Science

Write a program which: Prompts the user for a positive integer >= 0 Validates the user...

Write a program which:

Prompts the user for a positive integer >= 0

Validates the user input to ensure it is a positive integer >= 0

Allocate (dynamically) an array big enough for the data.

Load the array with random numbers ranging in value from1 to 100

Display the elements of the array (unsorted)

Display the elements of the array (sorted)

Display the average

Display the median

Display the mode, if none, display appropriate message

RESTRICTIONS

No global variables

No infinite loops examples.

for(;;)

while(1)

while(true)

do{//code}while(1);

No break statements to exit loops

program must contains all the functions below

#include
#include /* srand, rand */
#include /* time */
using namespace std;

Must use all the functions below

// Function prototypes
double median(int *, int);
int mode(int *, int);
int *makeArray(int);
void loadNumberData(int *, int);
void selectionSort(int [], int);
double average(int *, int);
void displayArray(int * numberData, int qtyOfRandomNumbers);
void validateInt(string userIn, int& userInput);

int main()
{
  
}

//function definitions

//*************************************************
//function displayArray
//this function displays the elements of the array
//use pointer arithmetic to step through the array

//*************************************************
//function validateInt ensures that the user input

//deny non numeric input (ex. space, x, !,@,^)
//is an integer >= 0

//*************************************************
// Function makeArray *
// This function dynamically allocates an array of*
// ints and returns a pointer to it. The parameter*
// size is the number of elements to allocate. *
//*************************************************

//*************************************************
// Function loadNumberData *
// This function loads the array with random numbers*
//ranging in value from 1 to 100
//use pointer arithmetic to step through the array *
//*************************************************


//*************************************************
// Function selectionSort *
// This function performs the selection sort *
// algorithm on array, sorting it into ascending *
// order. The parameter size holds the number of *
// elements in the array. *
//*************************************************

//**************************************************
// Function median *
// This function displays the median of the values *
// in the array pointed to by the parameter arr. *
// The num parameter holds the number of elements *
// in the array. *
//**************************************************

//*********************************************************
// Function mode *
// This function returns the mode of the array pointed to *
// by arr. The mode is the value that appears most often. *
// The parameter num holds the number of elements in the *
// array. If no element appears more than once, the *
// function returns -1. *
//*********************************************************

//**************************************************
// Function average *
// This function calculates and returns the average*
// of the values in the array arr. num is the *
// number of elements in the array. *
//**************************************************

Solutions

Expert Solution

#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <cmath>
using namespace std;

// Function Declarations
double median(int *, int);
int mode(int *, int);
int *makeArray(int);
void loadNumberData(int *, int);
void selectionSort(int[], int);
double average(int *, int);
void displayArray(int * numberData, int qtyOfRandomNumbers);
int validateInt(int &userInput);


//main function
int main()
{
   //Declaring variable
int userInput;

first:
//Getting the number entered by the user
cout << "How many numbers do you want to generate? ";
cin >> userInput;
  
if (cin.fail())
{
cin.clear();
cin.ignore();
cout << "\nInvalid.Number must be >=0, Try again\n";
goto first;
}
  
if (validateInt(userInput) == 0)
{
int *array = makeArray(userInput);
loadNumberData(array, userInput);
cout <<"\nBefore Sorting the elements in the array are :\n";
displayArray(array, userInput);
selectionSort(array, userInput);
cout << "\n\nAfter Sorting the elements in the array are :\n";
displayArray(array, userInput);
cout << "\n\nThe Average is : " << average(array, userInput);
cout << "\n\nthe Median of the numbers is : " << median(array, userInput);
int modeValue = mode(array, userInput);
if (modeValue == 0)
cout << "\nThe set has no mode.\n" << endl;
else
cout << "\nArray Mode is : " << modeValue<<endl;
}
else
{
cout << "\nthat is not an integer >=0, try again\n";
goto first;
}
system("pause");
return 0;
}
//function definitions
//*************************************************
//function displayArray
//this function displays the elements of the array
//use pointer arithmetic to step through the array
//*************************************************
void displayArray(int * numberData, int qtyOfRandomNumbers)
{
for (int i = 0; i<qtyOfRandomNumbers; i++)
{
cout << " " << *(numberData + i);
}
}
//*************************************************
//function validateInt ensures that the user input
//is an integer >= 0
//*************************************************
int validateInt(int &userInput)
{
if (userInput >= 0)
return 0;
if(userInput < 0)
return -1;
}
//*************************************************
// Function makeArray
// This function dynamically allocates an array of*
// ints and returns a pointer to it. The parameter*
// size is the number of elements to allocate. *
//*************************************************
int *makeArray(int size)
{
int *arr = new int[size];
return arr;
}
//*************************************************
// Function loadNumberData
// This function loads the array with random numbers*
//ranging in value from 1 to 100
//use pointer arithmetic to step through the array *
//*************************************************
void loadNumberData(int *arr, int size)
{
srand(time(NULL)); /* initialize random seed: */
for (int i = 0; i<size; i++)
{
*(arr + i) = rand() % 100 + 1; /* generate random number between 0 and 100: */
}
}
//*************************************************
// Function selectionSort
// This function performs the selection sort *
// algorithm on array, sorting it into ascending *
// order. The parameter size holds the number of *
// elements in the array.
//*************************************************
void selectionSort(int arr[], int size)
{
int pos_min;//pos_min is short for position of min
for (int i = 0; i < size - 1; i++)
{
pos_min = i;//set pos_min to the current index of array
for (int j = i + 1; j < size; j++)
{
if (arr[j] < arr[pos_min])
pos_min = j;
//pos_min will keep track of the index that min is in, this is needed when a swap happens
}
//if pos_min no longer equals i than a smaller value must have been found, so a swap must occur
if (pos_min != i)
{
int temp = arr[i];
arr[i] = arr[pos_min];
arr[pos_min] = temp;
}
}
}
//**************************************************
// Function median *
// This function displays the median of the values *
// in the array pointed to by the parameter arr. *
// The num parameter holds the number of elements *
// in the array. *
//**************************************************
double median(int *arr, int size)
{
int middle;
double med;
middle = floor(size / 2.0);
if (size % 2 == 0)
med = (*(arr + middle - 1) + *(arr + middle)) / 2.0;
else
med = *(arr + middle);
return med;
}
//*********************************************************
// Function mode *
// This function returns the mode of the array pointed to *
// by arr. The mode is the value that appears most often. *
// The parameter num holds the number of elements in the *
// array. If no element appears more than once, the *
// function returns -1. *
//*********************************************************
int mode(int *arr, int size)
{
int counter1 = 0, counter2, modevalue;
for (int i = 0; i<size; i++)
{
counter2 = 0;
for (int j = i; j<size; j++)
{
if (*(arr + i) == *(arr + j))
{
counter2++;
}
if (counter2 > counter1)
{
counter1 = counter2;
modevalue = *(arr + i);
}
}
}
if (counter1>1)
return modevalue;
else
return 0;
}
//**************************************************
// Function average *
// This function calculates and returns the average*
// of the values in the array arr. num is the *
// number of elements in the array. *
//**************************************************
double average(int *arr, int size)
{
   //Declaring variable
double total = 0;
  
//This for loop will calculate the sum of an elements in array
for (int i = 0; i<size; i++)
total += *(arr + i);
  
//Returning the average
return (total / size);
}

________________________

Output:

____________Thank You


Related Solutions

1.Prompts the user for a positive integer >= 0 2.Validates the user input to ensure it...
1.Prompts the user for a positive integer >= 0 2.Validates the user input to ensure it is a positive integer >= 0 3.Allocate (dynamically) an array big enough for the data. 4.Load the array with random numbers ranging in value from1 to 100 5.Display the elements of the array (unsorted) 6.Display the elements of the array (sorted) 7. Display the average 8.Display the median 9.Display the mode, if none, display appropriate message #include <iostream> #include <stdlib.h> /* srand, rand */...
Write a program which prompts the user for a positive integer, and then prints out the...
Write a program which prompts the user for a positive integer, and then prints out the prime factorization of their response. Do not import anything other than the Scanner. One way you might go about this using nested loops: Start a "factor" variable at 2 In a loop: repeatedly print the current factor, and divide the user input by it, until the user input is no longer divisible by the factor increment the factor This plan is by no stretch...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Write a new program named Bar that prompts the user to enter a positive integer. The...
Write a new program named Bar that prompts the user to enter a positive integer. The program should then display a line consisting of the entered number of asterisks using a while loop. If the user enters a number that is not positive, the program should display an error message (see example below). Example 1: Enter a positive number: 6 ****** Example 2: Enter a positive number: 11 *********** Example 3: Enter a positive number: -4 -4 is not a...
python programming. Write a program that prompts the user to enter an integer from 0 to...
python programming. Write a program that prompts the user to enter an integer from 0 to 9. The program will check if the input is a positive integer. It displays the correct answer and shows the guess is correct or not. The demos are shown as following.(Hint:1. Use a random math function 2. Use str.isdigit() to check the input)
Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers...
Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers using a sentinel while loop. The program should accept integer inputs until the user enters the value -1 (negative one is the sentinel value that stops the while loop). After the user enters -1, the program should display the entered numbers followed by their sum as shown below. Notice that -1 is not part of the output. The program should ignore any other negative...
Write a C++ program that will take and validates from the user an integer n between...
Write a C++ program that will take and validates from the user an integer n between 1 and 4. The program will then continue by taking 5 characters from the user. The program will ask the user whether he needs to rotate the characters to the left or to the right. If the user enters a wrong answer, the program will do a rotation to the right. Depending on the answer, the program will rotate the characters "n" times and...
Write a C++ program which prompts the user to enter an integer value, stores it into...
Write a C++ program which prompts the user to enter an integer value, stores it into a variable called ‘num’, evaluates the following expressions and displays results on screen. num+5, num-3, (num+3) – 2, ((num+5)*2 / (num+3)) For performing addition and subtraction, you are allowed to use ONLY the increment and decrement operators (both prefixing and postfixing are allowed). You must remember that using increment/decrement operators changes the original value of a number. Indent your code and include comments for...
Write a C program that uses a loop which prompts a user for an integer 10...
Write a C program that uses a loop which prompts a user for an integer 10 times and stores those integers in an array. The program will print out the numbers entered by the user with a comma and a space separating each number so the numbers can be read easily. The program will then calculate and print out the sum, integer average and floating point average (to 4 decimal points) of the numbers in the array. The information being...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT