QUESTION: What is a computer?
Answer: Copmuter is an electronic machine, that takes input, processes it and
gives the output.
In this tutorial, the topic of discussion is programming computer to ask values
from us, process the same, and produce a result.
PRE-REQUIREMENTS:
In order to understand this tutorial, you should have a basic idea on handling
a situation where, two people with same name exists in different
class-rooms. Suppose that, there are two people with same name,
"Sam" in two different rooms, A and B. How do you call
them?
A simple solution is... "Room A's Sam" and "Room B's Sam".
The process of removing this kind of confusion is called as conflict
RESOLUTION.
Coming to the programing tutorial, as usual,
1) The program is presented as a whole
2) Explanation is given on each line of the program, later.
In the first tutorial, we saw, how to display text. It is possible to
display known numbers, as a part of text. But now, we will handle a
different situation, where we leave the task of computing a calculation to the
computer, and finally, computer will show us, what result did it get.
Here is the program..
___________________________________________
# include <iostream>
void main( )
{
double result = 234.0 + ( 135.0 - 456.0)/245.0;
std::cout << "The result of the calculation
is...";
std::cout << result;
}
___________________________________________
Program explanation:
In the previous tutorial, we saw, how to use printf to display some text.
std::cout will also do a similar job. The symbol, "<<"
gives, what ever there exists on it's right side to the one on it's left.
So, "<<" gives the text, "The result of the calculation
is..." to std::cout. Now, std::cout will display (in the console
window), what ever it gets. By pressing "F10", you can absorve
that.
In std::cout, std stands for standard. Recall that, we resolve confusion
by using "Room A's Sam". In a similar way, cout could be the
name of some other thing, some where else. In this context, we are using
cout, that belongs to a place , named std. As we use 's after Room A, we
use :: after std, to indicate that, cout belongs to std. ( the symbol ::
is called as Scope resolution operator)
In the same way, std::cout displays the result (in the next line of code).
Now, another program is presented to you now. This program reads a number
from the user. Let's see, how we can do it.
__________________________________________
# include <iostream>
void main( )
{
double value, square;
std::cout << "Enter a real number
please....";
std::cin >> value;
std::cout << "Square of the number you entered
is....";
square = value * value;
std::cout << square;
}
__________________________________________
Explanation:
As std::cout displays on the console, the thing given to it,
std::cin does the reverse thing.
It gives the thing, displayed in the console to the thing, right to it.
That's why, the number you typed will be assumed(by the computer) to be that
value of "value" variable. This process is called as reading
input.
NOTE: After typing the value, you should press enter key, to let the computer
assume that, you completed typing the number.
Later, we calculated square of the given number. This is called as
processing the input.
Finally, we showed the result. This is called as displaying the output.
Now, click
here, to get an idea, how software companies earn money.