---
title: CSCI 1301 -- Lab 25
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
---

# Exam #2

You can find a `pdf` of the exam [here](exam_2.pdf).

## "Favorite number" Problem

The statement reads:

> Write a program that asks the user to guess your favorite number, and keep asking until the user guess it. You should keep the count of the "valid" attempts, i.e., of the number of times the user entered an integer that was not your favorite number.

A possible solution is:

~~~~~~~{.cs .numberLines .lineAnchors}
    const int FAVORITE = 11;
    int guess;
	int count = 0;
	do{
		Console.WriteLine ("Try to guess my favorite number!");
		if (int.TryParse(Console.ReadLine(), out guess)){count++;}
	}while(guess != FAVORITE);
	Console.WriteLine($"You won, my number was {FAVORITE}! It took you {count} attempts!");
~~~~~~~

If your favorite number is $0$, then the program has to be adapted.
Can you see why?

## "Switch" Problem

You can use the code to test your answers:

~~~~~~~{.cs}
using System;

class MainClass {
 public static void Main(string[] args) {

  bool passed = false;   // Change the value here!
  string major = "COMM"; // Change the value here!
  int graduation = 2021; // Change the value here!

  switch (major) {
   case ("CS"):
   case ("AIST"):
   case ("CYBR"):
    if (graduation > 2020)
     if (passed) Console.WriteLine("You should take 1302.");
     else Console.WriteLine("You should take 1200.");
    break;
   case ("HIST"):
    if (graduation > 2020 && passed) Console.WriteLine("You should take 2600.");
    else if (passed) Console.WriteLine("You should take 2700.");
    break;
   case ("PHYS"):
   case ("CHEM"):
    if (passed) Console.WriteLine("You should take 1302.");
    else if (graduation > 2020) Console.WriteLine("You should take 1200.");
    break;
   default:
    if (passed) Console.WriteLine("You should take 1302.");
    else Console.WriteLine("You should take 1200.");
    break;
  }
 }
} 
~~~~~~~

## "Time conversion" Problem

The statement starts with:

> Write a program that asks the user for the current time (in 12-hours format, e.g. 1:30 PM, 9:50 AM, 12:00 AM, etc.) and convert it to 24-hours format (a.k.a. military time, e.g., 1330, 0950, 0000, etc.).

You can make sure you understand how 24-hours format work at <https://en.wikipedia.org/wiki/24-hour_clock>.
You can convince yourself that 12-hours format should not be used anymore by reading the "A remark for readers from the U.S." at <https://www.cl.cam.ac.uk/~mgk25/iso-time.html>.

A possible (extremely short!) solution is:

~~~~~~~{.cs}
  int milit_time = 0;
  Console.WriteLine("Is it the morning? Enter \"Y\" for yes,\n" +
   "anything else for no.");
  if (Console.ReadLine() != "Y") {
   milit_time += 1200;
  }

  Console.WriteLine("Enter the hour followed by enter.");
  milit_time += (int.Parse(Console.ReadLine()) * 100);

  Console.WriteLine("Enter the minutes followed by enter.");
  milit_time += int.Parse(Console.ReadLine());
  
  // Apply patch here.
  
  Console.WriteLine($"It is {milit_time.ToString("D4")}.");
~~~~~~~

However, note that this program does not always behave properly.
Usually, the convention is that "12:00 PM" stands for "noon", and "12:00 AM" is "midnight".
So, if the user enters "N", "12" and "30", that stands for 12:30 PM (i.e., "Thirty minutes past noon"), but converting it to 2430 would be wrong: it should be 1230!
The other way around, if the user enters "Y", "12" and "30", that stands for 12:30 AM (i.e., "Thirty minutes past midnight"), but converting it to 1230 would be wrong: it should be 0030.

This can be easily patched with the following snippet, to insert where `// Apply patch here.`{.cs} is:

~~~~~~~{.cs}
if (milit_time > 2400 || (milit_time > 1201 && milit_time < 1299)){milit_time -= 1200;}
~~~~~~~

Since this was not formally asked in the instructions, no points will be taken off for students that did not thought about it.

