Question

Create a CodeBlocks project "HW 9"

Write the code to ask the user to enter the size of an array. Then create an integer array of that exact size.

Ask the user to enter a maximum value and then write a loop to fill the array with random numbers with value in the range of 1 to the maximum value. For example, if the maximum value is 100, random numbers must have value 1 to 100 inclusive.

Input size of the array must be greater than 1. Display error if this is not true and exit the program.

Input maximum value of random numbers must be greater than 9. Display error if this is not true and exit the program.

The program should find the smallest, the largest, and median value of the array. To find these values, the array must be sorted in ascending order (low value to high value)

Requirement:

Write a function that calculates median value using this EXACT prototype:

double median(int sortedNumbers[ ], int size)

Parameters:

    sortedNumbers: array that is already sorted before the function is called

    size: this is the input array size

Return: median value

main() function sorts the numbers then passes the array to the median function.

Feel free to define other functions as you want, but make sure you have function prototypes.

How to sort array

For example,

Array

  3 1 4 10 9 7

Sorted Array

  1 3 4 7 9 10

Use the sort() function in the C++ algorithm #include header to sort the array.

1/1 sort() Example using arrays. 2 // By Zereo 04/22/13 3 #include <iostream> 4 #include <algorithm> 6 using namespace std; e

Note that our array size is a VARIABLE input by the user, not a constant as seen in the example.

After sorting the array, write the code to find the smallest, largest and call median() function using the sorted array.

The Median is the "middle" number of a sorted array.

How to get median

For example

For Array with odd size 5:

1 3 7 9 9

                      ^

                       | median

The median is 7 (subscript 2). Note that array size is 5 and note that 2 = 5/2 (integer divide)

There are 2 numbers below and 2 numbers above the median 7

For Array with even size 6:

1 3 4 7 9 12

                              ^

                               | median

                           

The median is the average of the two middle numbers, (4 + 7) / 2 = 5.5

Note that subscript of number 4 is 2 (= 6 / 2 - 1) and subscript of number 7 is 3 (= 6 / 2)

There are 3 numbers below the median and 3 numbers above the median.

Notice that the median can have a decimal fraction even when the numbers are all integers, and that the median value may not be in the array.

You can use this code to test your median function with simple sorted arrays:

int size1 = 5;

int sortedArr[ size1 ] = { 1, 3, 7, 9, 9 };

double med = median(sortedArr , size1);

cout << med; // Should get result 7

int size2 = 6;

int sortedArr2[ size2 ] = { 1,3,4,7,9,12};

double med2 = median(sortedArr2 , size2);

cout << med2; // Should get result 5.5

Sample execution

This program generates random numbers and finds the median value. Enter the count of random numbers: 10000 Enter the maximum

Second run of program:

DCK9t_3FJkXIeXLOm2g6aJQ6iKBMQfMpF2rApZb8

Third run of program:

This program generates random numbers and finds the median value. Enter the count of random numbers: 0 Count must be greater

Fourth run of program:

This program generates random numbers and finds the median value. Enter the count of random numbers: 100 Enter the maximum va

THERE IS NO NEED TO USE ANY LOOP EXCEPT FOR A LOOP THAT FILLS ARRAY WITH RANDOM NUMBERS.

Remember to use srand() to seed the random generator once.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here's the .cpp code for finding median value as mentioned in question!

#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<time.h>
using namespace std;
double median(int sortedArray[],int size) {
if(size%2 == 0) { // for even size
return (sortedArray[size/2] + sortedArray[size/2-1]) / 2.0;
}
else { // for odd size
return (double)sortedArray[size/2];
}
}
int main() {
cout<<"This program generates random numbers and finds the median value.";
cout<<"\nEnter the count of random numbers: ";
int size;
cin>>size;
if(size <= 1) {
cout<<"Count must be greater than 1";
exit(0);
}
int intArray[size];
cout<<"\nEnter the maximum value of random numbers: ";
int max_value;
cin>>max_value;

if(max_value <= 9) {
cout<<"\nMaximum value must be greater than 9";
exit(0);
}

int greatestElement = 0; //initializing with minimum value which is zero
int smallestElement = INT_MAX; //initializing with maximum value an integer can hold

//filling array with random values.
srand(time(0)); //seeding random generator
for(int i=0;i<size;i++) {
intArray[i] = rand() % max_value + 1;
smallestElement = min(smallestElement,intArray[i]);
greatestElement = max(greatestElement,intArray[i]);
}
cout<<"\nThe smallest number is "<<smallestElement;
cout<<"\nThe largest number is "<<greatestElement;


//sorting array
sort(intArray, intArray+size);

//call to function median with sorted array and size of array
double myMedian = median(intArray,size);
cout<<"\nThe median number is "<<myMedian;
}

