Statistics with Pointers Program You are to write code to compute some statistic
ID: 3670921 • Letter: S
Question
Statistics with Pointers Program
You are to write code to compute some statistical information. You are to read a set of numbers from the input file “numbers.txt”. I don’t know how many numbers are in the file, so set up your array to hold the same number of values located in the file and allocate the array dynamically. Read in the values into the allocated array.
Print out the number of values read in and the average. Determine from the list of numbers in the array the ‘mode’. The mode is the value that occurs most often in the list of numbers. (A function) Find the ‘median’ of the set of values. The median is the middle value in the list. If the list has an odd number of values, then select the middle one. If the list has even number of values, then take the average of the two middle values in the list. Next, reverse the values in the array and then print out the first 32 values of the list, 8 numbers per line. Don’t print the entire list please. Print out the number of times each value in the list appears in the list. Ex. 1-3 5-2 … where 1 is in the list 3 times, 5 is in the list twice etc. Label each output part.
Restrictions: You must dynamically allocate your array. You must use pointers to manipulate the array.
No [ ]’s are to be found in your code. You can use the [ ]’s for ‘new’ function only.
Explanation / Answer
int main()
{
ifstream file("numbers.txt");
if(file.is_open())
{
double* myArray = NULL;
myArray = new double [];
for(int i = 0; i < 5; ++i)
{
file >> myArray[i];
}
}
// finding mean
int n=myArray.length;
double sum=0;
double median,mode;
int count = 1;
int countMode = 1;
for(int i=0;i<n;i++)
sum=sum+myArray[i];
double avg=sum/n ;
cout<<"Mean/Average is:"<<avg;
//To find meadian, we first sort the myArray, and then continue. We are suing Insertion sort here to sort the myArray
for (int i = 1; i < n; i++)
{
for (j = i; j >= 1; j--)
{
if (myArray[j] < myArray[j-1])
{
temp = myArray[j];
myArray[j] = myArray[j-1];
myArray[j-1] = temp;
}
else
break;
}
}
//Now we find Median
if(n%2==0)
median=(myArray[n/2]+myArray[(n/2) + 1])/2;
else
median=myArray[n/2];
cout<<"Median is :"<<median;
// Now find Mode
int number=myArray[0];
for (int i=1; i<n; i++)
{
if (myArray[i] == number) {
countMode++;
}
else
{ // now this is a different number
if (count > countMode)
{
countMode = count;
mode = number;
}
count = 1;
number = myArray[i];
}
}
cout<<"Mode is :"<<mode;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.