Week 2: String, Overflow and Type Casting (Solution)
Week 2: String, Overflow and Type Casting (Solution)
Overview
Solutions for the Week 2 exercises. Topics covered:
- Overflow, Underflow
- Handling Strings with
<string.h> - Type Casting / Conversion
1. Overflow
Check the maximum and minimum values of other data types and see what happens when you go beyond the range of values.
Exercise 1 Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
#define MAX 2147483647
#define MIN -2147483648
int main(){
// int -2147483648 ~ 214783647
printf("max +1 value: %d\n",MAX+1);
printf("min -1 value: %d\n",MIN-1);
// char -128 ~ 127
char charMax=127;
char charMin=-128;
printf("max+1 value of char: %c\n",charMax+1);
printf("min-1 value of char: %c\n",charMin-1);
// float 1.0e-45f ~ 3.4e38f
float floatmax=3.4e38f;
float floatmin=1.0e-45f;
printf("overflow value of float: %f\n", floatmax*2.0f);
printf("underflow value of float: %f\n", floatmin/2.0f);
return 0;
}
2. String
Create a program that introduces yourself by getting input of your last name, first name and age. Please get input of your last, middle, first name separately and print them by putting your name in one string variable. (Output Ex. Hello, my name is JinseSeo and I am 24 years old. Nice to meet you!)
Exercise 2 Solution:
Create a program that declares variables for your name, age, and favorite number, then prints them.
1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#include <string.h>
int main() {
char hello[20]="hello";
char world[20]="world";
printf("%s%s\n",hello,world);
strcat(hello,world);
printf("%s and length of hello string is: %d\n",hello,strlen(hello));
return 0;
}
3. Typecasting
Exercise 3 Solution:
Write a program that asks the score of Math, English, Korean and prints the average score to the second decimal place.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<stdio.h>
int main(){
int math;
int english;
int korean;
printf("Please input your Math score: \n");
scanf("%d",&math);
printf("Please input your English score: \n");
scanf("%d",&english);
printf("Please input your Korean score: \n");
scanf("%d",&korean);
printf("Your average score is: %.2f\n",(float)(math+english+korean)/3);
}
4. Assignment: a personal information output program
Task:
- Ask the user for their last name, first name, age, gender (M/F), height (in cm) and weight (in kg).
- Ask the user for their scores of Math, English, Korean and Art.
- Print all this information using proper formatting.
Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<stdio.h>
#include<string.h>
int main(){
int age;
char gender;
float height;
float weight;
char Lastname[20];
char Firstname[10];
int Math;
int English;
int Korean;
int Art;
printf("Please type your Last name, First name, age, gender(M/F), height (cm), and weight(kg)\n");
scanf("%s %s %d %c %f %f", Lastname, Firstname, &age, &gender, &height, &weight);
printf("Please input your scores of Math, English, Korean, and Art\n");
scanf(" %d %d %d %d", &Math, &English, &Korean, &Art);
printf("You are %s, %d years old and your gender is \"%c\"\n", strcat(Firstname,Lastname), age, gender);
height /=100; // cm to meter
printf("Your BMI is %f\n",weight/(height*height));
printf("Your average score is %.2f\n",(Math+English+Korean+Art)/4.0);
}
Next Week: Basic C Programming - Loops
Happy coding! ๐
This post is licensed under CC BY 4.0 by the author.