0% found this document useful (0 votes)
17 views

MCIS 6204 - Data Structure & Algorithms (Labs) - Day2

This document contains instructions for two programming labs - Lab 2A on if statements and Lab 2B on while loops. Lab 2A requires writing a program that takes a user input number between 1-5 and outputs the corresponding academic year (freshman, sophomore, etc.). Lab 2B requires modifying a program to read temperature values from a data file, convert Celsius to Fahrenheit, and output the results in a table using a while loop. The document provides code snippets and directions to add the necessary logic to complete the programs. It also includes sample expected output and files required to test and run the programs.

Uploaded by

bharathkumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

MCIS 6204 - Data Structure & Algorithms (Labs) - Day2

This document contains instructions for two programming labs - Lab 2A on if statements and Lab 2B on while loops. Lab 2A requires writing a program that takes a user input number between 1-5 and outputs the corresponding academic year (freshman, sophomore, etc.). Lab 2B requires modifying a program to read temperature values from a data file, convert Celsius to Fahrenheit, and output the results in a table using a while loop. The document provides code snippets and directions to add the necessary logic to complete the programs. It also includes sample expected output and files required to test and run the programs.

Uploaded by

bharathkumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

MCIS 6204 Data Structure and Algorithms (Labs) Dr.

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

1.2 Build and run the Program.


Make sure it works for numbers 1 to 5, and that it prints out an error message for numbers 0
and 6. Also, test out what happens when you enter something that is not a number.
1.3 Sample Output
Enter year number?
1
You are a Freshman
Enter Year Number ?
4
You are a Senior
Enter Year Number ?
7
You entered wrong input
1.4 Hand in
Upload your source code and test result Lab2A.java, Answer(Lab2A).docx to the Black board.

Page

2 of 5

2. Lab2B While Loops


2.1 Create Lab2B.cpp
While Loops
Lab2B.cpp (also shown below) has the beginnings of a program designed to take input from
the keyboard or text files in degrees Celsius, and then convert them to degrees Fahrenheit,
and write out the results to a table. Start with the program Lab2B.cpp.
Then, build the program by adding the loop in the indicated space, and also add any code
where it says [Add code here].
// Lab2B.cpp
// <Your Name>
// <Your Section Name >
//<Your ID>
// This program will read in from a file a series of numbers in degrees
// Celsius, convert them to degree Fahrenheit and print out a table of
// those values.
#include <iostream>
#include <fstream> //for file open
#include <iomanip> // for format number numbers
using namespace std;
int main ( )
{
// Declare variables
ifstream OpenFile("Lab2Bdata.txt");
double degreeCelsius;
double degreeFahrenheit;
double aNumber;
string aType;
// Print to the screen the header of the output table
// as seen in the sample run below.
// There are two lines in the header.
// Make sure it has the right spacing to line up with the table.
//1. [Add code here]
// Write your loop here
// We want to loop until there are no more inputs to be read.
while( /* 2. [Add code here]*/ )
{

//Until file OpenFile.eof


Page

3 of 5

// Read one value from the Data


// If a double store in degreeCelsius from data file
// named Lab4data.txt.
// then
// Convert input degrees Celsius to degrees Fahrenheit
// and store in degreeFahrenheit.
// f = c * (9.0/5.0) + 32.0
// [Add code here]
//else
//if input data is not double then
//set f to zero ( f = 0 )
// Display to the screen the output as shown in the
// sample run below. Use cout Make sure
// that everything lines up properly.
cout ( /* [Add code here */ );
} // End of while
cin >> aNumber;
return 0;
} //End of main
2.2 To test the program, you will use the text file lab4data.txt, which should be included
with the lab. The text of this file is shown below:
5.5
15
25.4
35.8
45.1
55.9
65
75.7
85.6
95.0
2.3 Related #include files as follows:
#include <iostream> // IO - cout ,cin
#include <fstream> // file IO
#include <iomanip> // formatting setw(6), setpresicion(2)
#include <typeinfo> //for checking variable types
#include <string>
//for string
Page

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

You might also like