Lecture2 PDF
Lecture2 PDF
1
Example Documentation If statement Use & to read integer into variable
/*****************************************************************
scanf(“%d”,&x);
* *
* Programmer: Gary Christensen *
if ((x < 0)||(x>5)){
* Date: 2/17/04 * printf(“Please enter an integer between 0 and 5.\n”);
* Name: quicksort * printf(“Try again\n”);
* Function: This function sorts an array of integers from * scanf(“%d”,&x);
* smallest to largest value. * }
* Algorithm: This subroutine sorts a list of integers using *
Logical OR Operator
* recursion. The first element of the list is moved to *
* it
its fi
final
l position
iti i
in th
the li
list
t and
d a sublist
bli t of
f numbers
b *
if (score >= 60){
* less than this number and a sublist of numbers greater *
* than this number is created. Each sublist is then *
printf(“You Passed\n);
* sorted by passing it to quicksort again. The * }else{
* termination condition for the recursion are as * printf(“You Failed\n”);
Logical AND Operator
* follows: A one element list is sorted by definition and * };
* is returned. *
* Input: ListOfInts - a pointer to an array of integers to be * Logical NOT Equal Operator
*
*
sorted.
size - the number of integers in the list to be sorted.
*
*
Logical Expressions
* Output: ListOfInts - a pointer to an array of sorted integers * ((x <= 5)&&(x >= 0)) and (y != 5) are examples of logical
* * expressions.
*****************************************************************/
> greater
t than
th if ((score > 100) || (score < 0)){
printf(“Score is invalid\n”);
< less than }
>= greater than or equal
if (!((score <= 100) && (score >= 0))){
<= less than or equal printf(“Score is invalid\n”);
}
2
Example program to count number of
while (grade != ’\n’) {
A, B, and C grades Counts both lower and upper
switch(grade) { case A.
/* Counting A, B, and C grades */ case ’a’:
#include <stdio.h> case ’A’: ++aCount;
int main() { break; /* denotes end of this case */
char grade; case ’B’: ++bCount;
int aCount =0, bCount = 0, cCount=0; break;
case ’C’: ++cCount;
printf("Enter the letter grades A, B, or C. "); break;
printf("Enter the ’return’ character to end.\n"); defa lt /* catch all othe
default: other cha
characters
acte s */
scanf("%c",&grade); printf("Incorrect input.\n");
} /* end of switch */
scanf("%c",&grade);
} /* end of while */
3
Class Average Example Pseudo-code Algorithm
Set total to zero
z Problem statement: A class of ten students
Set grade counter to one
took a quiz. The grades (integers in the range
0 to 10) for this quiz are available to you. while (grade counter is less than or equal to ten):
Determine the class average on the quiz. Input the next grade
z class average = (sum of grades / total Add the grade into the total
students) Add one to the grade counter
z Main algorithm: endwhile
input each of the grades Set the class average to the total divided by ten
perform the averaging calculation Print the class average
print the result Note: This is an example of a counter-controlled loop (loop is executed a fixed
number of times, controlled by a counter)
4
The for Loop Construct for loop versus while loop
z Handles some of the counter-controlled repetition details
z Example: for (expression_1; expression_2; expression_3) {
for (counter = 0; counter < 10; counter++) { statement;
}
printf("%d\n", counter);
} IS THE SAME AS: