http://pastebin.ca/3379585 Notes: C++ Program using pointers. Cannot use [] exce
ID: 3672049 • Letter: H
Question
http://pastebin.ca/3379585
Notes:
C++ Program using pointers.
Cannot use [] except for "new" function only.
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
//System Libraries
#include <iostream> //input/output stream library
#include <cstdlib> //C Standard Library
using namespace std; //utilize standard name-space directly
//User Libraries
//Global Constants
//Function Prototypes
void sort(int array[], int size);
int* mode(int [], int);
float mean(int [], int);
float median(int[], int);
/*
* Mode array may have nothing, 1 mode, or many
*/
int main(int argc, char** argv) {
int size=24;
int numbers[size];
for(int i=0; i<size; i++){
numbers[i]=i%5;
}
// int size;
// cout<<"Enter the number of integers you will input"<<endl; //prompt users for number of integers in data set
// cin>> size;
// int numbers[size];
// cout<<"Enter in your integer values"<<endl; //input integer data set
// for (int i= 0; i< size; i++){ //as user is inputting integers, they are stored into numbers[]
// cin>>numbers[i];
// }
int *modeArray= mode(numbers, size); //create an array of modes
int numModes= modeArray[0]; //retrieve the number of modes
int maxFreq= modeArray[1]; //retrieve the max frequency
if(!numModes){ //if numModes =0, there is no mode
cout<<"This data set has no modes ";
return 0;
}
else {
cout<<"There are "<<numModes<<" mode(s)."<<endl; //print out number of modes
cout<<"Each mode appears "<<maxFreq<<" times."<<endl; //print out the max frequency
cout<<"The mode(s) are: "; //print out the modes
for(int i = 2; i<numModes+2; i++){
cout<<modeArray[i]<<" ";
}
cout<<endl;
cout<<"The mean is: "<<mean(numbers, size)<<endl;
cout<<"The median is: "<<median(numbers, size)<<endl;
}
return 0;
}
int* mode(int numbers[], int size)
{
int maxFreq= 0, //max frequency of a number in the data set
numModes= 0, //how many modes there are in the data set
freq= 1; //frequency counter for each number in the data set
int currentElement; //current element of the data set's array in the loop
sort(numbers, size); //calls sort function to sort array
//find the maximum frequency:
for(int i = 0; i< size; i++){
currentElement= numbers[i]; //set currentElement to the ith element in numbers
int j=i+1; //check if the next element in numbers is the same as currentElement
while (numbers[j]== currentElement){ //if it is, then continue checking and add a count to the frequency
freq++;
j++;
}
i= j-1;//skip the next elements that are the same values
if(freq> 1 && freq> maxFreq){ //set max frequency
maxFreq= freq;
}
freq= 1;//reset freq to 1
}
//find number of modes:
for(int i = 0; i<size; i++){
currentElement= numbers[i];
int j=i+1;
while(numbers[j]==currentElement){
freq++;
j++;
}
i= j-1;
if(freq>1 && freq==maxFreq){ //if frequency of currentElement is same as the maxFreq
numModes++;
}
freq=1;//reset frequency counter to 1
}
// add modes to modeArray:
int *modeArray = new int[numModes+2]; //create array of modes
modeArray[0]=numModes; //set the first element in modeArray as number of modes
modeArray[1]=maxFreq; //set the second element in modeArrays as the maximum frequency
int index=1; //create an index counter for the modeArray
for(int i = 0; i<size; i++){
currentElement= numbers[i];
int j=i+1;
while(numbers[j]==currentElement){
freq++;
j++;
}
i= j-1;
if(freq>1 && freq==maxFreq){ //if frequency of currentElement is same as the maxFreq
index++;
modeArray[index]=numbers[i]; //add that currentElement to modeArray
}
freq=1;//reset frequency counter to 1
}
return modeArray;
}
float mean(int numbers[], int size){
float sum = 0.0;
for(int i = 0; i< size; i++){
//sum of all numbers in the array
sum+=numbers[i];
}
return sum/size; //returns average
}
float median(int numbers[], int size){
if (size%2==0) //if there's an even number of elements
//return average of the middle two numbers
return (float)(numbers[size/2] + numbers[size/2 + 1])/2;
else //if there's an odd number of elements
//return the middle number
return numbers[size/2 + 1];
}
//function definition of sort- selection sort
void sort(int array[], int size)
{
int startScan, minIndex, minValue;
for (startScan =0; startScan < (size -1); startScan++){
minIndex = startScan;
minValue = array[startScan];
for(int index= startScan + 1; index < size; index++){
if (array[index] < minValue){
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan]=minValue;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.