You are on page 1of 3

CSCI 151: Programming for Scientists and Engineers

Online Exam 4 - Part 2


Thursday 12 November 2020 9:45–10:15
This programming task is Part 2 of Online Exam 4 and is based on the contents of Lessons 1–23. You will need
to write the code in the template.c file posted on Moodle, insert your first name and last name in the file where
they are requested, rename the file as specified in the rules below and submit it on Moodle as a single ‘.c’ file
that your instructors can compile directly.
Below you find
• the Rules that you have to adhere in carrying out and submitting your file (this page please read them
carefully);
• the Task you have to carry out (page 2);
• an illustrative Example (page 3).

Rules
• This is the second part of online exam 4 and it is worth 5 points, which is 50% of the maximum grade of
online exam 4.
• You must submit the programming task by 10:15am.
• Any form of hard coding, cumbersome code or any unnecessary code will result in point deductions.
• You should make sure to use appropriate indentation.
• You do not need to use comments.
• Your code must be done individually. You cannot collaborate with your peers or communicate
with anybody other than the instructors. You cannot post your code on any public repository,
neither can you share it with anybody.
• You are not allowed to use notes, books and calculators.
• You are not allowed to reuse any code you previously developed or obtained from any source,
• You are not allowed to browse the internet, to send emails, to post messages (with the exception of posting
private messages to the Instructors on Piazza — only for emergency situations).
• Check your code with the online compiler https://repl.it/languages/c before submitting it to make sure
it compiles. However, even if you get the expected results from the online compiler, this does not guarantee
that your code is correct (you can only claim that your program compiles, not that it is correct).
• Submitted code will be automatically checked using tools that detect plagiarism.
• In case cheating is detected, the grade of online exam 1 will be zero and will not be dropped in the calculation
of the average of the online exams. Moreover, the academic misconduct will be reported to the school and
will result in failing of the course in case of repeated misconduct.
• You may be asked to explain your code and rewrite some parts of it in front of the instructors.
• Submit your code as one single .c file renamed YourlastnameYourfirstname.c without creating any
archive. This file must also contain a main function with appropriate testing code.
• In order to be accepted, your submission
– must be a single file in text format (no Word or other formats and no archives!) correctly
named as explained above;
– must have a ‘.c’ extension;

1
Task
Write a recursive function
int arrayToString (int from, int to, int arr[])
which
1. prints to the console output the string given by converting the integers in array arr
between from and to inclusive to characters, but skipping all integers that are not
ASCII codes of alphabetic characters;
2. returns the number of elements of the array between from and to inclusive that are
not converted into alphabetic characters.
Recall that characters are represented as integers. Thus
• you do not need to know the ASCII codes of the alphabetic characters;
• you can use relational operators to compare integers and characters;
• integers values can be printed as characters.
You can assume that arr contains at least the element in position 0 and that neither from
nor to is negative. However, from may be greater than to.
Make sure to test your function using at least the four test cases in the example at
page 3 or similar test cases.
In order to get points
you MUST
• use recursion to meet both specifications 1 and 2 above;
you CANNOT
• use any kinds of loops within the body of the arrayToString function;
• define any function other than arrayToString.
• change the name arrayToString of the function or its return type;
• change the type or the order of the formal parameters of arrayToString (you
are free to change the names of the formal parameters).
Failing to do so will result in a 0 grade.
Moreover, you CANNOT use any libraries other than stdio.h. Failing to do so will
result in point deduction.

2
Example
The following example is aimed at illustrating the task and helping you to understand it
but is not needed to solve the task.
Given the declaration and initialization
int anArray[] = { 140, 70, 60, 50, 100, 160, 130, 110 };
we would have the following results:
• call arrayToString(0,7,anArray) prints Fdn (characters for ASCII codes 70, 100
and 110, respectively) and returns 5 (integers 140, 60, 50, 160 and 130 are not
converted);
• call arrayToString(5,6,anArray) does not print anything and returns 2 (integers
160 and 130 are not converted);
• call arrayToString(4,6,anArray) prints d (character for ASCII code 100) and
returns 2 (integers 160 and 130 are not converted);
• call arrayToString(6,4,anArray) does not print anything and returns 0 (no integer
of the array is processed because from = 6 > 4 = to).
In fact, the ASCII codes for upper case letters start at 65 (’A’) and end at 90 (’Z’); the
ASCII codes for lower case letters start at 97 (’a’) and end at 122 (’z’).

You might also like