Professional Documents
Culture Documents
H&K Chapter 3
Instructor - Andrew S. OFallon
CptS 121 (January 27, 2014)
Washington State University
Outputs
C. Hundhausen, A. OFallon
Initial algorithm
Refined algorithm
C. Hundhausen, A. OFallon
Implementation
#include <stdio.h>
int main (void)
{
int num_credits1 = 0, num_credits2 = 0, num_credits3 = 0;
double grade_point1 = 0.0, grade_point2 = 0.0, grade_point3 = 0.0,
weighted_credits = 0.0, total_num_credits = 0.0, gpa = 0.0;
/* Get the grade points and credits */
printf ("Please enter your grade point for computer science course 1: ");
scanf ("%lf", &grade_point1);
printf ("Please enter the number of credits possible for computer science course 1: ");
scanf ("%d", &num_credits1);
printf ("Please enter your grade point for calculus course 2: ");
scanf ("%lf", &grade_point2);
printf ("Please enter the number of credits possible for calculus course 2: ");
scanf ("%d", &num_credits2);
printf ("Please enter your grade point for physics course 3: ");
scanf ("%lf", &grade_point3);
printf ("Please enter the number of credits possible for physics course 3: ");
scanf ("%d", &num_credits3);
C. Hundhausen, A. OFallon
C. Hundhausen, A. OFallon
Notes on Example
Its redundant!
C. Hundhausen, A. OFallon
C. Hundhausen, A. OFallon
Compute gpa
get_grade_point()
compute_weighted_credits ()
get_credits ()
compute_gpa ()
C. Hundhausen, A. OFallon
display_gpa ()
Functions (1)
f(x) = x2 - 4x + 4
In C, we pass the value of x into a function called f and
get a result back
C. Hundhausen, A. OFallon
Functions (2)
10
C. Hundhausen, A. OFallon
Functions (3)
Return
value
11
C. Hundhausen, A. OFallon
Function
name
Functions (4)
12
C. Hundhausen, A. OFallon
Functions (5)
Definition of get_grade_point ()
13
C. Hundhausen, A. OFallon
Functions (6)
Definition of get_credits ()
14
C. Hundhausen, A. OFallon
Functions (7)
Definition of compute_weighted_credits ()
15
C. Hundhausen, A. OFallon
Functions (8)
Definition of compute_gpa ()
16
C. Hundhausen, A. OFallon
Functions (9)
Definition of display_gpa ()
/* Outputs the calculated gpa to the screen */
17
C. Hundhausen, A. OFallon
Functions (10)
18
C. Hundhausen, A. OFallon
Functions (11)
19
C. Hundhausen, A. OFallon
Formal
parameter
Functions (12)
Procedural abstraction
The
20
C. Hundhausen, A. OFallon
Functions (13)
Reuse
Recall
Why
21
C. Hundhausen, A. OFallon
Functions (14)
Why
Testing
Allows
22
C. Hundhausen, A. OFallon
C Math Functions
23
C. Hundhausen, A. OFallon
C Math Functions
24
C. Hundhausen, A. OFallon
Next Lecture
25
C. Hundhausen, A. OFallon
References
J.R.
26
C. Hundhausen, A. OFallon
Collaborators
Chris
27
Hundhausen
C. Hundhausen, A. OFallon