Midterm
Problem 1
Write a C program that computes the end time from the start time and elapsed time.
1. Read the hour of start time as an integer in range [1, 12].
2. Read the elapsed time in hour, which is an arbitrary non-negative integer.
3. Compute the hour of the end time from the sum of the start hour and the elapsed hour and print it.
- The end hour should be in range [1, 12].
- Do not separate AM and PM.
Example)
Input the start hour (1~12): 5
Input the elapsed time in hours: 20
5 + 20 = 1
Input the start hour (1~12): 12
Input the elapsed time in hours: 0
12 + 0 = 12
Input the start hour (1~12): 8
Input the elapsed time in hours: 30
8 + 30 = 2
Code
#include <stdio.h>
int main(){
int start=0;
int elapse=0;
int end=0;
printf("Input the start hour (1~12):");
scanf("%d",&start);
printf("Input the elapsed time in hours:");
scanf("%d",&elapse);
end=start+elapse;
if(12<end){
while(12<end)
end-=12;
}
printf("%d + %d = %d\n",start, elapse, end);
}
Problem 2
Write a C program as follows:
1. Read two numbers, k and limit.
2. Print all positive multilples of k that is less than or equal to limit as the following examples.
3. Print the sum of the multiples of k printed in step 2 as the following examples.
Example)
Input k: 3
Input limit: 60
3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60
The sum of multiples of 3 in [1, 60] = 630
Input k: 6
Input limit: 100
6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96
The sum of multiples of 6 in [1, 100] = 816
Code
#include <stdio.h>
int main(){
int k=0;
int limit=0;
printf("Input k: ");
scanf("%d",&k);
int sum=0;
printf("Input limit: ");
scanf("%d",&limit);
for(int i=k ; i<=limit ; i+=k){
printf("%d ", i);
sum+=i;
}
printf("\nThe sum of multiples of %d in [%d, %d] = %d\n",k,1,limit,sum);
}
Problem 3
Write a C Program as follows:
1. Read a number as a string from the user.
2. Read a base from the user.
The base is an arbitrary integer between 2 and 10.
But, you don't have to check whether the input value is in range [2, 10].
3. Decide whether the number is a valid number in the numeral system of the base.
e.g.) Binary numbers are numeral system of base 2.
Octal numbers are numeral system of base 8.
Decimal numbers are numeral system of base 10.
If the input number (string) is a valid number of base, print "<number> is a base-<base> number".
Otherwise, print "<number> is NOT a base-<base> number"
<number> and <base> should be replaced by the input number and base.
Guideline)
All valid numbers in a numeral system of a base B is composed of digits in [0, B-1].
e.g.) A binary number is composed of 0 and 1
An octal number is composed of digits in [0, 7]
A decimal number is composed of digits from [0, 9]
If the input number (string) contains other characters, it is NOT a valid nummber of bases B.
Otherwise, decide it is a valid nummber of bases B.
Example)
Input a number: 325262
Input base (0 ~ 10): 8
325262 is a base-8 number. // 325262 is a valid octal number composed of digits in range [0, 7]
Input a number: 123*452
Input base (0 ~ 10): 9
123*452 is NOT a base-9 number. // '*' is not a digit
Input a number: 81726
Input base (0 ~ 10): 6
81726 is NOT a base-6 number. // '8', '7' and '6' are not in the valid range [0, 5]
Code
#include <stdio.h>
#include <string.h>
int main(){
char num[100];
int base=0;
int val=1; //assume the input is valid
printf("Input a number: ");
scanf("%s",num);
printf("Input base: ");
scanf("%d",&base);
for(int i=0 ; i<strlen(num) ; i++){
if(!(0<=num[i]-'0' && num[i]-'0'<=base-1)){
val=0; //the input is not valid
break;
}
}
if(val==0)
printf("%s is not a base-%d number\n",num,base);
if(val==1)
printf("%s is a base-%d number\n",num,base);
}
Problem 4
Write a C program that draws a vertical parallelogram.
1. Read the width of the parallelogram.
2. Draw a parallelogram whose left and right sides are vertical lines as the following examples.
- The parallelogram is compose of three parts (an upper triangle, a middle square, and a lower triangle).
The height of each part is the same as the width.
- You need to use the loop statements, such as the for-statement.
- Do NOT user gotoxy() or other functions in Console.c and Console.h
Example)
Input the width of vertical parallelogram: 5
*
**
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
**
*
Input the width of vertical parallelogram: 8
*
**
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
**
*
Code
#include <stdio.h>
int main(){
int width=0;
printf("Input the width of the vertical parallelogram: ");
scanf("%d",&width);
//upper part
for(int i=0 ; i<width ; i++){
if(i==0)
printf("*");
else{
printf("*");
for(int j=0 ; j<i-1 ; j++)
printf(" ");
printf("*");
}
printf("\n");
}
//middle part
for(int i=0 ; i<width ; i++){
printf("*");
for(int j=0 ; j<width-2 ; j++)
printf(" ");
printf("*");
printf("\n");
}
//lower part
for(int i=0 ; i<width ; i++){
for(int j=0 ; j<i ; j++)
printf(" ");
printf("*");
for(int z=0 ; z<width-2-i ; z++)
printf(" ");
if(i!=width-1)
printf("*");
printf("\n");
}
}