This assignment must be done using pointers functions and dy
This assignment *must* be done using pointers, functions and dynamic memory allocation. I want to see that you understand how to use pointers and memory allocation before we move on to more advanced topics.
Write a C program that asks the user to enter an integer n of any size. You will then generate n random numbers (between 0 and 99) and store them in an array. Iterate through this array to print the unsorted values. Then, pass this array to a function that will sort the array using any sorting algorithm you choose. Upon returning to your main function, print the now sorted array.
Then, using the sorted array, calculate the mean, median, mode, standard deviation and variance from the values in the array. Each of these statistics should be calculated in a different function and returned to the main function.
Solution
#include <stdio.h>
 #include <stdlib.h>
int main()
 {
 int n, i,j,t,median, *ptr, sum = 0;
    float x;
printf(\"Enter number of elements: \");
 scanf(\"%d\", &n);
ptr = (int*) malloc(n * sizeof(int)); //memory allocated using malloc
 if(ptr == NULL)   
 {
 printf(\"Error! memory not allocated.\");
 exit(0);
 }
printf(\"Enter elements of array: \");
 for(i = 0; i < n; ++i)
 {
 //scanf(\"%d\", ptr + i);
        int r = rand()%100;
 //sum += *(ptr + i);
        ptr+i=r;
 }
printf(\"Sum = %d\", sum);
   
    //calculate mean
    x= getMean(n,sum);
 printf(\"Mean\\t= %f\",x);
      
    x= getMedian(n,sum);
 printf(\"Median\\t= %f\",x);
      
    for(i=0;i<n;i++)
 {
 for(j=i+1;j<n;j++)
 {
 if(*(ptr+i)>*(ptr+j))
 {
 t=*(ptr+i);
 *(ptr+i)=*(ptr+j);
 *(ptr+j)=t;
 }
 }
 }
 
 //calculate median
 median = getMedian(n, ptr);
    printf(\"median\\t= %f\",median);
   
    //calculate mode
    calculateMode(n,*ptr);
   
 free(ptr);
 return 0;
 }
float getMean(int n,int sum){
float x;
 x=(float)sum/(float)n;
    return x;
 }
float getMedian(int n, int* ptr){
 float y;
 if(n%2==0)
 y=(float)(*(ptr + n/2)+*(ptr+(n-1)/2)/2;
 else
 y=*(ptr+(n-1)/2);
 printf(\"\ Median\\t= %f\",y);
 return y;
 }
 
 void calculateMode(int n,int* ptr){
 
 int i,j,ptr+0=0,n,t,ptr+n=0,k=0,c=1,max=0,mode,*ptr1;
 float x=0.0,y=0.0;
 for(i=0;i<n-1;i++)
 {
 mode=0;
 for(j=i+1;j<n;j++)
 {
 if(*(ptr+i)==*(ptr+j))
 {
 mode++;
 }
 }
 if((mode>max)&&(mode!=0))
 {
 k=0;
 max=mode;
 ptr1+k=*(ptr+i);
 k++;
 }
 else if(mode==max)
 {
 ptr1+k = *(ptr+i);
 k++;
 }
 }
 for(i=0;i<n;i++)
 {
 if(*(ptr+i)==*(ptr1+i))
 c++;
 }
 if(c==n)
 printf(\"\ There is no mode\");
 else
 {
 printf(\"\ Mode\\t= \");
 for(i=0;i<k;i++)
 printf(\"%d \",*(ptr1+i));
 }
 }



