Systems Programming (XMUT)
Exercise 1: Basic C Program Development
Due 22 September 7 pm
Resources and links
-
activity1.c
-
activity2.c
-
activity3.c
-
activity4.c
-
activity5.c
-
activity6.c
Do not rename these files.
Activity 1: Basic I/O [10 Marks]
C uses the functions
printf()
and
scanf()
for basic output and input, respectively. These functions can actually perform complicated formatting, but for this activity, you will use them to acquire some input from the keyboard (referred to as standard input or
stdin
in technical manuals) and display something to the monitor (referred to as standard output or
stdout
in technical manuals).
We will talk about
printf()
and
scanf()
in the first tutorial-style lecture, but if you want to start this exercise, you can refer to our reference materials for help on how to use these functions.
For this activity, write a C program that performs the following:
- Display the message
"Enter an integer:⍽"
to stdout
(⍽ denotes a single space character. So, there is a whitespace after ':'
). Do not modify the message, otherwise, the automated marking might not mark your work correctly. (
- Use
scanf()
to read input integer from stdin
.
- Display the input integer to
stdout
. Do not add any other string in the output, otherwise, the automated marking might not mark your work correctly.
Save the program as
activity1.c
. Compile and run the program. If you are happy with the program, submit it to the Assessment System for marking.
Activity 2: Basic I/O [10 Marks]
This is similar to Activity 1, except that you will now handle any string input, even those with whitespaces.
Write a C program that performs the following:
- Display the message
"Enter a string without whitespaces:⍽"
to stdout
( ⍽ denotes a single space character. So, there is a whitespace after ':'
). Do not modify the message. Otherwise, the automated marking might not mark your work correctly.
- Use
scanf()
to read input string from stdin
.
- Display the input string to
stdout
. (Do not any other string in the output, otherwise, the automated marking might not mark your work correctly.)
Save the program as
activity2.c
. Compile and run the program. If you are happy with the program, submit it to the Assessment System for marking.
Activity 3: Basic I/O [20 Marks]
This is similar to Activity 2, except that you will now handle any string input, even those with whitespaces.
Write a C program that performs the following:
- Display the message
"Enter a string:⍽"
to stdout
( ⍽ denotes a single space character. So, there is a whitespace after ':'
). Do not modify the message, otherwise, the automated marking might not mark your work correctly.
- Use
scanf()
to read input from stdin
. Make sure that strings with whitespaces are wholly captured.
- Display the input string to
stdout
. (Do not add any other string in the output, otherwise, the automated marking might fail.)
Save the program as
activity3.c
. Compile and run the program. If you are happy with the program, submit it to the Assessment System for marking.
Activity 4: Basic I/O and Operators [20 Marks]
In this activity, you will use some C operators to perform a computation, and format a floating-point output using
printf()
.
Write a C program that will convert an input temperature from Celsius to Fahrenheit. The entire functionality should be implemented within the
main()
function. Follow this guide to implement the program.
- Declare a float variable
ctemp
which will hold the temperature in Celsius.
- Declare another float variable
ftemp
which will hold the temperature in Fahrenheit.
- Display the message
"Enter temperature: "
to stdout
(there is a whitespace after ':'
). Do not modify the message, otherwise, the automated marking might not mark your work correctly.
- Use
scanf()
to read input from stdin
. Store the input in ctemp
.
- Perform the actual conversion computation and assign the result to
ftemp
. Hint: The formula for converting Celius to Fahrenheit is given by
Hint: The formula for converting Celius to Fahrenheit is given by
tF =(9/5)×tC+32,
where tC is the temperature in Celsius, and tF is the temperature in Fahrenheit.
- Display
ftemp
, to three decimal places, using the printf()
function. (Do not add any other string in the output, otherwise, the automated marking might fail.)
To verify your code is correct, you can try the following conversions:
- -20 Celsius is -4.000 Fahrenheit.
- 0 Celsius is 32.000 Fahrenheit.
- 100 Celsius is 212.000 Fahrenheit.
Save the program as
activity4.c
. Compile and run the program. If you are happy with the program, submit it to the Assessment System for marking.
Activity 5: Functions [20 Marks]
In this activity, you will learn to write a C function.
Consider the following C program:
#include <stdio.h>
/* function implementation of calculate_area(length, width) */
// write your implementation here (you can use multiple lines)
/* end function implementation */
int main(void)
{
float l, w, a;
scanf("%f %f", &l, &w);
a = calculate_area(l, w);
printf("%f", a);
}
Complete the above program, providing an implementation of
calculate_area(...)
which returns the product of the input parameters
length
and
width
. (Do not change the other parts of the program.)
Save the program as
activity5.c
. Compile and run the program. If you are happy with the program, submit it to the Assessment System for marking.
Activity 6: Function-Like Macro [20 Marks]
In this activity, you will learn to write a C function-like macro.
Consider the following C program:
#include <stdio.h>
/* function-like macro implementation of SUM(A,B) */
// write your implementation here
/* end function-like macro implementation */
int main(void)
{
int a, b, s;
scanf("%d %d", &a, &b);
s = SUM(a, b);
printf("%d", s);
}
Complete the above program, providing a function-like macro implementation of
SUM()
, which returns the sum of the input parameters
A
and
B
. (Do not change the other parts of the program.)
Save the program as
activity6.c
. Compile and run the program. If you are happy with the program, submit it to the Assessment System for marking.