


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Lab; Class: Introduction to Computer Programming; Subject: Computational Science; University: Syracuse University; Term: Fall 2007;
Typology: Lab Reports
1 / 4
This page cannot be seen from the preview
Don't miss anything!



CPS 196 Lab 19 Fall 2007
I. A. Open Visual Studio and add the program that we looked at in class:
/*averagefromfile.c / / Compute the average temperature and the high temperature for a month from data stored in a file. Record the answers on the monitor and in a file */
#include<stdio.h> #define sentinel 1000 int main(void) { /* declare variables for the file streams / FILE myin; FILE myout; int count; /for counting the items/ double avg; /for the sum of the temps / int temp; / the temperature read from file / int sum; / the sum of the temperatures / int high; / the high temperature */
/open the files/ myin=fopen("Jan05temps.txt","r"); /* input file / / If this file does not exist, there will be a run-time error. */
myout=fopen("statdataJan.txt","w"); /* output file / / If this file does not exist, it will be created (in the project folder.) If this file already exists, it will be overwritten. */
/* initialize variables / count = 0; sum = 0; high = -100; / an unreasonably low value */
/* read first piece of data */ fscanf(myin, "%d", &temp);
/* process each valid temperature / while (temp != sentinel) { count++; sum += temp; / same as sum = sum + temp; / if (temp > high) high = temp; / Update high if this temp is highest so far. */
fscanf(myin, "%d", &temp); /* read next temperature / } / end of while */
/* only got here when temp == sentinel / / do final calculations / avg = (double)sum/count; / print to the screen / printf("The average temperature for the month "); printf("was %.1lf degree Farenheit\n",avg); printf("The highest temperature was %d", high); printf(" degrees Farenheit\n"); / print to the file / / print column labels / fprintf(myout, "Average Temperature "); fprintf(myout, "High Temperature\n\n"); fprintf(myout, "%19.2f%21d\n",avg,high); /right justified/ fprintf(myout, "%12.2f%22d\n",avg,high); /centered (the decimal pt)/ fprintf(myout, "%-19.2f %-16d\n",avg,high); /left justified*/
/* close the files */ fclose(myin); fclose(myout); return(0); }
B. Save the input file, Jan05temps.txt, from the web page to the same directory as your .c
programs.
II. In addition to finding the highest temperature for the month, we also want to find the lowest. Do
a Save As and call the new file avgHighLow.c. This way, as you modify the program, you will still have the old version to use as a reference.
A. Change the program so that it will also compute the lowest temperature. Introduce a new
variable to contain the lowest temperature and don’t forget to write this to both the monitor output and the file output.
Run this program on the Feb05temps.txt input file. What is the lowest temperature?
B. Now change the program so that all the temperatures can also be written to the output file.
To do this,
When you are done, print out your program and the output file with the temperatures and hand it in
with your lab sheet.