CMSC 160
Fall 2018
08/30/2018
Exploring Basics
Due:  end of lab

For this exercise, you may work in pairs if you wish.    If you work in a pair, hand in only one submission sheet and put both names on it.   Print both names clearly and each of you should sign the paper.  Your signature indicates that you indeed worked as a pair on the entire exercise.

Assignment:    (4 points)

Create and save a new program file.   In it should go the following structure:

  #include <iostream>
  using namespace std;
  int main()
  {

      // snippets go here

  }


What does each of the following code fragments do? 
  1. Try to predict what it does
  2. Type the code in to a program, run it and see if you were right.
  3. If you were wrong, understand what happened.  Ask questions as necessary.
Note: Each snippet should be put in SEPARATELY.  (Erase one before typing in the next one.)



A B C
1 int a = 5;
int b = 10;
cout << a;
cout << b;
int a = 5;
int b = 10;
cout << a << endl;
cout << b;
int a = 5;
int b = 10;
cout << a;
cout << b << endl;
2 int a = 5;
int b = 4 + a * 2;
cout << a << endl;
cout << b << endl;
int a = 5;
int b = a*4;
a = 10;
cout << a << endl;
cout << b << endl;
int a = 15;
int b = a%4;
cout << a << endl;
cout << b << endl;
3 double a = 5.0;
int b = 4 + a * 2;
cout << a << endl;
cout << b << endl;
int a = 10 * 6 / 9;
cout << a << endl;

int a = 10 * (6 / 9);
cout << a << endl;
4 int a = 100;
cout << a << endl;
++a;
cout << a << endl;
int a = 100;
cout << a << endl;
--a;
cout << a << endl;
int a = 3.14159;
cout << a << endl;
--a;
cout << a << endl;
5 int a = 100;
cout << a << endl;
a = a + 1;
cout << a << endl;
int amount;
cout << "How much did the meal cost? " ;
cin >> amount;
cout << "The tip is: ";
cout << amount * 0.15 << endl;
int amount;
cout << "How much did the meal cost? " ;
cin >> amount;
double tip =
amount * 0.15;
cout << "The tip is: " << tip << endl;
6 char letter = 'x';
cout << letter << endl;
cout << letter + 1 << endl;
cout << letter + 2 << endl;
cout << letter + 3 << endl;
string greeting = "Hello";
cout << greeting >> endl;
bool x = true;
bool y = false;
cout << x;
cout << ! x;
cout << y;
cout << ! y;

7 string name;
cout << "Enter your name: ";
cin >> name;
cout << "Hello, " << name << endl;

// HINT: enter both first and last names when you run the program
string name;
cout << "Enter your name: ";
getline(cin,name);
cout << "Hello, " << name << endl;

// HINT: enter both first and last names when you run the program
string name;
cout << "Enter your name: ";
getline(cin,name);
cout << "Hello, " << name << endl;
return 1;

// HINT: enter both first and last names when you run the program


Handin:

Hand in the sheet you are handed in lab.