C++ Assignment Description: Develop a program that calculates basic statistics o
ID: 3882899 • Letter: C
Question
C++ Assignment
Description:
Develop a program that calculates basic statistics on a one-dimensional array of doubles. Your program should first prompt the user for the size of the array followed by prompting for individual elements of the array. Your program should contain the following functions:
1.) min()
Finds and returns the minimum value within the array.
2.) max()
Finds and returns the maximum value within the array.
3.) mean()
Finds and returns the mean value of the elements in the array.
4.) median()
Finds and returns the median value of the elements in the array.
5.) sort()*
Sorts the elements of the array in ascending order.
6.) readArray()
Populates the elements of the array with input from the user (or via file redirection).
7.) printArray()
Prints the elements of the array.
*You may use any sorting algorithm that you want. Here are a few easy suggestions:
Bubble sort pseudocode:
do
swapped = false
for each i in 1 to length(A) inclusive do:
if A[i-1] > A[i] then
swap A[i-1] and A[i]
swapped = true
end if
end for
while swapped
Insertion sort pseudocode:
i 1
while i < length(A)
j i
while j > 0 and A[j-1] > A[j]
swap A[j] and A[j-1]
j j - 1
end while
i i + 1
end while
Additional Specifications:
Your program should not use any pre-existing classes such as string or vector classes!
Make sure your program compiles and runs on one of the Linux machines in the Linux lab before you submit.
Your program should consist of a single source code.
EXAMPLE OUTPUT:
Examples using file redirection:
casey@vanderwaal:-/Dropbox/Teaching/CSCE24oFalt2617/Projects/Assignment2$ g++ Assignment2.cpp casey@vanderwaal:-/Dropbox/Teaching/CSCE240Fall2017/Projects/Assignment2$ ./a.out Please enter the size of your array: 6 Please enter 6 elements to populate the array. 212.3-6799 The elements of the array are: 212.3-6 7 99 The elements of the array sorted in ascending order: 6 0 27 12.3 99 The min of the array is-6 The max of the array is 99 The mean of the array is 19.05 The median of the array is 4.5 casey@vanderwaal:-/Dropbox/Teaching/CSCE240Fall2017/Projects/Assignment2$Explanation / Answer
Here is your c++ program:
C++ Program:
#include<iostream>
#include<conio.h>
using namespace std;
double arra[100];
static int n;
void mean()//to find mean
{
double sum=0;
for(int i=0;i<n;i++)
sum+=arra[i];
cout<<" The mean of the array is "<<sum/n;
}
void median()//to find median
{
int mid=n/2;
if(n%2!=0)
{
int temp=((n+1)/2)-1;
cout<<" The median of the array is "<<arra[temp];
}else
{
cout<<" The median of the array is "<<arra[(n/2)-1];
}
}
void min()//to find min
{
double low;
low=arra[0];
for(int i=1;i<n;i++)
{
if(low>arra[i])
low=arra[i];
}
cout<<" The min of the array is "<<low;
}
void max()//to find max
{
double high=arra[0];
for(int i=1;i<n;i++)
{
if(high<arra[i])
high=arra[i];
}
cout<<" The max of the array is "<<high;
}
void sort()//to sort array
{
double temp;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)
continue;
else
if(arra[i]<arra[j])
{
temp=arra[j];
arra[j]=arra[i];
arra[i]=temp;
}
}
}
cout<<" The elements of the array sorted in ascending order: ";
for(int i=0;i<n;i++)
cout<<arra[i]<<" ";
}
void main()
{
double input;
cout<<"Enter the size of array ";
cin>>n;
cout<<"enter Array Element ";
for(int i=0;i<n;i++)
{
cin>>input;
arra[i]=input;
}
sort();
min();
max();
mean();
median();
getch();
}
I hope this solves your problem
Comment if you have any problem in above code
And please give it a thumbs up if it solved our problem :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.