lab04
by
typing:
mkdir lab04Change to this directory by typing:
cd lab04
perfect.c
that reads a positive integer
n from standard input and prints all the factors of n,
their sum and if indicates if n is a
perfect number.
./perfect Enter number: 6 The factors of 6 are: 1 2 3 6 Sum of factors = 12 6 is a perfect number ./perfect Enter number: 1001 The factors of 1001 are: 1 7 11 13 77 91 143 1001 Sum of factors = 1344 1001 is not a perfect number
1511 style perfect.c
When you think your program is working you can use
autotest
to run some simple automated tests:
1511 autotest perfect
give cs1511 lab04_perfect perfect.cNote, even though this is a pair exercise, you both must run give from your own account before Monday 01 July 17:00 to obtain the marks for this lab exercise.
Write a C program, reverse_array.c
, which reads
integers line by line, and when it reaches the end of input, prints
those integers in reverse order, line by line.
You will never be given more than 100
integers to print
out.
The output from your program should look exactly like this:
dcc reverse_array.c -o reverse_array ./reverse_array Enter numbers forwards: 10 50 20 40 Reversed: 40 20 50 10 ./reverse_array Enter numbers forwards: -5 -4 -3 -2 -1 Reversed: -1 -2 -3 -4 -5
The result from calling scanf
can be assigned to an
integer, like so:
int scanned_in_value;
int result = scanf("%d", &scanned_in_value);
result
will be equal to the number of variables that
scanf successfully scanned in.
Explanation
In the code sample above, if the user does enter a number, scanf
will be able to read this in to the variable
scanned_in_value
, and thus it will have successfully
scanned in one value, so the variable result
will
contain the value 1.
If the user had not entered a valid number (for example if they
typed in a word instead), there would be no integer there for
scanf to read into
scanned_in_value
, so scanf would not successfully
scan in any values, and so the variable result
would
contain the value 0.
Detecting the end of input
The variable result
can be used to determine whether
your program has reached the end of the input (hint: if there
aren't any values left for scanf to scan in, the
result
variable won't contain the number
1
).
If you have a while
loop condition that checks the
result of scanf
to determine whether scanf was able
to successfully scan the expected number of variables, you can use
this information to keep scanning numbers until the user tells you
to stop.
Note: on Mac/Linux, you can signal "end of input" on the command line by pressing Control + D
1511 style reverse_array.c
When you think your program is working you can use
autotest
to run some simple automated tests:
1511 autotest reverse_array
give cs1511 lab04_reverse_array reverse_array.cNote, even though this is a pair exercise, you both must run give from your own account before Monday 01 July 17:00 to obtain the marks for this lab exercise.
In this task, you will create a program called
danish_flag.c
which will read in a size and display a
Danish Flag of that size using the following algorithm and the
#
character.
This exercise must be completed without Arrays.
The Danish Flag is made up of 6 blocks. It is 3 blocks wide, and 2
blocks high. To display it using empty spaces and the character
#
, we will read in a value called size
.
Each block will be 3 times size
wide, and 2 times
size
high.
#
character.See the diagram below for details.
For example
dcc -o danish_flag danish_flag.c ./danish_flag Enter the flag size: 1 ## ##### ## #####
./danish_flag Enter the flag size: 2 ##### ########### ##### ########### ##### ########### ##### ########### ##### ########### ##### ###########
./danish_flag Enter the flag size: 3 ######## ################# ######## ################# ######## ################# ######## ################# ######## ################# ######## ################# ######## ################# ######## ################# ######## ################# ######## #################
1511 style danish_flag.c
When you think your program is working you can use
autotest
to run some simple automated tests:
1511 autotest danish_flag
give cs1511 lab04_danish_flag danish_flag.cNote, even though this is a pair exercise, you both must run give from your own account before Monday 01 July 17:00 to obtain the marks for this lab exercise.
For this challenge, make a program called boxes.c
which
reads in a number and then draws that many square boxes inside each
other using the integer 1
.
This exercise can be completed with or without arrays. Either way you can assume that you will not be tested on any values of n above 50.
For example:
./boxes How many boxes: 1 111 101 111
./boxes How many boxes: 2 1111111 1000001 1011101 1010101 1011101 1000001 1111111
./boxes How many boxes: 5 1111111111111111111 1000000000000000001 1011111111111111101 1010000000000000101 1010111111111110101 1010100000000010101 1010101111111010101 1010101000001010101 1010101011101010101 1010101010101010101 1010101011101010101 1010101000001010101 1010101111111010101 1010100000000010101 1010111111111110101 1010000000000000101 1011111111111111101 1000000000000000001 1111111111111111111
1511 style boxes.c
When you think your program is working you can use
autotest
to run some simple automated tests:
1511 autotest boxes
give cs1511 lab04_boxes boxes.cYou must run give before Monday 01 July 17:00 to obtain the marks for this lab exercise. Note, this is an individual exercise, the work you submit with give must be entirely your own.
spiral.c
that reads an integer
n from standard input. and prints an nxn pattern
of asterisks and dashes in the shape of a spiral.
You can assume n is odd and >= 5.
This exercise must be completed without arrays.
Make your program match the examples below exactly.
./spiral Enter size: 5 ***** ----* ***-* *---* ***** ./spiral Enter size: 7 ******* ------* *****-* *---*-* *-***-* *-----* ******* ./spiral Enter size: 9 ********* --------* *******-* *-----*-* *-***-*-* *-*---*-* *-*****-* *-------* ********* ./spiral Enter size: 17 ***************** ----------------* ***************-* *-------------*-* *-***********-*-* *-*---------*-*-* *-*-*******-*-*-* *-*-*-----*-*-*-* *-*-*-***-*-*-*-* *-*-*-*---*-*-*-* *-*-*-*****-*-*-* *-*-*-------*-*-* *-*-*********-*-* *-*-----------*-* *-*************-* *---------------* *****************
1511 style spiral.c
When you think your program is working you can use
autotest
to run some simple automated tests:
1511 autotest spiral
give cs1511 lab04_spiral spiral.cYou must run give before Monday 01 July 17:00 to obtain the marks for this lab exercise. Note, this is an individual exercise, the work you submit with give must be entirely your own.
decimal_spiral.c
that reads an
integer n from standard input. and prints an nxn
pattern of decimal digits and dashes in the shape of a spiral.
You can assume n is odd and >= 5.
This exercise must be done without arrays.
Make your program match the examples below exactly.
./decimal_spiral Enter size: 5 65432 ----1 210-0 3---9 45678 ./decimal_spiral Enter size: 7 0987654 ------3 87654-2 9---3-1 0-012-0 1-----9 2345678 ./decimal_spiral Enter size: 9 876543210 --------9 8765432-8 9-----1-7 0-210-0-6 1-3---9-5 2-45678-4 3-------3 456789012 ./decimal_spiral Enter size: 15 654321098765432 --------------1 2109876543210-0 3-----------9-9 4-210987654-8-8 5-3-------3-7-7 6-4-87654-2-6-6 7-5-9---3-1-5-5 8-6-0-012-0-4-4 9-7-1-----9-3-3 0-8-2345678-2-2 1-9---------1-1 2-01234567890-0 3-------------9 456789012345678
1511 style decimal_spiral.c
When you think your program is working you can use
autotest
to run some simple automated tests:
1511 autotest decimal_spiral
give cs1511 lab04_decimal_spiral decimal_spiral.cYou must run give before Monday 01 July 17:00 to obtain the marks for this lab exercise. Note, this is an individual exercise, the work you submit with give must be entirely your own.
You can run give multiple times. Only your last submission will be marked.
Don't submit any exercises you haven't attempted.
If you are working at home, you may find it more convenient to upload your work via give's web interface.
Remember you have until Monday 01 July 17:00 to submit your work.
You cannot obtain marks by e-mailing lab work to tutors or lecturers.
You check the files you have submitted here
Automarking will be run by the lecturer several days after the
submission deadline for the test, using test cases that you haven't
seen: different to the test cases
autotest
runs for you.
(Hint: do your own testing as well as running
autotest
)
After automarking is run by the lecturer you can view it here the resulting mark will also be available via via give's web interface
When all components of a lab are automarked you should be able to view the the marks via give's web interface or by running this command on a CSE machine:
1511 classrun -sturecThe lab exercises for each week are worth in total 2 marks.
The best 8 of your 9 lab marks for weeks 2-10 will be summed to give you a mark out of 13. If their sum exceeds 13 - your mark will be capped at 13.