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

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

ID: 3793809 • Letter: W

Question

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

GENERAL RESTRICTIONS.

No global variables

No infinite loops, examples include:

No break statements to exit loops

#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */

using namespace std;

// 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);
int validateInt(int &userInput);

//main function
int main()
{
   int userInput;
first:
   cout << "How many numbers do you want to generate? ";
   cin >> userInput;
   if (cin.fail())
   {
       cin.clear();
       cin.ignore();
       cout << " that is not an integer >=0, try again ";
       goto first;
   }
   if (validateInt(userInput) == 0)
   {
       int *array = makeArray(userInput);
       loadNumberData(array, userInput);
       cout << "-------------------- ";
       displayArray(array, userInput);
       selectionSort(array, userInput);
       cout << " -------------------- ";
       displayArray(array, userInput);
       cout << " The Average is : " << average(array, userInput);
       cout << " the Median of the numbers is : " << median(array, userInput);
       int modeValue = mode(array, userInput);
       if (modeValue == 0)
           cout << " The set has no mode." << endl;
       else
           cout << " Array Mode is : " << modeValue;
   }
   else
   {
       cout << " that is not an integer >=0, try again ";
       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)
{
   double total = 0;
   for (int i = 0; i<size; i++)
       total += *(arr + i);
   return (total / size);
}

I need to store user input as string and int

Explanation / Answer

//You can use the following method to convert int to string.

#include <iostream>
#include<sstream>

using namespace std;

int main()
{
int x=42; //The integer
string str; //The string
ostringstream temp;
temp<<x;
str=temp.str();
cout<<str;

return 0;
}

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