Question

In: Computer Science

Write a C program that does the following. (a) Start with comment statements at the top...

Write a C program that does the following.

(a) Start with comment statements at the top (before your includes) with the following info: Name, Date

(b) Write the following functions:

void setupRandArray(int n, int x[], int min, int max)

void printArray(int n, int x[], char label[])

float getAverage(int n, int x[]) int getMaximum(int n, int x[])

int getCountInRange(int n, int x[], int min, int max)

Note: This function returns the number elements of x that are between min and max (including min and max).

(c) Write a main to test your functions. Create an array of random test scores between 70 and 100. Compute the average score, the high score, and the number of B grades.

A typical output is shown below. Do all printing of results at the end of main. Run your program twice: once with 10 scores and then with 20 scores. Put your output in comments underneath main.

Hint: Set up a variable for the array size in main so that you can easily change it.

scores: 78 93 72 98 94 92 86 90 83 75

ave: 81.2

high: 98

numB: 2

scores: 88 71 95 83 88 90 72 89 79 73 94 91 86 88 78 92 82 81 76 70

ave: 83.2

high: 95

numB: 8

Solutions

Expert Solution

C Code:

//Name : ---- Roll :----
#include <stdio.h>
#include <stdlib.h>
//function defintion to create or setup random array of size n between min and max
void setupRandArray(int n, int x[], int min, int max){
  for(int i=0; i<n; i++)
  {
    x[i] = (rand() % (max-min+1)) + min;       //rand() is function to generate random numBer.
  }
}

void printArray(int n, int x[], char label[]){
  printf("scores %s",label);
  for(int i=0; i<n; i++)
  {
    printf("%d ", x[i]);  //printing array elements
  }
}
double getAverage(int n, int x[])  //function defintion for calculating average
{
    double ans = 0;
    for(int i=0; i<n; i++)
    {
         ans += x[i];               //calculating sum of all elements
    }
   return ans/n;                    //returning the sum of elements
}   
int getMaximum(int n, int x[])      //function defintion for calculating maximum in array
{
    int max = -1;
    for(int i=0; i<n; i++)
    {
       if(max < x[i])
       {
       max = x[i];
    }
  }
  return max;               //returning max value
}
int getCountInRange(int n, int x[], int min, int max)       //function defintion to calculate no in range
{
  int cnt=0;
  for(int i=0; i<n; i++)
  {
      if(x[i] >= min && x[i] < max)         //checking for range
      cnt++;    
  }
  return cnt;                           //return the no. which are in range
}
int main()
{
  int n = 20,high,numB;
  double avg;
  int arr[n];
  int min = 70, max = 100;
  char label []=": ";
  setupRandArray(n, arr, min, max);
  printArray(n, arr, label);
  avg = getAverage(n, arr);
  high = getMaximum(n, arr);
  numB = getCountInRange(n, arr, 90, 100);
  printf("\nave: %.1f\n", avg);
  printf("high: %d\n", high);
  printf("numB: %d\n", numB);
}
//Output for 10 elemnts
/*scores : 80 92 80 96 81 77 78 71 93 75
ave: 82.3
high: 96
numB: 3*/
//Output for 20 elements
/*scores : 80 92 80 96 81 77 78 71 93 75 71 98 70 95 80 96 89 77 92 81 
ave: 83.6
high: 98
numB: 7*/

Output for 10 elements:

Output for 20 elements:

If you have any doubt feel free to ask and if you like the answer please upvote it .

Thanks


Related Solutions

Write a program that does the following in C++ 1 ) Write the following store data...
Write a program that does the following in C++ 1 ) Write the following store data to a file (should be in main) DC Tourism Expenses 100.20 Revenue 200.50 Maryland Tourism Expenses 150.33 Revenue 210.33 Virginia Tourism Expenses 140.00 Revenue 230.00 2 ) Print the following heading: (should be in heading function) Store name | Profit [Note: use setw to make sure all your columns line up properly] 3 ) Read the store data for one store (should be in...
Write a C program that does the following In this part, you will write more complicated...
Write a C program that does the following In this part, you will write more complicated functions. They will require parameters and return values. The purpose is to give you experience with these components, and to show you how functions can be used to break your code down into smaller parts. You will also get some more experience with iterating through arrays.Open repl project Lab: User-Defined Functions 2. Write a program that does the following: 1.(20 pts.) Allows the user...
You are to write a C++ program which does the following: Reads in the size of...
You are to write a C++ program which does the following: Reads in the size of a list of characters. Reads in the list of characters. Prints the list of characters in the opposite order read in. Prints the list of characters in the order read in. Sorts the list. Prints the sorted list. You may assume there will be no more than 1000 characters in the list. (You should use a constant to make this limit easily changeable.) You...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an empty main function, then add statements in main() to accomplish each of the tasks listed below. Some of the tasks will only require a single C++ statement, others will require more than one. For each step, include a comment in your program indicating which step you are completing in the following statement(s). The easiest way to do this is copy and paste the below...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an empty main function, then add statements in main() to accomplish each of the tasks listed below. Some of the tasks will only require a single C++ statement, others will require more than one. For each step, include a comment in your program indicating which step you are completing in the following statement(s). The easiest way to do this is copy and paste the below...
Write a C++ program that will display the top internet stories from the stories.txt file The...
Write a C++ program that will display the top internet stories from the stories.txt file The program must display the stories that have a score which the statistical "mode". The "mode" of a set of values is the value that occurs most often (with the greatest frequency) The data about the stories is in a file that contains the following: storyTitle    (a sequence of numbers and/or letters, may contain spaces in it) storyURL    (a regular URL, like http://www.twitter.com/story1,without spaces) score        (an integer number,...
Write a program in C that does the following: 1. Declares an array called numbers_ary of...
Write a program in C that does the following: 1. Declares an array called numbers_ary of 6 integer numbers. 2. Declares an array called numbers_ary_sq of 6 integer numbers. 3. Reads and sets the values of numbers_ary from the keyboard using a loop. 4. Sets the values of numbers_ary_sq to the square of the values in numbers_ary using a loop. 5. Displays the values of numbers_ary and the values of numbers_ary_sq beside each other using a loop. Example Output Assume...
Write the following program in java please. a. Include a good comment when you write the...
Write the following program in java please. a. Include a good comment when you write the method described below: Method method1 receives an integer, n, and a real number, d, and returns a real number. If the integer is positive it returns the square root of the real number added to the integer. If the integer is negative or zero it returns the absolute value of the integer multiplied by the real number. b. Write the statements from the main...
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
Write a program in java processing. Write a program that does the following: · Assume the...
Write a program in java processing. Write a program that does the following: · Assume the canvas size of 500X500. · The program asks the user to enter a 3 digit number. · The program then checks the value of the first and last digit of the number. · If the first and last digits are even, it makes the background green and displays the three digit number at the mouse pointer. · If the two digits are odd, it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT