ECE 175: Computer Programming for Engineering Applications Lab 3
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
ECE 175: Computer Programming for Engineering Applications
Lab 3
Topics: nested loop and File I/O
Demo to TA or ULA your code for each problem by the end of your lab session in order to earn your lab 3 score.
Problem 1 (15 points):
Write a C program that lets a user enter only a number between 1 and 9 and display the following output pattern. Your program should make sure that a user enters only a positive number between 1 and 9.
Hint: Nested loops should be used.
Note: Your code must work for any user input from 1-9. If you hard-code the pattern in your program, you will get 0 point for this problem.
Sample code execution 1: Bold text indicates information entered by a user Enter only integer 1-9: -5
Enter only integer 1-9: 0
Enter only integer 1-9: 12
Enter only integer 1-9: 4
1
222
33333
4444444
From the output pattern above,
first row, 3 spaces then 1 digit of 1 is displayed second row, 2 spaces then 3 digits of 2 are displayed third row, 1 space then 5 digits of 3 are displayed fourth row, 0 space then 7 digits of 4 are displayed
Sample code execution 2: Bold text indicates information entered by a user Enter only integer 1-9: 9
1
222
33333
4444444
555555555
66666666666
7777777777777
888888888888888
99999999999999999
Problem 2 (15 points):
Write a C program that read values data size in bits from a file called “data.txt” and convert it to kilo bytes, bytes, and bits (in this order).
Note: 1 kilo byte (Kbytes) = 1024 bytes and 1 byte = 8 bits
Given data in “data.txt” 4500 8192 7 2500 0 99999 8 190
(you have to create this text file before running your program)
Sample code execution: If any of the units is equal to zero, it should not be printed.
4500 Data: 562 bytes 4 bits
8192 Data: 1 Kbytes
7 Data: 7 bits
2500 Data: 312 bytes 4 bits
0
99999 Data: 12 Kbytes 211 bytes 7 bits
8 Data: 1 bytes
190 Data: 23 bytes 6 bits
2022-10-13