Add a comment
Know the answer?
Add Answer to:
Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++ please no global varaible write a value returning function input data to dynamically allocate a...

    c++ please no global varaible write a value returning function input data to dynamically allocate a short array of size elements and store the input data entered from the disk file into array.Return the base address of the array allocated.Read the data from the text file Hello.txt and store the data in reverse order in the array.the size of the data set will be in the file size 10 and the data was 1 2 3 4 5 6 7...

  •      program will enter data into two single dimension arrays (do not st...

         program will enter data into two single dimension arrays (do not store duplicate values in arrays)      program will find the union and intersection of the two arrays using one function      program will find the symmetric difference of two arrays      program will display the union, intersection, and symmetric difference   */     short* input_data(short size);   // function to dynamically allocate and array and enter data into the array void display_data(short *data, short size); // function to display data in an array void get_union_intersection(short...

  • Write a program to merge two sorted arrays into a third array. Ask the user to...

    Write a program to merge two sorted arrays into a third array. Ask the user to enter the size of the two arrays. The length of array1 could be greater than, equal to or less than the length of array2. Fill both with random numbers 0-99. Sort both arrays as explained below. Write a method merge that takes the two arrays as arguments. The method returns a merged array with size equal to the size of both arrays combined. Note:...

  • #include <iostream> using namespace std; struct node { int base=0; int power=0; }; void insert(node ptr[],int...

    #include <iostream> using namespace std; struct node { int base=0; int power=0; }; void insert(node ptr[],int basee,int powerr) { ptr[powerr].power=powerr; ptr[powerr].base=basee; } void subtract(node ptr1[],int size1,node ptr2[],int size2,node ptr3[]) { for(int j=0;j<=size1;j++) { if(ptr1[j].base!=0) { ptr3[j].base=(ptr3[j].base)+(ptr1[j].base); ptr3[j].power=ptr2[j].power; } } for(int j=0;j<=size2;j++) { if(ptr2[j].base!=0) { ptr3[j].base=(ptr3[j].base)-(ptr2[j].base); ptr3[j].power=ptr2[j].power; } } } void addition(node ptr1[],int size1,node ptr2[],int size2,node ptr3[]) { for(int j=0;j<=size1;j++) { if(ptr1[j].base!=0) { ptr3[j].base=(ptr3[j].base)+(ptr1[j].base); ptr3[j].power=ptr2[j].power; } } for(int j=0;j<=size2;j++) { if(ptr2[j].base!=0) { ptr3[j].base=(ptr3[j].base)+(ptr2[j].base); ptr3[j].power=ptr2[j].power; } } } void display(node ptr[],int size)...

  • Complete a program In C#: some code is included, you edit the areas that have not...

    Complete a program In C#: some code is included, you edit the areas that have not been filled. For your C# program you will complete code that plays the War card game. In this game, the deck of cards is evenly divided among two players. The players are not allowed to look at the cards in their hand. On each round of the game, both players lay down the top card from their hand. The player with the higher value...

  • 1. Your project will include the following three files: A header file: dynamicArray.h that includes a...

    1. Your project will include the following three files: A header file: dynamicArray.h that includes a list of function prototypes as enumerated in the next section. An implementation file: dynamicArray.cpp that implements the functions declared in the header file. A test driver file: dynamicArray-main.cpp that includes the main() function so that you can test all the functions you've implemented above. 2. The header file dynamicArray.h will include the following list of functions: constructing a dynamic array of the specified size...

  • Fix this C++ code so that the function prototypes come before main. Main needs to be...

    Fix this C++ code so that the function prototypes come before main. Main needs to be declared first. Then call to the functions from inside of main. #include<cstdlib> using namespace std; //prompt user for input void getGrades(int gradeArray[],int size) { for(int i=0; i<size; i++) { cout<<"Enter the grade "<<i+1<<": "; cin>>gradeArray[i]; } } // finding average of all numbers in array float computeAverage(int numbers[],int size) { float sum=0; for(int i=0; i<size; i++) { sum = sum + numbers[i]; //compute sum...

  • C programing Write a program to sort numbers in either descending or ascending order. The program...

    C programing Write a program to sort numbers in either descending or ascending order. The program should ask the user to enter positive integer numbers one at a time(hiting the enter key after each one) The last number entered by the user should be -1, to indicate no further numbers will be entered. Store the numbers in an array, and then ask the user how to sort the numbers (either descending or ascending). Call a function void sortnumbers ( int...

  • Write a C++ program that asks user number of students in a class and their names....

    Write a C++ program that asks user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’ names and ranking. Follow the Steps Below Save the project as A4_StudentRanking_yourname....

  • Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write...

    Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT