MCIS 6204 - Data Structure & Algorithms (Labs) - Day2
MCIS 6204 - Data Structure & Algorithms (Labs) - Day2
Jongyeop Kim
Lab #2:
A. If Statement
B. While Loops
Lab#2.A
If statements
In this Lab, you will write a program that prints out whether you are a freshman, sophomore,
junior, senior or graduate student based on a number input by the user. The numbers are as
follows:
1 for freshman
2 for sophomore
3 for junior
4 for senior
5 for graduate student.
Lab#2.B
While Loops
A program that can only run a set of instructions once without making choices or repeating
commands is not that useful. Typically, a program will want to choose different execution
paths, and iterate over a set of instructions many times, either to come to a cumulative final
result, or to process a bunch of items in sequence. That is where if statements and loops
come in. In this lab, you will explore the while loop construct in C++.
Directions
1. Lab2A If Statements
1.1 Create Lab2A.cpp
Start with the file Lab2A.cpp, the text of which is shown below.
Replace instances of [Add code here] with appropriate code to do what the comments say to do.
Also, include your name and student number at the top.
// C++ Lab2A
// <Your name here>
// <Your Section name here>
//<Your Id Here>
// This program will print user's current academic year given by the user.
#include <iostream>
using namespace std;
Page
1 of 5
int main ( )
{
//Declare five string constants, one for each year of possible input.
//[Add code here for YEAR1 to YEAR4]
string YEAR5 = "grad";
//User input for current year
int academicYear;
// Write code to prompt the user for a year number.
// Then, store the input into acedemicYear. You may assume that
// the user will enter a valid number.
// [Add Code Here]
// Write a series of if ... else if ... else statements
// to print out what year you are based on the int read in.
//If something besides those five numbers was entered,
// Print an error message and quit the program.
// [Add code here]
cin >> academicYear;
return 0;
} // End of main
Page
2 of 5
3 of 5
4 of 5
2.4 A Sample run with the given input can be seen below.
Celsius | Fahrenheit
-----------+-----------5.50| 41.90
15.20| 59.36
35.80| 96.44
45.10| 113.18
55.90| 132.62
65.30| 149.54
85.60| 186.08
95.00| 203.00
2.5 Make sure that it includes your name and section name id at the top.
2.6 Upload your three files to the black board
Lab2B.cpp, Lab2Bdata.txt, Lab2Bout.docx (screen shot of your output).
Page
5 of 5