2
54
5
12538
notice that the lines that are under the single digit lines have the same number of characters as the digit above it. (the actual file can have different numbers than the ones above)
The goal is to take in the longer strings of numbers and output a table with two rows, the first row representing the first multi digit row, the second representing both of the multi digit rows (for example, for the sum column, row one would display 9, and row 2 would display 28) The columns should display the categories of information, those being ( Total number of elements[2 and7 in this case], the sum of the values, the range[4 to five and 1 to 8], the mean, the variance, and the standard deviation. Printed out, the table should look like this:
_________________________________________________________
|# of data| sum | range | mean | variance | standard deviation|
_________________________________________________________
| 5| 27| 3 to 7| 5.400| 2.300| 1.517 |
_________________________________________________________
| 7| 34| 2 to 7| 4.857| 3.143| 1.773 |
_________________________________________________________
so far, this is the code that I have.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#define STRLEN 256
#pragma warning(disable: 4996)
int main(int argc, char *argv[])
{
FILE * inFileHandle = NULL;
FILE * outFileHandle = NULL;
char filename[STRLEN];
int numberofdata = 0;
int i = 0;
int n = 0;
int sum = 0;
int nTotal = 0;
int square = 0;
int sumofsquares = 0;
double mean = 0.0;
double variance = 0.0;
double standarddeviantion = 0.0;
puts("Standard Deviation Program");
printf("__________________________\n");
printf("Working model written by Nathan Jernigan\n");
if(argc > 1)
{
strncpy(filename, argv[1], STRLEN);
}
else
{
printf("Please enter the name of the File from which to read the data:", filename);
fgets(filename, STRLEN, stdin);
if(filename[strlen(filename) - 1] == '\n')
filename[strlen(filename) - 1] = '\0';
else
while(getchar() != '\n');
}
inFileHandle = fopen(filename, "r");
if(inFileHandle == NULL)
{
printf("File %s could not be opened.\n"
"Press Any Key to Continue", filename );
getch();
return EXIT_FAILURE;
}
if(argc > 2)
{
strncpy(filename, argv[2], STRLEN);
}
else
{
puts("Please enter the name of the file to write the report: ");
gets(filename);
}
outFileHandle = fopen(filename, "w");
if(outFileHandle == NULL)
{
printf("File %s could not be opened.\n"
"Press Any Key to Continue", filename );
getch();
return EXIT_FAILURE;
}
while(fscanf(inFileHandle, "%d", &numberofdata) == 1)
{
for(i = 0; i < numberofdata; i++)
{
fscanf(inFileHandle, "%d", &n);
nTotal++;
sum += n;
mean = (sum / nTotal);
square = n^2;
sumofsquares += n^2;
}
variance = ((sumofsquares - (sum^2 / nTotal)) / (nTotal - 1));
standarddeviantion = sqrt(variance);
fprintf(outFileHandle, "%d ", nTotal);
fprintf(outFileHandle, "%d ", sum);
fprintf(outFileHandle, "%5.3lf ", mean);
fprintf(outFileHandle, "%5.3lf ", variance);
fprintf(outFileHandle, "%5.3lf ", standarddeviantion);
}
fclose(inFileHandle);
fclose(outFileHandle);
puts("\nPress Any Key to Continue");
getch();
return EXIT_SUCCESS;
}
Take note that the sumofsquares function is not working properly, thus, neither is the variance section. Also note, that I do wish the table to have the tabular lines.


Sign In
Create Account

Back to top









