Write a program dataSum. c that takes as command-line arguments an input-file na
ID: 3770126 • Letter: W
Question
Write a program dataSum. c that takes as command-line arguments an input-file name and an output- file name. The input file consists of several lines of space-separated integers. The first line consists a single integer indicating the number of subsequent lines. Each of the following lines starts with an integer specifying how many integers, called data points, will follow it in the line. The program calculates the statistics of the data points in each line. It first opens the input file, reads the data points in each line, calculates their mean and variance, and prints them to a 6-digit precision in the output file. The mean overline x, and variance sigma^2 for integers x_1, x_2, .., x_n are calculated as For example, for input file the first line indicates that there are 2 data lines. For the second line, the first integer, 3, indicates that the line has three data points, 0, 1, and 8. The mean for this line will be: overline x = (0 + 1+ 8)/3 = 3 and the variance is sigma^2 = ((0 - 3)^2 + (1-3)^2 + (8-3)^2)/3 = 12.666667. Therefore the first line of the output file should be mean = 3.000000, variance = 12.666667. Similarly, the second line of the output file should be mean = 5.500000, variance = 0.250000. If the input-file or output-file are missing from the command line arguments the program prints "Missing Input!" and terminates. You can assume that the input file exists and has the right format. Your program must call the following functions: void statistics(int a[],int length, double *mean, double *variance); calculates the mean and variance of the elements in int a [ ] and change *mean and *variance correspondingly. int main(int argc, char *argv []) opens the input file argv [1 ], calls statistics to calculate the mean and variance of data in each line, and prints the statistics of each line in the output file argv [2].Explanation / Answer
#include <stdio.h>
#include <string.h>
void stattistics(int a[], int length, double *mean, double *variance) {
int i =0;
int total = 0;
while(i < length) {
total += a[i++];
}
*mean = (double) total / length;
i =0;
double vartotal = 0;
double diff = 0;
while(i < length) {
diff = a[i++] - *mean;
vartotal += diff * diff;
}
*variance = vartotal / length;
}
int main(int argc, char * argv[]) {
char* inFile = "D:/ravi/Cheg/meanAvgFile.txt"; //argv[1];
char* outFile = argv[2];
//read from file
FILE * inputFile;
inputFile = fopen(inFile, "r");
//printf("reading file ");
int line_size = 1024; //change this size accordig to your need
char str[line_size];
if (inputFile == NULL) {
printf("File not found ");
return -1;
}
//char** curArr = NULL;
int count = 0;
int noOfLines = 0;
double mean = 0, variance = 0;
while (fgets(str, line_size, inputFile) != NULL) {
//printf("%s ", str);
if (count == 0)
noOfLines = (int) atoll(str);
else {
const char s[2] = " ";
char *token;
int i = 0;
/* get the first token */
token = strtok(str, s);
int noOfIntegers = (int) atoll(token);
int array[noOfIntegers];
/* walk through other tokens */
printf("No of integers: %d ",noOfIntegers);
while (token != NULL && i < noOfIntegers) {
token = strtok(NULL, s);
array[i] = (int) atoll(token);
printf(" %d ",array[i]);
i++;
}
stattistics(array, noOfIntegers, &mean, &variance);
printf(" Mean : %f Variance: %f ", mean, variance);
}
count++;
//including the line contianing no of lines
if (count == noOfLines+1)
break;
}
}
--------------output-----------------
No of integers: 3
0 1 8
Mean : 3.000000 Variance: 12.666667
No of integers: 2
5 6
Mean : 5.500000 Variance: 0.250000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.