Lab 19: Open Visual Studio - Introduction to Computer Programming | CPS 196, Lab Reports of Computer Science

Material Type: Lab; Class: Introduction to Computer Programming; Subject: Computational Science; University: Syracuse University; Term: Fall 2007;

Typology: Lab Reports

Pre 2010

Uploaded on 08/09/2009

koofers-user-of8
koofers-user-of8 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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 */
pf3
pf4

Partial preview of the text

Download Lab 19: Open Visual Studio - Introduction to Computer Programming | CPS 196 and more Lab Reports Computer Science in PDF only on Docsity!

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,

  • Add an array variable that has the number of elements as there are temperatures in the file, but not including the sentinel.
  • During the loop that reads input temperatures, save each temperature, except the sentinel, in the array. Note that the array positions will be numbered from 0 to n-1, where n is the number of temperatures. Can you use the value of the count variable as the array position? If not, figure out what number to add or subtract from this variable.
  • Add a loop, in the file output section, that will loop over every element of the temperatures array and write those elements to the file. Choose whether to write out all the elements on one line, or to put them on separate lines.

When you are done, print out your program and the output file with the temperatures and hand it in

with your lab sheet.