PRF192
PRF192
C
Homework Assignment 01
Objectives:
(1) Reviewing for number systems
(2) Exploring data types in C
(3) Basic inputs, outputs, and variables
Instruction: Students submit a single word or pdf file containing all solutions.
Exercise 1: Convert between decimal, binary, octal, and hexadecimal numbers and fill
in the following table.
in the following table.
# Decimal Binary Octal Hexadecimal
1 98 0b1100010 0o142 0x62
2 290 0b100100010 0o442 0x122
3 108 0b1101100 0o154 0x6C
4 74 0b1001010 0o112 0x4A
5 12.125 0b1100.001 0o14.1 0xC.2
Exercise 2: Data types with modifiers
The operator sizeof() can be used to find the amount of memory allocated to a given
data type (in bytes). For example:
#include <stdio.h>
main()
{
printf("%lu\n", sizeof(char));
printf("%lu\n", sizeof(int));
}
Output:
1
4
Use sizeof() in your program to fill in the following table.
# Data type size in byte(s)
1 float 4 byte(s)
2 double 8 byte(s)
3 unsigned char 1 byte(s)
4 long 8 byte(s)
5 short 2 byte(s)
6 signed char 1 byte(s)
7 long double 16 byte(s)
Exercise 3: Representation of numeric data types in C
a) Let x be a variable having type (signed) int in C. Then, x is represented by 4 bytes,
using the two-complement method. Find the binary representation of x and fill in the
following table:
# x (decimal) Representation of x in computer (int: 4
bytes)
1 -10 1111 1111 1111 1111 1111 1111 1111 0110
2 -25 1111 1111 1111 1111 1111 1111 1110 0111
- (37)10 = (100101)2
2. 7F + 1100
16 2
- (139)10 = (10001011)2
3. 1970 + 7E9
10 16
- (7E9)16 = 7. 16^2 + E. 16^1 + 9. 16^0 = (2025)10
Add the decimal values:
- (3995)10 = (111110011011)2
#include <stdlib.h>
int main() {
char inputChar;
int inputInt;
float inputFloat;
scanf("%c", &inputChar);
scanf("%d", &inputInt);
inputInt);
}else{
scanf("%f", &inputFloat);
return 0;