Populate an array that contains 100 random numbers between 1

Populate an array that contains 100 random numbers between -100 and 100 Sort the array from largest to smallest Calculate the sum of all elements in the array Calculate average of array Get min and max of array Calculate median of array

Solution

#include<iostream>
#include<cstdlib>
using namespace std;
int* generatearray(int min ,int max,int no)
{

int * foo;
foo = new int [no];
for(int i=0;i<no;i++)
{
*(foo+i) = rand()%(max-min + 1) + min;
}
return foo;
}
int* insertion_sort (int arr[], int length){
      int j, temp;
      
   for (int i = 0; i < length; i++){
       j = i;
      
       while (j > 0 && arr[j] > arr[j-1]){
           temp = arr[j];
           arr[j] = arr[j-1];
           arr[j-1] = temp;
           j--;
           }
       }
return arr;
}

int main()
{
int *a=generatearray(-100,100,10);
a = insertion_sort(a,10);
int minval =10000;
int maxval =-100000;
int sum=0;
float avg;
for(int i=0;i<10;i++)
{
sum+=a[i];
if(a[i]>maxval)
maxval =a[i];
if(a[i]<minval)
minval=a[i];
}
avg=sum/10;
cout<<\"min value is: \"<<minval<<endl;
cout<<\"max value is: \"<<maxval<<endl;
cout<<\"Sum is : \"<<sum<<endl;
cout<<\"average is: \"<<avg<<endl;
}

 Populate an array that contains 100 random numbers between -100 and 100 Sort the array from largest to smallest Calculate the sum of all elements in the array

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site