CS100 Homework [1]

Problems:

  1. Find Bugs!
  2. Equation of A Line
  3. Spell It Out

CS100 Homework 1 (Spring 2023)

Deadline: 2023-02-26 (Sun) 23:59:59

Late submission will open for 24 hours after the deadline, with 50% point deduction.

If you get full marks in this assignment by no more than 20 submission attempts, you can earn
special OJ displays and a "1-case protection" that can be used in further assignments to cancel
one testcase failure. See Piazza or OJ dashboard for more information.


Problem 1: Find Bugs!

Description

Your friend has just started learning programming, and has spent hours on a textbook practice
problem. He/She asks you for help by treating you to a KFC Crazy Thursday meal. You agree, and
he/she sends you a .txt file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <sudo.h>

void main()
{
// Calculate the average score of all students in a class.

int num;
int sum;

printf('How many students are there?/n')
scanf('d%', num)
printf("What are their scores?/n")

// We programmers count from zero!
for (int i = 0, i <= num, i++);
{
int sum, score;
scanf("d%", score);
sum += score;
}

double average = sum / num;
if (average = 60)
printf('Good!/n');
if (average > 60)
printf('Excellent!/n');
else
printf(“Bad!/n”);

printf("Average score is &.2f./n", average);

return 0;
}

For the sake of Crazy Thursday, you now have the responsibility to debug your friend's code
(Don't do so in ShanghaiTech!). You may first fix compile errors so that the code runs, and then
figure out why it (possibly) does not produce correct results.

Input format

The first line contains a number , representing the number of students in a class.

The following lines each contain , score of a student.

  • For 100% cases, 0<n500 < n \leqslant 50, and 0<si1000 < s_i \leqslant 100.

Output format

Four sentences, specified as in printf statements in the code.

In the last sentence, format the average score to 2 decimal digits.

Example

Input:

1
2
3
2
70
81

Output:

1
2
3
4
How many students are there?
What are their scores?
Excellent!
Average score is 75.50.

Test cases

👉 Go to GitHub


Problem 2: Equation of A Line

Description

Input two points' coordinates (x, y)(x,\ y) , where xx and yy are two integers. You need to calculate the
equation of the line going through these two points in the form of y=kx+by = kx + b, or x=cx = c (when
the slope k doesn't exist).

Input format

Two lines, each containing a coordinate in the form (x, y) , where x and y are integers.

  • For 100% cases, x<1000|x| < 1000, and y<1000|y| < 1000.

Output format

A line in the form of y = kx [+/-] b , or x = c .

  • k , b and c should all be of type double . When printed, format them to 2 decimal digits.
    If b < 0 , the minus sign - should take place of the + , turning the result into y = kx - b .

  • You do not need to simplify the cases b=0b= 0, k=0k=0, and k=±1k = \pm 1 . Just leave the number
    there, like y = 1.00x + 0.00.

Pay attention to spaces before and after '=' and '+'/'-' . It's safest to directly copy-paste the
above equations into your code.

Example

Input:

1
2
(2, -1)
(4, -3)

Output:

1
y = -1.00x + 1

Input:

1
2
(-2, 3)
(4, -6)

Output:

1
y = -1.50x + 0

Input:

1
2
(-10, -3)
(2, -3)

Output:

1
y = 0.00x - 3

Input:

1
2
(-2, 0)
(-2, 1)

Output:

1
x = -2

Test cases

👉 Go to GitHub


Problem 3: Spell It Out

Description

"How I wish that numbers could be gone!", cursed you after a terrible math exam. That exact
night, you had a bad dream where all numbers have disappeared! No one understands 1, 2, 3,
and you had to spell them out in English like one, two, three!

Luckily you had a computer to do all this, and a piece of guide to follow:

  • Use a hyphen ('-') to connect a word ending in -ty to another word. (22 -> twenty-two, 48000 -> forty-eight thousand)

  • There is NO "-s" (plural) for words "hundred" and "thousand". Also, 100 and 1000 are spelled "one hundred" and "one thousand".

  • To use or to not use an "and" after "hundred" is both OK. However, inserting an "and" makes the spelling easier to read, so let's put an "and" after "hundred" or "thousand" when they are followed by a part less than 100. (101 -> one hundred and one, 2023 -> two thousand and twenty-three, 114500 -> one hundred and fourteen thousand five hundred)

  • NO comma (',') after the world "thousand", although it sounds more natural to stop there when reading numbers out.

  • Pay attention to spellings, especially for "fourteen", "forty", "nineteen", and "ninety"! You don't want to spend hours debugging to find out nothing but a typo!

Input description

The input is a number.

  • For 20% cases, 0x9990 \leqslant x \leqslant 999 .
  • For 20% cases, x % 1000 == 0 .
  • For 100% cases, 0x9999990 \leqslant x \leqslant 999999 .

Output description

You should output the English spelling of number x in one line. It's OK whether you end the line
with a '\n' or not.

Hints

  • This problem does NOT involve strings (something we haven't learned)! Try to separate the output into different parts, and print each one with a printf! Keep in mind that printf does not necessarily end with a '\n' .

  • Some work might need to be performed multiple times (like translating 1-9 into "one" through "nine"). Why not write them into functions to make things easy and avoid bugs?

Sample input/outputs

Input Output
0 zero
44 forty-four
1590 one thousand five hundred and ninety
114500 one hundred and fourteen thousand five hundred

Appendix: Numbers in English

0 1 2 3 4 5 6 7 8 9
zero one two three four five six seven eight nine
10 11 12 13 14 15 16 17 18 19
ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
20 30 40 50 60 70 80 90 100 1000
twenty thirty forty fifty sixty seventy eighty ninety hundred thousand

Test cases

👉 Go to GitHub


Reference solutions

Remember, you should always solve all the problems yourself and passed all the testcases first.

  1. Find Bugs!
  2. Equation of A Line
  3. Spell It Out