0% found this document useful (0 votes)
14 views5 pages

PRF192

The document provides instructions for a homework assignment on programming fundamentals in C. It includes 5 exercises on number systems, data types, inputs/outputs, and variable representations. Students are asked to complete code snippets, convert between numeric bases, and write a short program for character and number input/output.

Uploaded by

chihieu.15082005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

PRF192

The document provides instructions for a homework assignment on programming fundamentals in C. It includes 5 exercises on number systems, data types, inputs/outputs, and variable representations. Students are asked to complete code snippets, convert between numeric bases, and write a short program for character and number input/output.

Uploaded by

chihieu.15082005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PRF192-Programming Fundamentals using

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

b) Convert between decimal and floating-point (float32) numbers


# x float32 representation of x (4 bytes)
1 - 1 1000 0010 10000100000000000000000
12.125
2 200.75 0 1000 0110 10010001100000000000000
Exercise 4: Compute the following expressions and write the result in binary
representation.
1. 1010 + 11011
2

- (1010)2 = 1. 2^3 + 1. 2^1 = 8 + 2 = (10)10

- (11011)2 = 1. 2^4 + 1. 2^3 + 1. 2^1 + 1. 2^0 = 16 + 8 + 2 + 1 = (27) 10

Add the decimal values:

- (10)10 + (27)10 =(37)10

Convert the result back to binary:

- (37)10 = (100101)2

2. 7F + 1100
16 2

- (7F)16 = 7. 16^1 + F. 16^0 = 7. 16 + 15 = (127)10


- (1100)2 = 1. 2^3 + 1. 2^2 = 8 + 4 = (12)10
Add the decimal values:

- (127)10 + (12)10 = (139)10

Convert the result back to binary:

- (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:

- (1970)10 + (2025)10 = (3995)10

Convert the result back to binary:

- (3995)10 = (111110011011)2

Exercise 5: Basic data input, output, and variables


The following table gives a list of arguments in printf.
printf() conversion characters
Conversion character How the corresponding argument is printed
c as a character
d as a decimal integer
e as a floating-point number in scientific notation
f as a floating-point number
g in the e-format or f-format, whichever is
shorter
s as a string
For example, the statement
printf(“%c, %d”, ‘A’, ‘A’);
will display A, 65 in the screen. (In the ASCII table, character ‘A’ is encoded by 65. The
first 32 characters in the ASCII-table are control characters and thus unprintable.
https://www.ascii-code.com/)
Write a C program that does the following tasks:
1. Ask the user to input a character. Display the character and its corresponding
ASCII code.
2. Ask the user to input an integer between 32 to 127. Display the integer and its
corresponding ASCII character.
3. Ask the user to input a floating-point number and display the number in scientific
notion and floating-point number format.
#include <stdio.h>

#include <stdlib.h>

int main() {

char inputChar;
int inputInt;

float inputFloat;

printf("Enter a character: ");

scanf("%c", &inputChar);

printf("Character: %c, ASCII code: %d\n", inputChar, inputChar);

printf("Enter the integer (32 to 127): ");

scanf("%d", &inputInt);

if (inputInt >= 32 && inputInt<= 127){

printf("Integer: %d, Corresponding ASCII character: %c\n", inputInt,

inputInt);

}else{

printf("Invalid input, please enter an integer between 32 and 127.\n");

printf("Enter a floating-point number: ");

scanf("%f", &inputFloat);

printf("Floating-point number: %f\n", inputFloat);

printf("Scientific nonation: %e\n", inputFloat);

return 0;

You might also like