COMP1521 2020T3 Q3 — Processes & syscalls
COMP1521 2020T3 Q3 · 9 marks · C
Topic: Processes & syscalls · Source: https://cgi.cse.unsw.edu.au/~cs1521/26T1/resources/practice_exams.html
Summary: Decide if an env-var integer is close to a target (default 42)
Write a C program, 20t3final_q3.c ,
which takes two names of environment variables as arguments.
20t3final_q3.c should print 1
iff both environment variables
are set to a similar integer value.
20t3final_q3.c should print 0 otherwise.
Two integer values are similar if they differ by, strictly, less than 10 .
So 37 and 42 are similar .
So 52 and 42 are not similar .
An unset environment variable should be assumed to have value 42
The shell command export sets an environment variable to a value;
the shell command unset unsets an environment variable.
In the following example,
export is used to set the environment variables
VAR1 , VAR2 and VAR3 and
unset is used to ensure environment variable VAR4 is not set.
export VAR1=40
export VAR2=45
export VAR3=55
unset VAR4
dcc 20t3final_q3.c -o 20t3final_q3
./20t3final_q3 VAR1 VAR2
1
./20t3final_q3 VAR3 VAR2
0
./20t3final_q3 VAR2 VAR4
1
./20t3final_q3 VAR4 VAR3
0
There is no supplied code for this question.
Your program can assume it is always given 2 arguments.
You program can assume that if an environment variable is set it
contains a valid integer and nothing else.
Your program should always print one line of output to stdout.
The line of output should contain only a single character: 0 or 1, and a newline.
Your solution must be in C only.
You are not permitted to run external programs.
You are not permitted to use system, popen, posix_spawn, fork or exec.
🤖 Adapted: read TWO lines from stdin = the two integer values (<unset>means the variable is unset and is treated as 42). Print1iff the two values differ by strictly less than 10, else0.
Verified on the sandbox AND cross-checked against an independent oracle.
Samples
40 45
1
55 45
0