Objective: Scope: Exercises:

advertisement
ICS 103 – Computer Programming in C
Summer Semester 2008 (073)
Lab # 5 (Repetition)
Objective:
Learn the Repetition Structures.
Scope:
The student should know the following:
1. for loop
2. while loop
3. do-while loop
Exercises:
1.
Write a program that reads 3 double numbers from user and at the end, prints their sum,
product, and average. Use while loop in this program.
2.
Repeat Q1 using for loop.
3.
Repeat Q1 using do-while loop.
4.
Improve the above program of Q1 in the following way:
Instead of just reading 3 numbers always, now the program should ask in advance from the
user “How many numbers you want to enter?” Then the program should read as many
numbers as user wanted. At the end, the program should print the sum, product and
average of the entered numbers.
5.
Improve the above program of Q4 in the following way:
When the program asks “How many numbers you want to enter?” write the code using dowhile loop to make sure that users can enter only a positive number i.e., neither 0 nor a
negative number. If user tries to enter a 0 or negative number, the program should print an
error message and ask the user again: “How many numbers you want to enter?”. Then the
program should read as many numbers as user wanted. At the end, the program should
print the sum, product and average of the entered numbers.
6.
Write a program that reads some numbers from users until –1 is entered. For each input
number, the program should display its square. Use for loop in this program.
1
HW # 3
Due: next lab session
1. Write a C program that displays the following output. The program must use nested loops.
Sample Output:
6
5
3
4
2
1
2. Write a C program that reads some numbers from users until –1 is entered. At the end, the
program should print the average and standard deviation of the entered numbers (–1 is not
considered as the entered numbers).
1 N 
x

  xi 

N  i 1 
i 1
standard deviation 
N 1
N
average 
1
N
N
x
i 1
2
2
i
i
Submission Guidelines:
1. The homework must be submitted at the beginning of the next lab session in both soft and hard
copies.
2. Late homework is not accepted.
3. The homework must be solved individually. Any kind of cheating will result in F grade for all
parties involved.
Evaluation:
Your grade will depend on your active participation and seriousness during the lab.
2
Download