Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

CSCI 151: Programming for Scientists and Engineers

Programming Assignment 1
Deadline: THURSDAY, 29 SEPTEMBER 2022 by 20:00

This programming assignment is based on the contents of Lessons 1–11 inclusive. You
will need to submit on Moodle your program with .c extension as explained in the rules
below.

Rules
1. Late submissions will NOT be accepted. A 0 grade will be given for late
submissions.
2. No deadline extensions will be granted.
3. The assignment will be graded according to the following rubric:
○ 10% if there is a reasonable attempt to address the tasks;
○ 20% in total for: correct indentation (5%), appropriate comments (5%),
successful compilation (10%).
○ 70% in total for the three tasks, partitioned in the way indicated in the task
descriptions.
Please note that if compilation and/or execution problems cannot be easily
fixed during grading, you will lose the entire 70% grade component of the
three tasks.
4. Your code must be developed individually. You cannot collaborate with your
peers. You cannot post your code on any public repository, nor can you share it
with anybody. Any violations to this code of conduct will be treated as academic
misconduct.
5. Check your code with the online compiler
https://www.programiz.com/c-programming/online-compiler/ 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).
6. Submitted code will be automatically checked using tools that detect plagiarism.
7. In case cheating is detected, the grade of this assignment will be zero and a
further penalty of 10 points will be applied to the total course grade (you would
lose 25% of your course grade!).
8. You may be asked to explain your code and rewrite some parts of it in front of the
instructors.
9. Please note that
○ The only library you can use is <stdio.h>
○ You cannot use arrays, pointers, function definitions or anything that is
presented in the course after lesson 11.
10. Submit the tasks in 3 different C files named
Yourfirstname_Yourlastname_TaskN.c where N is either 1, 2, or 3. Make sure to
include the underscore ‘_’ in the filename.
11. In order to be accepted, your submission
○ must be of text format with a ‘.c’ extension (no Word or other formats and
no archives will be accepted), correctly named as explained in Rule 10.
○ You will receive a zero grade if you do not submit the assignment or you
do not adhere to this rule!

Task 1 (20%):
You will write a program that will simulate the rock, paper, scissors game.
Here are the steps you need to implement in your program:
● Ask Player 1 to enter his/her choice. Player 1 will enter characters ‘R’,’P’, or ‘S’
for rock, paper, or scissors, respectively.
● Ask Player 2 to enter his/her choice. Player 2 will enter characters ‘R’,’P’, or ‘S’
for rock, paper, or scissors, respectively.
● If Player 1 or Player 2 enters any other character, your program should output
“Not a valid input!”, and keeps asking the user to enter a valid input.
● In case Player 2 enters the same input as Player 1, your program will ask Player
2 to enter his/her input again until it’s different from Player 1.
● Based on both players' selection, your program will output the winning player,
i.e., either of the following messages will be printed “Player 1 wins”, or “Player 2
wins”.
● Your program will ask the user if he/she wants to play again. In case the user
enters ‘Y’ or ‘y’ the program will start over. If the user enters any other character
the program will stop.
Sample run (Bold font indicates user inputs):
Enter Player 1 choice ('R','P','S')
R
Enter Player 2 choice ('R','P','S')
S
Player 1 wins!
Do you want to play again?(Enter 'Y' or 'y' for YES)
y
Enter Player 1 choice ('R','P','S')
P
Enter Player 2 choice ('R','P','S')
X
Not a valid input!
Enter Player 2 choice ('R','P','S')
S
Player 2 wins!
Do you want to play again?(Enter 'Y' or 'y' for YES)
Y
Enter Player 1 choice ('R','P','S')
S
Enter Player 2 choice ('R','P','S')
S
Your input is the same as Player 1. Please select another choice
Enter Player 2 choice ('R','P','S')
R
Player 2 wins!
Do you want to play again?(Enter 'Y' or 'y' for YES)
n
Task 2 (25%):
In this task, you are asked to write a program that determines the day of the week on
which a given date falls using the following algorithm.

Let D, M, Y, K, J denote integer variables having the following values:

D = the day of the month (1, 2, . . . , 30, 31)


M = the month of the year (1, 2, ..., 11, 12)
Y = the year starting from 1970 upto and including 2022 (1970, 1971, …, 2021, 2022)
K = the remainder when Y is divided by 100
𝑌
𝐽= 100

The algorithm is summarized as follows:


● Get D, M, and Y from the console.
● If D, M, or Y is not in the required range, ask the user to enter it again.
● Compute integers K and J according to the descriptions shown above.
● If M is 1 or 2, add 13 to M and subtract 1 from Y
● Let E, F, G, H, I be integer variables computed using the following formulas:
● 𝐸 = 2. 6𝑀 − 5. 39
𝐾
● 𝐹= 4
𝐽
● 𝐺= 4
● H = D + E + K + F + G - 2*J
● I = the remainder when H is divided by 7

The value of I is the day of the week, where 0 represents Sunday, 1 represents Monday,
. . , and 6 represents Saturday. In case the computed value of I is negative, add 7 to get
a non negative number between 0 and 6.

Sample run1 (Bold font indicates user inputs):


Enter the day of the month (1...31)
1
Enter the month of the year (1...12)
1
Enter the year
2022
The given date is Saturday!
Sample run2 (Bold font indicates user inputs):
Enter the day of the month (1...31)
15
Enter the month of the year (1...12)
9
Enter the year
2022
The given date is Thursday!

Sample run3 (Bold font indicates user inputs):


Enter the day of the month (1...31)
32
Invalid input
Enter the day of the month (1...31)
6
Enter the month of the year (1...12)
13
Invalid input
Enter the month of the year (1...12)
5
Enter the year
1567
Invalid input
Enter the year
1978
The given date is Saturday!
Task 3 (25%):
The goal of this task is to read a sequence of uppercase alphabetic characters from an
input file named “in.txt” and encrypt it by shifting each character by a fixed number of
positions (p) along the alphabet and restarting from the beginning (letter A) after
reaching the end (letter Z).

For example, let “ABCDEFGHIJKLMNOPQRSTUVWXYZ” be the input character sequence


read from the input file, and let the number of positions used for the encryption be 3.
The output that will be written to the output file named “out.txt” will be
“DEFGHIJKLMNOPQRSTUVWXYZABC”.

If the input file contains some non-alphabetical characters or lowercase alphabetical


characters, your program should not change them and just copy them to the output file.

Assume in.txt consists of the following single line for Sample run1 and Sample run2:
ABCDEFGHIJKLMNOPQRSTUVWXYZ

Sample run1 (Bold font indicates user inputs):


Enter the value of p for encryption
3
out.txt has been created successfully.

After the program terminates, out.txt should contain this line:


DEFGHIJKLMNOPQRSTUVWXYABC

Sample run2 (Bold font indicates user inputs):


Enter the value of p for encryption
1
out.txt has been created successfully.

After the program terminates, out.txt should contain this line:


BCDEFGHIJKLMNOPQRSTUVWXYZA

Assume in.txt consists of the following two lines for Sample run3 and Sample run4:
A1 B2 C3 DEF
!@# CSCI151

Sample run3 (Bold font indicates user inputs):


Enter the value of p for encryption
1
out.txt has been created successfully.

After the program terminates, out.txt should contain these two lines:
B1 C2 D3 EFG
!@# DTDJ151

Sample run4 (Bold font indicates user inputs):


Enter the value of p for encryption
26
out.txt has been created successfully.

After the program terminates, out.txt should contain these two lines:
A1 B2 C3 DEF
!@# CSCI151

Assume in.txt consists of the following single line for Sample run5 and Sample run6:
TABLE

Sample run5 (Bold font indicates user inputs):


Enter the value of p for encryption
1
out.txt has been created successfully.

After the program terminates, out.txt should contain the following line:
UBCMF

Sample run6 (Bold font indicates user inputs):


Enter the value of p for encryption
27
out.txt has been created successfully.

After the program terminates, out.txt should contain the following line:
UBCMF

You might also like