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

Program specifics: Write a C++ program that does the following: a. Asks the user for the...
Program specifics: Write a C++ program that does the following: a. Asks the user for the distance to the pin and the depth of the green (both in yards). (Note: The pin is the hole in the green and the depth is the diameter of the green, assuming it is circular.) b. Asks the user for an integer club number from 2 to 10, where 10 is the pitching wedge (this club lifts the ball out of rough, sand, etc)....
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 does the following: As you can see, there is a file...
Write a C++ Program that does the following: As you can see, there is a file named "invoice1_test1.txt". You are to use this file as your input file for your program. Do the following: 1. Sort the file by last name using an array. You can use any of the sorting algorithms we have previously used in class. You may use the string data type to store text data. 2. Compute the following: a. The total balance due using the...
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 does the following: Read and input file containing the following PersonAName,...
Write a C++ program that does the following: Read and input file containing the following PersonAName, PersonBName, XA,YA, XB, YB where the coordinates of PersonA in a 100 by 100 room is XA, YA and the coordinates of PersonB is XB, YB. Use square root function in cmath sqrt() to calculate the shortest distance between two points. A file will be uploaded in this assignment that will list coordinates of two people. The program should use a function call that...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT