Computer Program

Computer Program
Computer Program
CONCEPT
 A program is a set of instructions a computer follows in order to performa task. A programming language is a special language used to write computer programs.

What Is a Program?
Computers are designed to follow instructions. A computer program is a set of instructions that tells the computer how to solve a problem or perform a task. For example, suppose we want the computer to calculate someone’s gross pay. Here is a list of things the computer might do:

1. Display a message on the screen asking “How many hours did you work?”

2. Wait for the user to enter the number of hours worked. Once the user enters a number, store it in memory.

3. Display a message on the screen asking “How much do you get paid per hour?”

4. Wait for the user to enter an hourly pay rate. Once the user enters a number, store it in memory.

5. Multiply the number of hours by the amount paid per hour, and store the result in
memory.

6. Display a message on the screen that tells the amount of money earned. The message
must include the result of the calculation performed in step 5.

Collectively, these instructions are called an algorithm. An algorithm is a set of well defined steps for performing a task or solving a problem. Notice these steps are sequentially ordered. Step 1 should be performed before step 2, and so forth. It is important that these instructions be performed in their proper sequence.
programming
programming

Although a person might easily understand the instructions in the pay-calculating algorithm, it is not ready to be executed on a computer. A computer’s CPU can only process instructions that are written in machine language. A machine language program consists of a sequence of binary numbers (numbers consisting of only 1s and 0s) which the CPU interprets as commands. Here is an example of what a machine language instruction might look like:
1011010000000101
As you can imagine, the process of encoding an algorithm in machine language is very tedious and difficult. In addition, each different type of CPU has its own machine language. If you wrote a machine language program for computer A and then wanted to run it on a computer B that has a different type of CPU, you would have to rewrite the program in computer B’s machine language.

Programming languages, which use words instead of numbers, were invented to ease the
task of programming. A program can be written in a programming language such as
C++, which is much easier to understand than machine language. Programmers save
their programs in text files, and then use special software to convert their programs to
machine language.
PROGRAM
1 // This program calculates the user's pay.
2 #include <iostream>
3 using namespace std;
4
5 int main()
6 {
7 double hours, rate, pay;
8
9 // Get the number of hours worked.
10 cout << "How many hours did you work? ";
11 cin >> hours;
12
13 // Get the hourly pay rate.
14 cout << "How much do you get paid per hour? ";
15 cin >> rate;
16
17 // Calculate the pay.
18 pay = hours * rate;
19
20 // Display the pay.
21 cout << "You have earned $" << pay << endl;
22 return 0;
23 }

Program Output with Example Input Shown in Bold

How many hours did you work? 10 [Enter]
How much do you get paid per hour? 15 [Enter]
You have earned $150
Previous
Next Post »