Programming Fundamental 2. chapter 9 pointers You will write a program that read
ID: 3742295 • Letter: P
Question
Programming Fundamental 2. chapter 9 pointers
You will write a program that reads in data from a text file and stores the data in a dynamic array. Your program should then calculate and display the average, median and mode of the data read in. See the sample output at the end of this document. You are provided with the data file and a start cpp file for your program. You are not required to use the starter file and you may make as many changes to the starter file as you would like. However, you must follow the requirements below and your output must match the output at the end of this document when using the provided data file (without hard-coding).
Requirements
You must use a dynamic array to store the data that you read in from the data file. The data file is a list of integers. The first integer in the list is the count of the integers that follow. Each integer that follows is the number of movies watched by a single student. When you read in the data, it would make since to read in the first number, create a dynamic array of integers that is the size of that number, and then read the remaining integers into your array.
You must pass pointers to your functions and functions should perform pointer arithmetic. You are NOT allowed to use index operators, [], anywhere in your program except when creating your dynamic array.
You should have at least five functions:
- Read data from file
- Calculate the average
- Calculate the median
- Calculate the mode
- Sort an array of integers
- Swap two integer values
You have been provided prototypes for these functions in the starter cpp file. Note: you have been provided with two different funtion prototypes for reading data. You should only use one of these or write your own. The second prototype goes with a definition that is more challenging to write than the first.
Make sure you use good programming style. Comment all your variables, function definitions, and any calculations. Make good use of whitespace.
Explanation / Answer
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
//prototyoe for functions
int average(int adata[],int size);
int meadian(int adata[],int size);
int mode(int adata[],int size);
int swap(int *xp,int *xy);
int main(){
char data[100];
fstream file_name;
clrscr();
//open a file
file_name.open("INPUT.txt",ios::out | ios::in);
file_name>>data;
cout<<data<<endl;
//taking first element as a size of array
int size=int(data);
cout<<size<<endl;
//store data to int array
int adata[size];
for(int i=1;i<=4;i++){
file_name>>adata[i];
cout<<adata[i];
}
//calulate average
int ans=average(adata,size);
cin>>ans; //write into file
//meadian
ans=meadian(adata,size);
cin>>ans;
//mode
ans=mode(adata,size);
cin>>ans;
//sort an array
sort(adata,size);
for(i=1;i<size;i++){
cout<<adata[i];
}
//swaping data
swap('write your swap data here.....');
file_name.close();
getch();
return 0;
}
int average(int adata[],int size){
int sum=0;
float average=0;
//caluate total
for(int i=1;i<=size;i++){
sum=sum+adata[i];
}
//divide by total no
average=sum/size;
cout<<"Average is="<<average<<endl;
return average;
}
int meadian(int adata[],int size){
float meadian=0;
int mid;
//if even no
if(size%2==0){
int temp=(size/2);
mid=adata[temp]+adata[temp+1];
meadian=mid/2;
cout<<"Meadian is..."<<meadian<<endl;
}
//if odd no
else{
int temp=(size/2);
meadian=adata[temp];
cout<<"Meadian is....."<<meadian<<endl;
}
return meadian;
}
int mode(int adata[],int size){
int i, j,z, tmp, maxCount, modeValue;
int temp[size];
//count repeating no
for(i=0;i<size;i++){
for(j=0;j<4-i;j++) {
if(adata[j]>adata[j+1]) {
tmp=adata[j];
adata[j]=adata[j+1];
adata[j+1]=tmp;
}
}
}
for (i = 0; i < size; i++){
for(z=i+1;z<size;z++){
if(adata[i]==adata[z]){
temp[i]++;
}
}
}
maxCount = 0;
modeValue = 0;
for (i = 0; i < size; i++){
if (temp[i] > maxCount){
maxCount = temp[i];
modeValue = adata[i];
}
}
cout<<" Mode value is : "<< modeValue;
return mode;
}
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void sort(int adata[],int size)
{
int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < size-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < size; j++)
if (adata[j] < adata[min_idx])
min_idx = j;
// Swap the found minimum element with the first element
swap(&adata[min_idx], &adata[i]);
}
}
INPUT::
(INPUT.txt)
1 1 2 4 7
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.