Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Lab 8 - CPS 196 Fall 2007: Observing if Statements & Writing Alternating Harmonic Series, Lab Reports of Computer Science

Instructions and exercises for lab 8 of the cps 196 course in fall 2007. Students are asked to observe the output of a program that computes the average of grades using if statements and then write a program to compute the alternating harmonic series using if-then-else statements. The gradeaverage.c program and instructions on how to run it and use debug to observe variable values.

Typology: Lab Reports

Pre 2010

Uploaded on 09/17/2009

koofers-user-cxi-2
koofers-user-cxi-2 🇺🇸

10 documents

1 / 3

Toggle sidebar

Related documents


Partial preview of the text

Download Lab 8 - CPS 196 Fall 2007: Observing if Statements & Writing Alternating Harmonic Series and more Lab Reports Computer Science in PDF only on Docsity! CPS 196 Lab 8 Fall 2007 Name: ________________________________________________ For the first problem, you will be making some observations of the running of a program, both the output and using debug for the values of variables. Please write down your observations on this lab sheet as indicated. 1. Practicing if statements. A. The following is a program to compute grades, GradeAverage.c. /* GradeAverage.c */ /* This program computes the average of a number of grades, and detects how many * were failing. Grades range from 0 to 100, and below 65 is failing. * This program uses a for loop with an if-then statement. */ #include <stdio.h> int main() { int grade = 0; /* input grade */ int index, numgrades; int gradetotal; /* the sum of all grades so far */ double average; /* compute the average of all grades */ int failurecount = 0; /* keep track of number of failing grades */ /* Give instructions to the user */ printf("Type in the number of grades to be entered (integers between 0 and 100)> \n"); scanf("%i", &numgrades); printf("\n"); /* initialize the total */ gradetotal = 0; for (index = 1; index <= numgrades; index++) { /* Get the input grade */ printf("Give the next grade> "); scanf("%i", &grade); /* add all the grades */ gradetotal = gradetotal + grade; /* test for and count the number of failing grades */ if (grade < 65) { failurecount++; } printf("\n"); } /* compute the average of the grades */ /* use a cast (conversion to a type) to make sure the division is not integer */ average = (double)gradetotal / numgrades; /* give the results */ /* the .2 says to print only 2 digits after the decimal point */ printf("Grade average = %.2lf \n", average); printf("Number of failures = %i \n", failurecount); return(0); } i. Cut and paste this program from the Lecture/Lab page. Run the program with 5 grades. What is the output? ii. Now step through the program using Debug | Step Into and Step Over and enter 5 grades where 3 of them are above 65 and two of them are below 65. Record the values of the variables as they are modified in the program. Variable Value1 Value2 Value3 …
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved