



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
● Pseudocode is an artificial and informal language. that helps you develop algorithms.
Typology: Exercises
1 / 5
This page cannot be seen from the preview
Don't miss anything!
Pseudocode
Pseudocode
Guide for Pseudocode
/******************************************************************
******************************************************************/
/*****************************************************************
*****************************************************************/
If statement
scanf(“%d”,&x); if ((x < 0)||(x>5)){ printf(“Please enter an integer between 0 and 5.\n”); printf(“Try again\n”); scanf(“%d”,&x); }
Use & to read integer into variable
Logical OR Operator
if (score >= 60){ printf(“You Passed\n); }else{ printf(“You Failed\n”); };
Logical Expressions
((x <= 5)&&(x >= 0)) and (y != 5) are examples of logical expressions.
Logical AND Operator
Logical NOT Equal Operator
Relational Operators
== Equality (don’t confuse with the
assignment operator, =)
!= not equal
greater thant th
< less than
= greater than or equal
<= less than or equal
Compound Conditions
If-else statement
if (score == 100) printf(“Your grade is an A+\n); else if (score >= 95) printf(“Youri grade iis an A\n”);
else if (score >= 90) printf(“Your grade is an A-\n”); else printf(“Your grade is lower than an A-\n”); . . .
The Switch Statement
z Multiple-selection structure
z Good for algorithms containing a
series of decisions
z Consists of
Class Average Example
z Problem statement: A class of ten students
took a quiz. The grades (integers in the range 0 to 10) for this quiz are available to you. Determine the class average on the quiz.
z class average = (sum of grades / total
students)
z Main algorithm:
Pseudo-code Algorithm
Note: This is an example of a counter-controlled loop (loop is executed a fixed number of times, controlled by a counter)
C Program For the Example
/* average program, counter controlled repetition / #include <stdio.h> int main() { / note meaningful variable names / int counter, grade, total, average; / INITIALIZATION phase */ total = 0; counter = 1;
include stdio.h for printing and reading variables beginning of main program
/* PROCESSING phase: loop for average calculations/ while (counter <= 10) { printf("Enter grade: "); scanf("%d", &grade); total = total + grade; counter = counter + 1; } / end while / / TERMINATION phase */ average = total / 10; printf("Class average is %d\n", average); return 0; }
Sentinel-controlled loops
z Sentinel value : a special input value to indicate the end of data entry
z The user must enter the sentinel value to indicate that all data items have been entered
z Used in cases of indefinite repetition
Initialize total to zero Initialize counter to zero Input the first grade while (the user has not yet entered the sentinel): Add this grade into the running total Add one to the grade counterAdd one to the grade counter Input the next grade (possibly the sentinel) endwhile If the counter is not equal to zero: Set the average to the total divided by the counter Print the average else Print “No grades were entered” endif
double average = 0.0; /* new data type / int counter = 0; int grade =0; int total = 0; / INITIALIZATION phase / printf("Enter grade, -1 to end: "); scanf("%d", &grade); /Loop to perform summation*/ while (grade != -1) { total = total + grade; counter = counter + 1;
/* average program, sentinel-controlled repetition */ #include <stdio.h> int main(){ new data type (double) initializers for variable declarations
printf("Enter grade, -1 to end: "); scanf ("%d", &grade); } /* TERMINATION PHASE / if (counter != 0) { / Casting to avoid integer division/truncation / average = ((double) total) / counter; printf("Class average is %.2f\n", average); } else { printf("No grades were entered\n"); } return 0; / program ended successfully */
}
type cast precision control for format specifier
The for Loop Construct
z Handles some of the counter-controlled repetition details z Example:
for (counter = 0; counter < 10; counter++) { printf("%d\n", counter); }
z for loop has three parts: z initializer z condition z update z Any for loop could be re-written as a while loop (counter- controlled repetition).
for loop versus while loop
for (expression_1; expression_2; expression_3) { statement; }
IS THE SAME AS:IS THE SAME AS:
expression_1; while (expression_2) { statement; expression_3; }
More for Loop Examples
do/while Loop Construct
z Tests loop-continuation at the end of loop body z Syntax: do { statement1; statement2;; } while (condition); z Example: What does this print? int counter = 1; do { printf("%d", counter); counter++; } while (counter <= 10); /Note semicolon/
do/while Versus while Loop
condition?
False
True
Action
Action
True
while/do do/while
condition?
False
True
for & while statements #define NUM 5
main(){ int i, a[NUM]={4, 2, 7, 3, 9};
/* Print out the elements of array a[] */ for(i=0; i<NUM; i++){ printf(“a[%d]=%d\n”,i,a[i]); }
Define global constants in Caps Only have to change one place no semi colon at end
/* Read in integers into the array a[] */ i=0; while (i<NUM) { scanf(“%d”,&(a[i])); i++; }
/* Print out elements of array a[] */ i=0; do { printf(“a[%d]=%d\n”,i,a[i]); i++; }while(i<NUM); }
Use & and () to read in data into array
Use do-while structure to execute command before checking condition.