Question

For this assignment you may use regular pointers. You are to write a program that performs...

For this assignment you may use regular pointers.

You are to write a program that performs basic statistical analysis on a 500 element array of integers. Your program should include the following programmer defined functions:

int* generateArray(int size){
This function should dynamically create an array of 501 elements and provide random numbers to fill the array. The random numbers should range from 10 to 100. The function should return the created array to the array generated in the main program.

}

int findMode(int *arr, int size){
This function should analyze the array to find the most occurring value in the array. You do not have to account for bi-modal occurrences, so the first modal value is enough to satisfy this function definition.
}

int findMedian(int *arr, int size){
This function should determine the median of the set of numbers in the array. Since there are 501 elements, you should have a clean median. Do not forget that the list has to be sorted in order to properly find the median.
}

Of course, main is critical to the function of your program and should output the mode, median of the list. An output of the 501 element array is not necessary.

Homework Answers

Answer #1

// C++ program that performs basic statistical analysis on a 501 element array of integers

#include <iostream>

#include <iomanip>

#include <cstdlib>

#include <ctime>

using namespace std;

// function prototypes

int* generateArray(int size);

int findMode(int *arr, int size);

int findMedian(int *arr, int size);

void sort(int *arr, int size);

int main()

{

       srand(time(0));

       int size = 501;

       // create an array of 501 random elements between [10,100]

       int *numbers = generateArray(size);

       // determine and print the first modal value

       cout<<"Mode : "<<findMode(numbers,size)<<endl;

       // calculate and print the median value

       cout<<"Median : "<<findMedian(numbers,size)<<endl;

               return 0;

}

// function to create and return an array of size filled, with random elements between [10,100]

int* generateArray(int size)

{

       int *numbers = new int[size];

       for(int i=0;i<size;i++)

             numbers[i] = rand()%91+10;

       return numbers;

}

// function to determine and return the first modal value from the array arr

int findMode(int *arr, int size)

{

       int countMode = 0;

       int mode ;

       int count;

       // loop to count the frequency of each number in the array

       for(int i=0;i<size;i++)

       {

             count = 1;

             for(int j=i+1;j<size;j++)

             {

                    if(arr[j] == arr[i] )

                           count++;

             }

             // if count of ith number > current mode count, update the mode and its count

             if(count > countMode)

             {

                    countMode = count;

                    mode = arr[i];

             }

       }

       return mode; //return first modal value

}

// function to calculate and return the median value from the array arr

int findMedian(int *arr, int size)

{

       sort(arr,size);

       int median;

       // if number of elements is even, median is half of the sum of the middle two values

       if(size%2 == 0)

             median = (arr[int((size-1)/2)]+arr[size/2])/2;

       else // else return the middle value

             median = arr[(size-1)/2];

       return median;

}

// function to sort the elements of the array in ascending order

void sort(int *arr, int size)

{

       int min;

       for(int i=0;i<size-1;i++)

       {

             min = i;

             for(int j=i+1;j<size;j++)

                    if(arr[j] < arr[min])

                           min = j;

             if(min != i)

             {

                    int temp = arr[i];

                    arr[i] = arr[min];

                    arr[min] = temp;

             }

       }

}

//end of program

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output...
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output for proof. Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Implement the following functions: Implement a function called readData int readData( int *arr) arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr....
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Requirements Implement the following functions: Implement a function called readData int *readData( )   The function returns a pointer that points to the locations with integers reading from the file data.txt. arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array...
Java Question-- I have a program with 2 separate methods-- 1 method that assigns random values...
Java Question-- I have a program with 2 separate methods-- 1 method that assigns random values to an array and the other that uses the insertion method to sort the array. And of course I have a main method. I need to call both of these methods in the main method. I have successfully called the random assigned method from the main method, but I am confused on how to call the second sort method. Here is what I have--...
USE PYTHON LANGUAGE PLEASE FOCUS YOU SHOULD ENTER AN ARRAY AND THEN THE PROGRAM GIVE OUTPUT(...
USE PYTHON LANGUAGE PLEASE FOCUS YOU SHOULD ENTER AN ARRAY AND THEN THE PROGRAM GIVE OUTPUT( TRUE/ FALSE) QUIZ 8 Array Challenge Have the function ArrayChallenge(arr) take the array of numbers stored in arr and return the string true if any two numbers can be multiplied so that the answer is greater than double the sum of all the elements in the array. If not, return the string false. For example: if arr is [2, 5, 6, -6, 16, 2,...
Write a program to determine the minimum element in an array of ten elements. The program...
Write a program to determine the minimum element in an array of ten elements. The program should have the following: 1. Class Name as ArrayProcessing. The main method should create an array of size 10 2. There should be two methods besides the main method in the class namely inputArray and MinimumElement 3. InputArray method should assign the ten elements in the array. Use scanner to input these elements. The array of 10 numbers in the method "InputArray" should be...
Your assignment is to write a c++ function that can calculate values for any 5 th...
Your assignment is to write a c++ function that can calculate values for any 5 th order polynomial function. ?(?) = ?0 + ?1? + ?2? 2+?3? 3 + ?4? 5 + ?5? 5 Your function should accept only two parameters, the first parameter should be the value of x at which to calculate y(x). The second parameter should be an array with 6 elements that contain the coefficients a0 to a5. The output of your function should be the...
Write a function that passes an array argument, getRandomNumbers, to get a pointer to an array...
Write a function that passes an array argument, getRandomNumbers, to get a pointer to an array of random numbers in the array. The function dynamically allocates an array, uses the system clock to seed the random number generator, populates the array with random values, and then returns a pointer to the array. Function getRandomNumbers to generate a random array and return a pointer. int* getRandomNumbers(int num); // The parameter indicates the number of numbers requested. The algorithm can be described...
Note: Do not use classes or any variables of type string to complete this assignment Write...
Note: Do not use classes or any variables of type string to complete this assignment Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along...