Question

Please help with this coding 1.You need to dynamically allocate memory to read into a number...

Please help with this coding

1.You need to dynamically allocate memory to read into a number of elements. At first, you need to determine how many numbers (here, ‘n’) to be read. Next, you need to dynamically allocate memory for ‘n’ integers. As you see,‘x’ is an integer pointer which needs to dynamically allocate memory to read ‘n’integers into it fromthekeyboard.

2 Read nintegers into the allocated block using scanf.

3. Complete the printArrayfunction to display ‘n’ integers. void printArray(int *array, const int size);

4.Complete the following functions to find the average (mean), median and the standard deviation of ‘n’ integers.

   a)double findMean(int *array, const int size);

b)double findMedian(int *array, const int size);

c)double findStandardDeviation( int*array, const int size, double average);6ptsUse the attached cheat sheet“SimpleStats.pdf” to find mean, median and standard deviation of numbers. Please be remembered, if you type only one integer, its standard deviation shouldbe set to zero.

5.Please be remembered that you need to have the array sorted to find the median value. So, you also need to write the following sortArray function:void sortArray(int *array, const int size);

6.The element in the sorted array should be arranged in ascending order. Inside sortArrayfunction, you need to call the swapElementsfunction as mentioned below. So, you also need to complete the swapElement function.void swapElements(int *a, int *b);

7.Lastly, you need to free the allocated memory....................................................................

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void swapElements(int *a, int *b);
void sortArray(int *array, const int size);
void changeElements(int *val);
void printArray(int *array, const int size);
double findMean(int *array, const int size);
double findMedian(int *array, const int size);
double findStandardDeviation( int *array, const int size, double average);

int main(){

   int n, *x;
        double mean = 0.0, median = 0.0, stdDev = 0.0;

   printf("This is the basic part of the program that asks the user to type the number of integers, i.e., 'n'. Next, allocate memory for 'n' integers, read the values of 'n' integers into the allocated memory usining scanf; next, find the mean, median and average of 'n' integers.Lastly, the allocated memory needs to be freed.\n");
      
   printf("\nRead using scanf how many integers you would like to type:\n");
        scanf("%d", &n);

      
   /*****************************************************************/

   //x is not automatically assigned a memory block when it is defined as a pointer variable, you need to allocate a block
   //of memory large enough to hold 'n' integers
        // Write the function that allocates memory to hold 'n' integers

      
        printf("Please type 'n' integers: \n");
   /***********************************************************************/
   //Read in the list of numbers 'n' into the allocated block using scanf
  
      

        printf("Displaying the numbers:\n");

       // Call printArray to display the integers    

        // Find the mean of integers using findMean function
        printf("Mean of the numbers is: %f\n", mean);

        // Fidn the median of integers using findMedian function
        printf("Median of the numbers is: %f\n", median);
        // Find the standard deviation of integers using findStandardDeviation function
         printf("Standard deviation of the numbers is: %f\n", stdDev);
  
       //Deallocate the memory ;
      
      
      
        return 0;
}

void printArray( int *array, const int size){ int j;. for ( j = 0; j < size; j++){. printf("Array index is: %d and value is: %d\n", j, array[j]);. } printf("\n");. }

void sortArray(int *array, const int size){

     //Complete this function
}

void swapElements( int *x, int *y){

     // Complete this function

}

double findMean(int *array, const int size){

    // COmplete this function
}

double findMedian(int *array, const int size){

   //Complete this function;
}


double findStandardDeviation( int *array, const int size, double average){

       // Complete this function

}

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

// C program to create a dynamic array and perform operations on the array
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void swapElements(int *a, int *b);
void sortArray(int *array, const int size);
void changeElements(int *val);
void printArray(int *array, const int size);
double findMean(int *array, const int size);
double findMedian(int *array, const int size);
double findStandardDeviation( int *array, const int size, double average);

int main(void) {

   int n, *x, i;
   double mean = 0.0, median = 0.0, stdDev = 0.0;

   printf("This is the basic part of the program that asks the user to type the number of integers, i.e., 'n'. \nNext, allocate memory for 'n' integers, read the values of 'n' integers into the allocated memory using scanf; \nnext, find the mean, median and average of 'n' integers.Lastly, the allocated memory needs to be freed.\n");

   printf("\nRead using scanf how many integers you would like to type:\n");
   fflush(stdout);
   scanf("%d", &n);


   /*****************************************************************/

   //x is not automatically assigned a memory block when it is defined as a pointer variable, you need to allocate a block
   //of memory large enough to hold 'n' integers
   // Write the function that allocates memory to hold 'n' integers

   x = (int*)malloc(sizeof(int)*n);
   printf("Please type 'n' integers: \n");
   /***********************************************************************/
   //Read in the list of numbers 'n' into the allocated block using scanf
   for(i=0;i<n;i++)
   {
       printf("Element-%d : ",i+1);
       fflush(stdout);
       scanf("%d",&x[i]);
   }


   printf("Displaying the numbers:\n");

   // Call printArray to display the integers
   printArray(x,n);
   // Find the mean of integers using findMean function
   mean = findMean(x,n);
   printf("Mean of the numbers is: %f\n", mean);

   // Find the median of integers using findMedian function
   median = findMedian(x,n);
   printf("Median of the numbers is: %f\n", median);
   // Find the standard deviation of integers using findStandardDeviation function
   stdDev = findStandardDeviation(x,n,mean);
   printf("Standard deviation of the numbers is: %f\n", stdDev);

   //Deallocate the memory ;
   free(x);
   return EXIT_SUCCESS;
}


