Assignment 4: Pointers and Files Due Saturday, August 6, 2011 by 11:59:59 pm PST
ID: 3627623 • Letter: A
Question
Assignment 4: Pointers and FilesDue Saturday, August 6, 2011 by 11:59:59 pm PST
Write a program that reads one or more sets of numbers from a file. The filename will be specified by the user. Each set of numbers consists of an integer N followed by N integer values. These values will be in the range from -1,000,000 to 1,000,000 (guaranteed). Your task is to compute the mean, median, and mode of each set of N integers. N may range from 1to 2 Billion. Your arrays will be dynamically allocated. They will be the exact size. Notice in the file that the first number for the set of numbers is the number of elements in the array.
As output for each set of numbers, print a single line containing the word "Mean" followed by the mean, the word "Median" followed by the median, and the word "Mode" followed by the mode, leaving enough spacing on the line to allow easy readability. Subsequent lines should be aligned with the first line (thus creating columns). Fractional portions should be correct to at least two decimal positions.
Definitions for purposes of this problem:
Mean is defined as the result of dividing the sum of the N numbers by N, the number of numbers.
Median is the "middle" number. That is, in an ordered list of numbers, the one in the middle of the list is the median. If N is even, then the mean of the two numbers in the middle is the median.
Mode is the value that occurs most often in the list. In case of ties, report the smallest value that meets the criteria.
Sample input:
5
1
3
5
4
5
Mean: 3.60 Median: 4 Mode: 5
4
6
2
5
25
Mean: 9.50 Median: 5.50 Mode: 2
--------------------------------------------------------------------------------
Development Details
Design your code in as modular a fashion as possible. Individual tasks should go in individual functions. You WILL be graded heavily on this.
Error checking for range is a must
Name the C file that contains your main method cscd255hw4.c
Explanation / Answer
// PointersNdFiles.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * readFileName();
FILE * openFile(char *);
int readTotal(FILE *);
int * fillArray(FILE *,int);
void printArray(int *,int);
void sort(int *,int);
double findMean(int *,int);
double findMedian(int *,int);
double findMode(int *,int);
void printResults(double,double,double);
void cleanup(int *);
int main()
{
FILE * fin = NULL;
int total;
int * myArray = NULL;
char *filename;
double mean, median, mode;
filename = readFileName();
fin = openFile(filename);
total = readTotal(fin);
while(!feof(fin))
{
myArray = fillArray(fin, total);
printArray(myArray, total);
sort(myArray, total);
mean = findMean(myArray, total);
median = findMedian(myArray, total);
mode = findMode(myArray, total);
printResults(mean, median, mode);
cleanup(myArray);
myArray = NULL;
total = readTotal(fin);
}// end while
if(filename != NULL)
free(filename);
filename = NULL;
fclose(fin);
fin = NULL;
}// end main
char* readFileName()
{
char fileName[25];
printf("Enter filename: ");
scanf("%s",fileName);
return fileName;
}
FILE * openFile(char *fileName)
{
FILE *fp = NULL;
fp = fopen(fileName,"r");
if(fp == NULL)
{
printf("Cannot find the file %s",fileName);
system("pause");
exit(0);
}
return fp;
}
int readTotal(FILE *fp)
{
int total;
fscanf(fp,"%d",&total);
return total;
}
int * fillArray(FILE *file,int total)
{
int i,*a;
a = (int *)malloc(sizeof(int) * total);
for(i=0;i<total;i++)
{
fscanf(file,"%d",&a[i]);
printf(" %d",a[i]);
}
return a;
}
void printArray(int *myarray,int total)
{
int i;
for(i=0;i<total;i++)
printf(" %d",myarray[i]);
}
void printResults(double mean,double median,double mode)
{
printf(" Mean: %.2lf median: %.2lf mode: %.0lf ", mean, median, mode);
}
//returns mode
double findMode(int *numbers, int total)
{
int temp;
int count=0;
int i;
int j;
int temp2;
int ret;
int count2=0;
for(i=0; i<total;i++)
{
temp=numbers[i];
count=1;
for(j=i+1; j<total; j++)
{
if(temp == numbers[j])
count++;
}
if(count>count2)
{
temp2=temp;
count2=count;
}
}
return temp2;
}
//function returns median
double findMedian(int *numbers,int total)
{
int x;
double median = (double) numbers[total/2];
if(total % 2 == 1)
{
for(x=1; x<total; x++)
{
if(numbers[x]==median)
median=numbers[x];
}
}
else
{
median = (double)(numbers[(total/2)-1] + (numbers[(total/2)]))/2;
}
return median;
}
//function calculates mean of elements
double findMean(int *numbers, int total)
{
int sum=0;
int i;
double mean;
int count=0;
for(i=0;i<total;i++)
{
sum=sum+numbers[i];
}
mean = (sum+0.0)/total;
return mean;
}
//function call to sort array
void sort(int *myArray, int total)
{
int i, j, temp;
for(i=0; i<total-1; i++)
for(j=i+1; j<total; j++)
if(myArray[i] > myArray[j])
{
temp=myArray[i];
myArray[i]=myArray[j];
myArray[j]=temp;
}
}
void cleanup(int *myArray)
{
if (myArray != NULL)
free(myArray);
}