---
title: CSCI 1301 -- Lab 17
author: Clément Aubert
lang: en
fontsize: 10pt
documentclass: scrartcl
numbersections: true
links-as-notes: true
papersize: letter
geometry: margin=1in
keywords:
- Computer Science
- Problem solving methods
- Syllabus
- Augusta University
- Bachelor of Science
---


This lab will focus on getting you started with your second project.
You can find the instructions for this project at <http://spots.augusta.edu/caubert/teaching/2019/spring/csci1301/shared/project2.pdf>.
The hints below are relative to each problem, but I strongly recommend you go over them all no matter what problem you end up choosing.

# "Daily Pay" Problem

This problem is about computing a value that partially depends on the user, and displaying it nicely.

## Hint 1 - Computing

Observe the following code:

~~~~~~~~{.cs .numberLines}
int a = 1;
int b = 10
while (b > 1){
    a = a + 2;
    Console.WriteLine($"a is now {a}.");
}
~~~~~~~~

Adapt it so that

#. The loop actually stops!
#. The value of `a` would be _multiplied_ by `2` insted of being _incremented_ by `2` every time the loop execute.
#. The value of `b` would be set by the user.
#. The sum of all the values of `a` is computed and stored in a variable.

Once you completed all of those steps, your loop will be pretty close to what you actually need for this problem!

## Hint 2 - Displaying

The other aspect of this problem is to display the values nicely.
One way of doing this is by using the `String.Format` method.
Observe the following:

~~~~~~~~{.cs .numberLines}
Console.Write("*");
Console.Write(String.Format("{0, 10}", "Hello"));
Console.Write("*\n");
Console.Write("*");
Console.Write(String.Format("{0, -10}", "Hello"));
Console.Write("*\n");
~~~~~~~~

Adapt it so that

#. The escape sequence `\t` is displayed before the `*\n` in both statements.
#. Instead of displaying "Hello", the value of a numerical variable is displayed.
#. The numerical value is displayed using the format specifier for money.


# "Newspaper Machine" Problem

This program have two aspects: you need to loop ("_As long as the user did not gave me enough money nor decided to quit…_"), and to convert a character or string input (`Q`, for instance) into a number ($0.25$ in that case).

## Hint 1 - Repeating

Write a program that asks the user for an integer, add that integer to all the previous integers entered so far, and repeat as long as the sum of all the integer entered so far is less than $100$.
A `do while` loop will be more appropriate for this task, something *like*:

~~~~~~~~{.cs .numberLines}
int sum = 0;
do
{
    Console.WriteLine("Enter an integer.");
    // Here, we need some code to read from the user.
    // And some code to sum the values entered so far.
} while (sum < 100);
~~~~~~~~

## Hint 2 - Converting the User Input

Now, in the problem, we don't want to ask the user for actual integers.
We want to give the user the possibility to enter a character, and then to map that character to a value.
Write a small program that

#. Declare a double variable, and a string variable.
#. Ask the user to enter "N", "D" or "Q", and assign the value entered to that string variable.
#. Assign $0.05$ to the double if the string entered was "N", $0.10$ if the string entered was "D", and $0.25$ if the string entered was "Q".

# "Rock, Paper, Scissors" Problem

For this problem, you need first to simulate how a computer could "play", and then to implement the logic of the game.

## Hint 1 -  Generating Random Numbers

For this problem, we need to be able to generate random numbers.
This is the way we will simulate some really primitive artificial intelligence: the computer will play rock-paper-scissors by just randomly chosing one of the possibilities.

The code given in the problem (without the comments) is the following:

~~~~~~~~{.cs}
Random myRandomObject = new Random();
int a, i = 0;
while (i <= 100)
{
	a = myRandomObject.Next(1, 11);
	Console.Write(a + " ");	
	i++;
}
~~~~~~~~

#. Compile it, execute it, and observe what is displayed on the screen.
#. Are you able to modify this code, so that the values randomly generated are between $1$ and $3$?
#. Can you write a program that asks the user if (s)he wants to generate a new value, and display a new random number between $1$ and $3$ every time (s)he answers "Yes", and quit otherwise?

## Hint 2 - The Logic of The Game

Complete the following table with **W**in, **L**oose or get a **T**ie:

If you play | and the computer plays | then you 
--- | --- | ---
**Rock** |   **Rock**, |   \_\_\_\_\_\_\_
**Rock** |   **Paper**, |   \_\_\_\_\_\_\_
**Rock** |   **Scissors**, |   \_\_\_\_\_\_\_
**Paper** |   **Rock**, |   \_\_\_\_\_\_\_
**Paper** |   **Paper**, |   \_\_\_\_\_\_\_
**Paper** |   **Scissors**, |   \_\_\_\_\_\_\_
**Scissors** |   **Rock**, |   \_\_\_\_\_\_\_
**Scissors** |   **Paper**, |   \_\_\_\_\_\_\_
**Scissors** |   **Scissors**, |   \_\_\_\_\_\_\_

Now, in our program, the user will enter a string made of a single character ("R", "S", "P") and the computer will randomly generate an integer (1, 2, 3), so we can't "directly" use this table in our program.

But, exactly as you associate a string made of only one character to a word (R $⇆$ Rock, P $⇆$ Paper, S $⇆$ Scissors), you can associate an integer to a word, can't you?
Then, how to decide who won or if it is a tie should be easy!

# "Grade Calculator" Problem

This problem involves a bit of Mathematics that can be tricky to get correctly, and involve cleverly getting the information from the user.

## Hint 1 - Do The Math!

The first thing you need to do for this problem is to understand the equation needed to compute the grade.
Start by trying to reproduce the example given in the project description on paper.
Can you get the same result?
Can you find a way that seems simple and flexible to compute the current grade so far in all generality?

## Hint 2 - Gathering The Data

Once you have the equation figured out, you need to understand how you can obtain the data from the user.
You have two possible ways of doing it:

- Will you ask first for the number of quizzes taken, and then ask for the values, or
- Will you ask for the values, and take "-1", for instance, as a signal that this quiz did not happened yet.

Try to implement one or the other, without worrying about the computation at this point (just add, for instance, the value entered).
