HW5_ruzmora

HW5_ruzmora - Page 2 /* Chapter 5 Problem 23 */ /* This...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 /* Chapter 5 Problem 22 */ /* This program calculates the average power output and the number of days with greater-than-average power output */ #include <stdio.h> #define N 56 #define FILENAME "power1.txt" int main (void) { /* Declare and initialize variables */ int k, count=0 double p[N], ave_pwo, sum=0 FILE *pw1 /* Open file, read data into an array */ pw1 = fopen(FILENAME,"r") if (pw1 == NULL) printf("Error opening input file. \n") else { /*Input and process data*/ for (k=0 k<=N-1 k++) { sum += p[k] } /*Compute average and count values that are greater than the average*/ ave_pwo = sum/N for (k=0 k<=N-1 k++) if (p[k] > ave_pwo) count++ /* Print count and close file */ printf("The average power output was calculated to be: %6.3f \n",ave_pwo) printf("\n") printf("Days greater than the average: %d \n",count) fclose(pw1) } /*Pause program */ getchar() getchar() /* Exit Program */ return 0 }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sheet1
Background image of page 2
Background image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Page 2 /* Chapter 5 Problem 23 */ /* This program prints the day of the week and number of the week on which the minimum power output occurred */ #include &amp;lt;stdio.h&amp;gt; #define NROWS 8 #define NCOLS 7 #define FILENAME &amp;quot;power1.txt&amp;quot; int main() { /* Define and declare variables*/ int pw[NROWS][NCOLS],w,d,min=99999 FILE *power1 power1 = fopen(FILENAME,&amp;quot;r&amp;quot;) if (power1 == NULL) printf(&amp;quot;Error opening input file. \n&amp;quot;) else { /*Assign data points into an array */ for(w=0 w&amp;lt;=NROWS-1 w++) for(d=0 d&amp;lt;=NCOLS-1 d++) { fscanf(power1,&amp;quot;%d&amp;quot;,&amp;amp;pw[w][d]) /*Find minimum*/ if(pw[w][d]&amp;lt;min) min=pw[w][d] } printf(&amp;quot;Day(s) with the minimum power consumption\n&amp;quot;) for(w=0 w&amp;lt;=NROWS-1 w++) for(d=0 d&amp;lt;=NCOLS-1 d++) if(pw[w][d]==min) printf(&amp;quot;Week %d Day %d\n&amp;quot;,w+1,d+1) /*Close file*/ fclose(power1) } /*Pause program*/ getchar() getchar() Sheet1 Page 3 /*Exit Program*/ return 0 }...
View Full Document

Page1 / 3

HW5_ruzmora - Page 2 /* Chapter 5 Problem 23 */ /* This...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online