void printArray( int *array, const int size){
   int j;
   for ( j = 0; j < size; j++)
   {
       printf("Array index is: %d and value is: %d\n", j, array[j]);
   }
   printf("\n");
}

void sortArray(int *array, const int size){

   int i,j;
   int min ;
   for(i=0;i<size-1;i++)
   {
       min = i;
       for(j=i+1;j<size;j++)
       {
           if(array[j] < array[min])
               min = j;
       }

       if(i != min)
       {
           swapElements(&array[i],&array[min]);
       }
   }
}

void swapElements( int *x, int *y){

   int temp = *x;
   *x = *y;
   *y = temp;
}

double findMean(int *array, const int size){

   int total = 0;
   int i;
   for(i=0;i<size;i++)
       total += array[i];
   if(size > 0)
       return(((double)total)/size);
   return 0;
}

double findMedian(int *array, const int size){

   sortArray(array,size);
   double median;
   if(size > 0)
   {
       if(size%2 == 0)
       {
           median = ((double)((double)array[(int)((size-1)/2)] + (double)array[(int)(size/2)]))/2;
       }else
           median = array[(int)(size/2)];
   }else
       median = 0;
   return median;

}


double findStandardDeviation( int *array, const int size, double average){

   int i;
   double deviation = 0;
   for(i=0;i<size;i++)
   {
       deviation += pow((double)(array[i]-average),2);
   }

   if(size > 0)
   {
       deviation = deviation/size;
       deviation = sqrt(deviation);
       return deviation;
   }

   return 0;
}

//end of program

Output:

Add a comment
Know the answer?
Add Answer to:
Please help with this coding 1.You need to dynamically allocate memory to read into a number...
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
  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

  • Update your first program to dynamically allocate the item ID and GPA arrays. The number of...

    Update your first program to dynamically allocate the item ID and GPA arrays. The number of items will be the first number in the updated “student2.txt” data file. A sample file is shown below: 3 1827356 3.75 9271837 2.93 3829174 3.14 Your program should read the first number in the file, then dynamically allocate the arrays, then read the data from the file and process it as before. You’ll need to define the array pointers in main and pass them...

  • sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size,...

    sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size, c;     float median;     printf("Enter the array size:\n");     scanf("%d", &size);     array = (int*) malloc(size * sizeof(int));     printf("Enter %d integers:\n", size);     for (c = 0; c < size; c++)         scanf("%d", &array[c]);     sort(array, size);     printf("Array sorted in ascending order:\n");     for (c = 0; c < size; c++)         printf("%d ", array[c]);     printf("\n");     median = find_median(array,...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • Debug the following matrix program in C: // Program to read integers into a 3X3 matrix...

    Debug the following matrix program in C: // Program to read integers into a 3X3 matrix and display them #include <stdio.h> void display(int Matrix[3][3],int size); int main(void) {         char size;         double Matrix[size][size+1];         printf("Enter 9 elements of the matrix:\n");         int i;         for (i = 0: i <= size: i++)     {       int j = 0;       for (; j <= size++; j++){         scanf("%d", matrix[i--][4])       }     }         Display(Matrix,9);         return 0; void...

  • I need the pseudocode for this c program source code. #include<stdio.h> void printarray(int array[], int asize){...

    I need the pseudocode for this c program source code. #include<stdio.h> void printarray(int array[], int asize){ int i; for(i = 0; i < asize;i++) printf("%d", array[i]); printf("\n"); } int sum(int array[], int asize){ int result = 0; int i = 0; for(i=0;i < asize;i++){ result = result + array[i]; } return result; } int swap( int* pA,int*pB){ int result = 0; if(*pA > pB){ int tamp = *pA; *pA = *pB; *pB = tamp; result = 1; } return result;...

  • Need this in C The starter code is long, if you know how to do it...

    Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • Hey everyone, I need help making a function with this directions with C++ Language. Can you...

    Hey everyone, I need help making a function with this directions with C++ Language. Can you guys use code like printf and fscanf without iostream or fstream because i havent study that yet. Thanks. Directions: Write a function declaration and definition for the char* function allocCat. This function should take in as a parameter a const Words pointer (Words is a defined struct) The function should allocate exactly enough memory for the concatenation of all of the strings in the...

  • Create a C project named login_l03t1. Create an array of int from parameters passed on the...

    Create a C project named login_l03t1. Create an array of int from parameters passed on the command line. Use argc to define the size of the array. Print the contents of the array in the format and Generate and print the total of the values in the array twice: once by using an index i, and a second time by incrementing a pointer to the array. I need help with this please! what I have done is below: int n...

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