Docsity

Lab 4: C Programming - Loops and Control Structures, Lab Reports of Computer Science

Toggle sidebar
bg1
Lab 4: Loops!
Todayโ€™s lab has us playing with loops.
Problem 0:
Give the output from the following code:
a) int c ou nt = 0 ;
while( cou nt <5)
{
p r i n t f ( โ€%d\nโ€ , co un t ) ;
cou n t ++;
}
p r i n t f ( โ€%d\nโ€ , c ou nt ) ;
Output:
b) int c ou nt = 5 ;
int i = 0;
while( cou nt >0)
{
p r i n t f ( โ€%d %d\n โ€ , i , co un t ) ;
i ++;
countโˆ’โˆ’;
}
p r i n t f ( โ€%d %d\n โ€ , i , co un t ) ;
Output:
c) int c ou nt = 5 ;
int i = 0;
while( cou nt >0)
{
i = 0;
while( i <co un t )
{
p r i n t f ( โ€%d %d\n โ€ , i , co un t ) ;
i ++;
}
countโˆ’โˆ’;
}
p r i n t f ( โ€%d %d\n โ€ , i , co un t ) ;
Output:
d) int c ou nt = 5 ;
int i ;
while( cou nt >0)
{
for( i =0 ; i <5 ; i++)
{
p r i n t f ( โ€%d %d\n โ€ , i , co un t ) ;
}
CPS 196: Introduction to C (Summer 2009) Page 1
pf2

Partial preview of the text

Download Lab 4: C Programming - Loops and Control Structures and more Lab Reports Computer Science in PDF only on Docsity!

Lab 4: Loops!

Todayโ€™s lab has us playing with loops.

Problem 0:

Give the output from the following code:

a) int count = 0 ; while ( count < 5 ) { p r i n t f ( โ€%d\nโ€ , count ) ; count++; } p r i n t f ( โ€%d\nโ€ , count ) ;

Output:

b) int count = 5 ; int i = 0 ; while ( count > 0 ) { p r i n t f ( โ€%d %d\nโ€ , i , count ) ; i ++; count โˆ’โˆ’; } p r i n t f ( โ€%d %d\nโ€ , i , count ) ;

Output:

c) int count = 5 ; int i = 0 ; while ( count > 0 ) { i = 0 ; while ( i < count ) { p r i n t f ( โ€%d %d\nโ€ , i , count ) ; i ++; } count โˆ’โˆ’; } p r i n t f ( โ€%d %d\nโ€ , i , count ) ;

Output:

d) int count = 5 ; int i ; while ( count > 0 ) { for ( i =0; i < 5 ; i ++) { p r i n t f ( โ€%d %d\nโ€ , i , count ) ; }

CPS 196: Introduction to C (Summer 2009) Page 1

Lab 4: Loops!

count โˆ’โˆ’; } p r i n t f ( โ€%d %d\nโ€ , i , count ) ;

Output:

Problem 1: Convert the following program from using while-loop to use a for-loop:

int i =0 ; int j ; while ( i < 5 ) { j = 0 ;

while ( j < 5 ) { p r i n t f ( โ€%d x %d = %d\nโ€ , i , j , i โˆ— j ) ; j ++; } i ++; }

Problem 2 : Write a program that computes a grade average in several steps:

(1) First the program asks the user for the number of grades to enter. (2) Second the program prompts the user for the each grade individually. (3) Third the program displays the average of the numbers that are entered.

Problem 3: Write a program that gets an integer,n, from the user and then calculates n! or n factorial which is: n ร— (n โˆ’ 1) ร— (n โˆ’ 2)... ร— 1

Problem 4: Write a program that acts as a simple printing calculator (Exer- cise: 4.0) The program should accept input in the form: number operator. Where valid operators are: +, -, *, /, %, S, E. Were E means exit and S means to set the accumlator to typed in number. The arithmetic operators are performed on the con- tents of the accumulator with the number that was keyed in acting as the second operand. Check for division by zero and unknown operators.

CPS 196: Introduction to C (Summer 2009) Page 2