Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that creates an array of 10 integers. the program should then us

ID: 3810708 • Letter: W

Question

Write a program that creates an array of 10 integers. the program should then use the following functions: getData() Used to ask the user for the numbers and store them into an array displayData() Used to display the data in the array. displayLargest() Used to find and display the largest number in the array. displaySmallest() Used to find and display the smallest number in the array. displayAveroge() Used to find and display the average of the numbers in the array displayRange() Used to find and display the range of numbers in the array. displayMode() Used to find and display the mode of numbers in the array. **displayMedian() Used to find and display the median value in the array. (Extra Credit!) may not use any global variables. must use a defined constant (#define MAX 10) and use this constant in your code. Your code must well for both an even number and an odd number of integers.

Explanation / Answer

#include<stdio.h>

#define MAX 10

int *getData() {

   int i, *arr;

   printf("Enter %d integers: ",MAX);

   arr = malloc(sizeof(int)*MAX); //we use malloc so that the array is created in the heap, not stack

   for (i=0; i<MAX; i++)

      scanf("%d",&arr[i]);

   return arr;

}


void displayLargest(int *a) {

   int i, max;

   max = a[0];

   for (i=1; i<MAX; i++)

      if (a[i] > max)

          max = a[i];

   printf("Largest is: %d ",max);

}


void displaySmallest(int *a) {

   int i, min;

   min = a[0];

   for (i=1; i<MAX; i++)

      if (a[i] < min)

          min = a[i];

   printf("Smallest is: %d ",min);

}


void displayAverage(int *a){

   int i;

   double avg = 0;

   for (i=0; i<MAX; i++)

      avg = avg +a[i]; //first calculate sum

   avg /= MAX; then divide by size of array

   printf("Average is: %lf ",avg);

}

void displayRange(int *a) {

   int i, min, max;

   min = max = a[0];

   for (i=1; i<MAX; i++)

      if (a[i] < min)

          min = a[i];

      else if (a[i] > max)

          max = a[i];

   printf("Range is from %d to %d ",min,max);

}

void displayMode(int *a){

   int i,j, maxFreq, freq[MAX] = {0}; //freq array shall hold the frequency of each elemt

   maxFreq = 0;

   for (i=0; i<MAX; i++)

      for(j=0; j<=i; j++)

          if (a[i] == a[j]){ //only the frequency of the first occurrence is updated

              freq[j]++;

              if (freq[j] > maxFreq)

                  maxFreq = freq[j];

              Break; //break inner loop

          }

   printf("Mode(s) of data are: ");

   for (i=0; i<MAX; i++)

      if(freq[i] == maxFreq)

          printf("%d ",a[i]);

   printf(" ");

}

void main(){

   int *arr = getData();

   displayLargest(arr);

   displaySmallest(arr);

   displayAverage(arr);

   displayRange(arr);

   displayMode(arr);

}

I hope you like the code. If you have any doubt, feel free to comment below. I shall be glad to guide you to success :)

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