need some help with last part of arrays
The last part of this project is that I have to read the numbers from a text file. I've looked at all the posts and FAQ's but they haven't really help me since, I'm not allowed to use sizeof, malloc or anything like that for this project.
I have to use fscanf to put the list of numbers from the text file. and the size of the array back to main. Part of it is I dont' know what to make with my for loop, the other part i don't know. As I said on my other post the array size is not set, instructor will test it with a random list he has of 1-100 numbers though. When I get the array back to main and the size everything I'll be done. Thank you.
Code:
// Compiler Includes
#include <stdio.h>
#include <math.h>
// Project Includes
// Constants
#define MAX_ARRAY_SIZE 100
#define MAX_STRING_LENGTH 80
// Function Prototypes
void PrintArray(double inputArray[], int inputArraySize);
int GetInput( double OutPutList[]);
double MaxNum(const double inputArray[], int inputArraySize);
double MinNum(const double inputArray[], int inputArraySize);
double MenNum(const double inputArray[], int inputArraySize);
double MedNum(const double inputArray[], int inputArraySize);
double StdDev(const double inputArray[], int inputArraySize);
double SumNum(const double inputArray[], int inputArraySize);
void SrtNum(const double inputArray[], int inputArraySize);
void disply(double max, double min, double mean, double med, double std);
void bubblesort( double a[], int sizer);
////////////////////////////////////////////////////////////////////////////////
int main(void) {
int numlist = 0;
double testA[MAX_ARRAY_SIZE] = {0};
double highestNum = 0;
double lowestNum = 0;
double MeanNum = 0;
double MedianNum = 0;
double StdDevNum = 0;
double SumNumbers = 0;
numlist = GetInput(testA);
printf("numbers of numbers %f\n", numlist);
highestNum = MaxNum(testA, numlist);
lowestNum = MinNum(testA, numlist);
MeanNum = MenNum(testA, numlist);
MedianNum = MedNum(testA, numlist);
StdDevNum = StdDev(testA, numlist);
disply(highestNum, lowestNum, MeanNum, MedianNum, StdDevNum);
return 0;
} // end main
int GetInput(double OutPutList[]) {
int numbersFound = 0;
int value = 0;
char inputFileName[MAX_STRING_LENGTH] = {'\0'};
FILE *inputFileHandle = NULL;
double currentNumber[] = {0};
int endOfFile = 0;
int i = 0;
//begin function
printf("Enter file name: ");
gets(inputFileName);
inputFileHandle = fopen(inputFileName, "r");
if (inputFileHandle == NULL) {
printf("ERROR: The file %s could not be found!\n", inputFileName);
} // end if
else {
endOfFile = fscanf(inputFileHandle, "%lf", ¤tNumber[value]);
// read text numbers put them into array
// having horrible time here figuring out why my array won't build
while (endOfFile != EOF) {
for (i = 0; i < ?????; i++) {
OutPutList[value] = fscanf(inputFileHandle,"%lf", ¤tNumber[value]);
numbersFound++; // increase number counter
} // end for loop
endOfFile = fscanf(inputFileHandle, "%lf", ¤tNumber[value]);
} // end while
// Close input file
fclose(inputFileHandle);
} // end else
return numbersFound;
} // end function
double MaxNum(const double inputArray[], int inputArraySize) {
int i = 0;
double max = inputArray[0]; // assume max number
for(i = 0; i < inputArraySize; i++) { // for loop brace
if (inputArray[i] > max) { // if brace
max = inputArray[i];
} // if loop end
} // for loop end
return max;
} // end function MaxNum
double MinNum(const double inputArray[], int inputArraySize) {
int i = 0;
double min = inputArray[0]; // assume max number
for(i = 0; i < inputArraySize; i++) { // for loop brace
if (inputArray[i] < min) { // if brace
min = inputArray[i];
} // if loop end
} // for loop end
return min;
} // end function MinNum
double SumNum (const double inputArray[], int inputArraySize) {
int i = 0;
double sum = 0;
for(i = 0; i < inputArraySize; i++) {
sum = inputArray[i]+ sum;
}
return sum;
} // end function SumNum
double MenNum(const double inputArray[], int inputArraySize) {
//int i = 0;
double mean = 0; // assume max number
double total = 0;
total = SumNum(inputArray, inputArraySize);
mean = total/inputArraySize;
return mean;
} // end function MenNum
void bubblesort( double a[], int sizer) {
int pass;
int j;
double hold;
for(pass = 1; pass < sizer; pass++) {
for (j = 0; j < sizer-1; j++) {
if (a[j] > a[j + 1]) {
hold = a[j];
a[j] = a[j + 1];
a[j + 1] = hold;
} // end if
} // end inner for
} // end outer for
} // end bubble sort function
double MedNum( const double inputArray[], int inputArraySize) {
double medianNum = 0;
double bubbleArray[MAX_ARRAY_SIZE] = {0};
int k = 0;
for(k = 0; k < inputArraySize; k++) {
bubbleArray[k] = inputArray[k];
}
bubblesort(bubbleArray, inputArraySize);
if(inputArraySize%2==0) { // if array size modulated by 2 leaves no remainder
// array size is even
medianNum = (bubbleArray[(inputArraySize)/2] + bubbleArray[inputArraySize/2-1])/2;
} // end if
else // else array size is odd
{
medianNum = bubbleArray[inputArraySize/2];
} // end else
return medianNum; // return the median number
} // end function MedNum
double StdDev(const double inputArray[], int inputArraySize) {
int i = 0;
double mean = 0;
double std = 0;
double x = 0;
mean = MenNum(inputArray, inputArraySize);
for (i = 0; i < inputArraySize; i++) {
x += (inputArray[i]-mean)*(inputArray[i]-mean);
}
x = x/(inputArraySize);
std = sqrt(x);
return std;
} // end function StdDev
void disply(double max, double min, double mean, double med, double std) {
printf("The higest value is: %f\n", max);
printf("The lowest value is: %f\n", min);
printf("The mean value is: %f\n", mean);
printf("The median value is: %f\n", med);
printf("The standard deviation is: %f\n", std);
} // end function disply
void PrintArray(double inputArray[], int inputArraySize) {
int i = 0;
for (i = 0; i < inputArraySize; i++) {
printf("The numbers in the array are: %f\n", inputArray[i]);
}
} // end function PrintArray