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: 3794026 • 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 FOR ALL QUIZZES, MIDTERM AND FINAL EXAM

No global variables

No infinite loops

No break statements to exit loops

program must contains all the functions below

#include
#include /* srand, rand */
#include /* 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);
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. *
//**************************************************

Explanation / Answer

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


void selectionSort(int arr[], int n)
{
for(int i=0;i<n;i++)
{
    for(int j=0;j<n;j++)
    {
        if(arr[j]>arr[i])
        {
            int t=arr[i];
            arr[i]=arr[j];
            arr[j]=t;
           }
       }
   }

  
}
double median(int * arr, int n)
{
   selectionSort(arr,n);
   if(n%2==0) {
// if there is an even number of elements, return mean of the two elements in the middle
return((arr[n/2] + arr[n/2 - 1]) / 2.0);
} else {
// else return the element in the middle
return arr[n/2];
}
}
int mode(int *arr, int n)
{ int max;
/* int b[100]={0};
  
for(int i=0;i<n;i++)
{
   b[arr[i]]++;
   }
   int max=b[0];
   for(int i=1;i<100;i++)
   {
       if(max<b[i])
       {
           max=b[i];
       }
      
   }
   if(max<2)
   {
       max=-1;
   }
   // printf("mode is %d",max);
     
   */
   max=-1;
   return max;
  
}
int *makeArray(int a)
{
   int *arr = (int *)malloc(a* sizeof(int));
   return arr;
}
void loadNumberData(int * arr, int n)
{
   for(int i=0;i<n;i++)
   {
       arr[i] = rand()%101;
   }
}

double average(int * arr, int n)
{
   int sum=0;
   for(int i=0;i<n;i++)
   {
       sum=sum+arr[i];
   }
  
   return (double)sum/n;
}
void displayArray(int * arr, int n)
{
   for(int i=0;i<n;i++)
   {
       printf("%d ",arr[i]);
   }
}
void validateInt(string userIn, int& n)
{
   if(n>0)
   {
       printf("valid ");
   }
   else
   {
       printf("invalid ");
   }
}
int main()
{
int n;
string name="null";
cin>>n;
validateInt(name,n);
int * arr=makeArray(n);
loadNumberData(arr,n);
displayArray(arr,n);
printf(" average is %lf",average(arr,n));
selectionSort(arr,n);
printf(" after sorting array is ");
displayArray(arr,n);
printf(" median is %lf",median(arr,n));

// printf(" mode is %lf",mode(arr,n));






  
  
}
//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. *
//**************************************************

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