You are on page 1of 7

MCIS 6204 Data Structure and Algorithms (Labs) Dr.

Jongyeop Kim
LAB #1:
A. Environmental Setup and Basic Debugging
B. Add Simple Functionality to a program
Lab #1.A
The purpose of this Lab #1.A is to get you interacting with C++ with Eclipse, so that you
can start to see and fix the kind of simple errors that crop up all the time when
programming.
In Lab #1.A you will type in a simple program and make various changes to it to observe
how it behaves. As you to the Lab # 1.A you make predictions about what will happen, and
will record what actually happens as you make small changes to the code. Your answers
will be recorded in a text file and turn it in to your Black board. You will be able to
complete your own machine after setup development environment.
Lab #1.B
In Lab#1.B, you will start to add simple functionality to a program. It will also allow you
to start seeing the differences between integer and floating point types in C++. The
program asks the user to type in a few values from the keyboard, and then perform various
math functions with those values, and prints out the results.
___________________________________________________________________________
Directions
1. Run your Eclipse
2. Lab1A
2.1 Create Lab1A.cpp
Type in the following program. Also, change the top print statement to print your name,
section name and ID number. Then compile and run the program.
// C++ Lab1A
// <Your Name>
//<Section Name>
// <Your Student ID>
#include <iostream>
using namespace std;
int main()
{
//Declaring Variables//
int iX, iY;
Page

1 of 7

cout << "Please enter your integer ";


cin >> iX;
iY = iX + 100;
cout << "Welcome to Southern Arkansas University \n";
cout << "Hope you are having fun in Lab \n";
cout << "iY = " << iY;
cin >> iY;
return 0;
}//end of main
2.2 Run the program and answer the following questions. The program ask you to enter a
number. Type in a number and press enter. A sample compilation and run of the
program can be seen below:
Please enter an integer 4
Welcome to Southern Arkansas University
Hope you are having fun in Lab
iY = 104
2.3 Make sure you can run your program correctly before proceeding any further.
2.4 Create a file named Answer(Lab1A).txt to hold your predictions of what will
happen, and actual outcomes , for the exercises below. At top of the file, type your
name and section name. Then, save the file in the same folder as before. As with the
Lab1A.cpp file, make sure that this file is in the right folder.
2.5 For this part of the Lab, you will make changes to Lab1A.cpp and try to compile and
run the program to see what happens.
2.5.1 For each letter below, write the letter in Answer(Lab1A).txt. Then predict what
will happen and write your prediction in Answer(Lab1A).txt
2.5.2 Next, make the change in Lab1A.cpp and try to compile and run after each
changes has been introduced. Record any error messages that the compiler
produces(copy them from the show output from: windows)
2.5.3 If no compile time error occurs, record any run-time exceptions or other errors
that occur when you run the program.
2.5.4 Undo the previous change each time before you introduce a new change
a. Comment out line number 6 which contains using namespace std;
b. Remove the first quotation mark in the string of the first cout statement
c. Change "int iX, iY;" to "int iX;"
Page

2 of 7

d. Remove the statement #include <iostream>


e. Remove the semicolon at the end of the line
iY = iX + 100; to iY = iX + 100
f. Change "iY = iX + 100" to "iY = iX + 100.0"
2.6 Make sure to save Answer(Lab1A).txt and Lab1.cpp
2.7 At this point, you are ready to turn in the Lab1A. To do so, you must turn in it your
files from your computer to your Black board.
Sample Answers
// C++ Lab1A
// Jongyeop Kim
// Section 002
// 99990001
a. Your Prediction: Undefined name space will make compile error
Run results:
1>d:\c++\lab1\lab1\lab1.cpp(9): error C2065: 'cout' : undeclared identifier
1>d:\c++\lab1\lab1\lab1.cpp(11): error C2065: 'cin' : undeclared identifier
b. Your Prediction:
Run results:

f. Your Prediction:
Run results:

3. Lab1B
3.1 Create Lab1B.cpp
Page

3 of 7

Type in or copy the following program. Also, change the top print statement to print your
name and ID number. Then compile and run the program.
// <Your Name>
//<Your Section Name>
// <Your Student ID>
//C++ Lab1B
#include <iostream>
using namespace std;
int main( )
{
int iX = 0;
float fY = 0;
string title;
// Declare integer variable iA, iB and
// float variables fA, fB here.
// Intialize all variables with zero.
// [Add Code Here]
// Prompt user for input
cout << "Enter two integer numbers : ";
// Read user int input and store value into variable iA
cin >> iA;
// Read in the second int value that the user
// enters and store it in iB
// Similar to the statement above
// [Add Code Here]
// Prompt the user to provide a floating value
// Similar to statements above
// [Add Code Here]
// Read user floating input and store value into variable fA
cin >> fA;
// Prompt the user to provide a second double value
// Similar to statements above
// [Add Code Here]
// Read in the second floating value that the user
Page

4 of 7

// enters and store it in fB


// Similar to the statement above
// [Add Code Here]
iX = iA + iB;
cout << "\n iA + iB = " ;
cout << iX;
cout << "\n " ;
// Calculate iA - iB and assign the results to iX here
// [Add Code Here]
cout << "\n iA - iB = ";
cout << iX ;
// Calculate iA * iB and assign the results to iX here
// [Add Code Here]
cout << "\n iA * iB = ";
cout << iX;
// Calculate iA / iB and assign the results to iX here
// [Add Code Here]
cout << "\n iA * iB = ";
cout << iX;
// Calculate iA % iB and assign the results to iX here
// [Add Code Here]
cout << "\n iA % iB = ";
cout << iX;
// Calculate fA + fB and assign the results to fY here
// [Add Code Here]
cout << "\n fA + fB = ";
cout << fY;
// Calculate fA - fB and assign the results to fY here
// [Add Code Here]
cout << "\n fA - fB = ";
cout << fY;

Page

5 of 7

// Calculate fA * fB and assign the results to fY here


// [Add Code Here]
cout << "\n fA * fB = ";
cout << fY;
// Calculate dA / dB and assign the results to dY here
// [Add Code Here]
cout << "\n fA / fB = ";
cout << fY;
// Calculate dA % dB and assign the results to dY here
// [Add Code Here]
cout << "\n fA % fB = ";
cout << fY;
cin >> Fy;
} // End of Lab2
3.2 Fill In the Code
Add code segments as instructed in comments to complete the program. You only need to add
code where it says [Add Code Here]. Verify that the program compiles without any errors and
runs as expected.
3.3 Run the program
After you have completed the program, and it compiles and runs, save a copy of its output by
copying it off of the terminal. You can then paste it into a text file Answer(Lab2).txt. An
example run of the file will look something like the following:
Enter two integer numbers:
2 20
Enter a double number:
3.3
Enter a second double number:
9.9
iA + iB = 22
iA - iB = -18
iA * iB = 40
iA / iB = 0
iA % iB = 2
fA + fB = 13.2
fA - fB = -6.6000000000000005
fA * fB = 32.67
fA / fB = 0.3333333333333333
Page

6 of 7

fA % fB = 3.3
3.4 Hand in the program
Handin two files to the Black board
: Your source code and execution results
Lab1B.cpp, Answer(Lab1B).txt

Page

7 of 7

You might also like