MTH 4300: Algorithms, Computers, and Programming II

Introduction to C++

1. First Program
The first problem that we will solve is the following problem:
Problem 1. The user input consists of two positive integers a and b. Create a program that calculates \(a\cdot (a+7b)\).

We will create the program by first typing the following code and saving it in the file calculation01.cpp.
#include< iostream>
int main(){
   int a;
   int b;
   std::cout<< "What are your two favorite integers ? ";
   std::cin >>a;
   std::cin >> b;
   int c;
   c=a*(a+7*b);
   std::cout << c;
   std::cout << std::endl;
   return 0;
}
The first line #include<iostream> pre-loads necessary code that can read user input from the keyboard and print the output to the screen.
Every C++ program must contain the function main. It is OK for now to think of main as a mandatory ritual in which you put the line int main(){ at the top the lines and return 0; and } at the bottom of your code.
The line int a; is the first command of our program. Every command must end with ;. This particular command gives the following request to the computer.
Please give me the memory that can hold one integer. This memory will be called a. When in future I ask to store or read from this memory, I will refer to it as a.
The line int b; has a similar effect.
The line std::cout<<"What are your two favorite integers? "; prints the question on the screen. Our understanding of the C++ for the near future will include the following wisdom "In C++ the computer screen is called std::cout".
The line std::cin>>a; asks the computer to take a number from the user input device. The standard input device is called std::cin. At the moment and during the next few weeks the standard input will be the keyboard. Later on, we will work with more advanced input devices where the wires will be directly plug to the brain and thoughts will be so easy to read.
The line std::cin>>b; does an analogous thing.
We then declare the variable c with the command int c; and with the next command we calculate \(a\cdot(a+7b)\) and store it in \(c\).
The command std::cout << c; prints the content of the variable c. The command std::cout<< std::endl; prints an end of line character. You may later make an experiment by removing this particular line and see how ugly the output will turn out to be.
2. Compilation of the source
The file calculation01.cpp is called the source file. It is written in C++ which is a language that the computers do not understand. We need to translate the source code to the binary file. We can choose the name firstBinary for our first binary file. The binary file is created if we type the following command in the terminal.
c++ calculation01.cpp -o firstBinary
The binary file is in machine language. The computer understands the machine language. So we can execute the file by typing the following in the terminal
./firstBinary
3. Execution of the binary
As mentioned earlier we type ./firstBinary in the terminal. The computer then asks us about our two favorite integers. Since they are \(17\) and \(5\) and we must never lie to a computer, these are the two things we type in. The program then outputs the number \(884\) which is obtained as the result of the calculation \(17\cdot(17+7\cdot 5)\).
4. Loops
The following simple program illustrates how the while loop is used in C++.
Problem 2. Create a program that reads real numbers from the user input until it reads a negative number. The negative number means that the input is over. The program should calculate the maximum of the numbers provided by the user.

5. Practice
Problem 3.

The user input consists of a list of integers each of which is either \(0\) or \(1\). Once the user provides the number \(2\) the input is over. You are allowed to assume that the user will provide at least two terms before typing number \(2\). The following sequence of operations is performed with the list from the input. Each operation consists of identifying the two leftmost terms that are equal. If they are both \(0\), then they are replaced with the single term equal to \(1\). If they are both equal to \(1\), then they are replaced with the single term equal to \(0\). Create the program that determines the final sequence that is obtained after repeatedly performing the described operations.

Example: Input: 1 0 1 1 0 0 1 2.

Output: 1 0 1.

Explanation: The first operation replaces the two leftmost terms \(1\) and \(1\) with \(0\). The resulting sequence is 1 0 0 0 0 1. The next operation transforms the sequence into 1 1 0 0 1 . Next the sequence becomes 0 0 0 1 . The final sequence is 1 0 1 .