Write the code in C programming , detailed instructions are attached: Best Pract
ID: 3736943 • Letter: W
Question
Write the code in C programming , detailed instructions are attached:
Best Practices for Code Style Your submissions must observe the following guidelines. Submissions which do not meet the guide- lines will have marks deducted, even if they function correctly (although some of the guidelines, such as those relating to uninitialized memory, might also affect the ability of the program to worlk correctly on a reliable basis, which could result in a deduction for both style and function). » Submitted files must be syntactically correct and named according to the assignment speci- Every source file must contain a comment (at or near the top of the file) with your name, » The goto statement is not permitted in any assignment submissions. Instead, your code fication. student number, the date the code was written and a brief description of the program. should use structured control flow elements, like if, for and while. . Global variables (data variables created outside of the scope of a function) are not permitted, except when explicitly allowed by the assignment. For this assignment, no global variables are permitted, except for const variables if needed. » Every function with a non-void return type must return a value. » Uninitialized variables may not be used before being assigned a value. » Every file opened (by functions such as fclose) must be explicitly closed (by a call to fclose).Explanation / Answer
Create a directory "fileOperation/" in your workspace and place an input file "input_data.txt" along with source code "daily_avg.c". Compile the code and run. It will create an output file "daily_averages_summary.txt" with result and it will print the result in stdout as well.
/********************/input_data.txt (update this file as per your data sheet)
3 11 12 05 1 8.7
2 12 12 05 1 2.7
4 11 10 05 1 5.7
2 12 12 05 1 1.7
4 5 12 05 1 -3.7
2 12 12 05 1 4.7
3 1 12 05 1 86.7
/************************/
//============================================================================
// Name : daily_avg.c
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int month,day,hour,minute,st_no;
float temp=0.0;
int read_count=1;
float dataArray[12][31]={0.0}; //2d array to store temperature
int countArray[12][31]={0}; //2d array to store valid count
FILE* ifile;
FILE* ofile;
ifile=fopen("input_data.txt","r"); //open input file
ofile=fopen("daily_averages_summary.txt","w");//open output file
while(read_count>0)//loop till end of file read
{
read_count=fscanf(ifile,"%d %d %d %d %d %f", &month,&day,&hour,&minute,&st_no,&temp); //read data from inputfile
if((month>0&&month<=12)&&(day>0&&day<=31))
{
dataArray[month-1][day-1]+=temp; //fill the data array
countArray[month-1][day-1]++; //fill the count array
}
}
int i,j;
for(i=0;i<12;i++)
{
for(j=0;j<31;j++)
{
if(countArray[i][j]>0)
{
float avg=dataArray[i][j]/countArray[i][j]; //compute average
printf("%d %d %.2f ", i+1,j+1,avg); //print
fprintf(ofile,"%d %d %.2f ", i+1,j+1,avg); //write to output file
}
}
}
fclose(ifile); //close input file
fclose(ofile); //close output file
return 0;
}
/****************/daily_averages_summary.txt
2 12 3.03
3 1 86.70
3 11 8.70
4 5 -3.70
4 11 5.70
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.