Anonymous
2012-12-10 16:52:51 UTC
I think it has something to do with the way i'm reading in the file. I'm using Microsoft Visual Studio 2010 but trying to write in C language instead of C++ if that has anything to do with it too. If anyone could help me to see what I'm doing wrong I'd greatly appreciate it. Here is what I have:
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
using namespace System;
//FUNCTIONS
void find_average(int *sum, int *total, int *average);
int main(int argc, char *argv[])
{
int *arr;
int sum = 0;
//Total is the total number of elements found, not a sum
int total = 0;
int average = 0;
//Make sure there are two arguments, one execution and one input file
if ( argc!= 2 )
{
printf("\nError. Not enough or too many arguments.\n");
printf("There must be the execution and input file.\n");
exit ( EXIT_FAILURE );
}
//Declare a file pointer to open the input file from the command line
FILE *in = fopen( argv[1], "r" );
//Check to see if the file was found
if ( in == NULL )
{
printf("\nInput file not found");
exit (EXIT_FAILURE);
}
//Read in the values from the input file
while ( !feof(in) )
{
if (fscanf(in, "%d", &arr[total]) > 0)
{
//Sum all of the elements and incriment total elements found
sum = sum + arr[total];
total++;
}
}
//Close the file
fclose (in);
//Find the average
find_average(&sum, &total, &average);
//Print results
printf("\nThere were %d entries found.\n", total);
printf("The sum is %d\n", sum);
printf("The average is %d\n", average);
return 0;
}
void find_average(int *sum, int *total, int *average)
{
*average = *sum / *total;
}