Lab # 1 DE-32 DCE
ALGORITHM & COMPUTING
Programming Languages
A program is a sequence of instructions that specifies
how to perform a computation. Language is a way by using which we can communicate with others. There are two types of programming languages. These are
High Level Languages
A programming language like C++ that is designed to be easy for humans to read and write. Low Level Languages A programming language that is designed to be easy for a computer to execute. Also called "machine language" or "assembly language."
Advantages & Disadvantages
Disadvantage
Programs written in a high-level language have to be
translated before they can run. This translation takes some time.
Advantages
First, it is much easier to program in a high-level language;
by "easier" I mean that the program takes less time to write, it's shorter and easier to read, and it's more likely to be correct. Secondly, high-level languages are portable, meaning that they can run on different kinds of computers with few or no modifications. Low-level programs can only run on one kind of computer, and have to be rewritten to run on another.
Translation of Program
There are two ways to translate a program;
interpreting or compiling. An interpreter is a program that reads a highlevel program and does what it says. In effect, it translates the program line-by-line, alternately reading lines and carrying out commands.
A compiler is a program that reads a high-level
program and translates it all at once, before executing any of the commands. In this case, the high-level program is called the source code, and the translated program is called the object code or the executable.
Integrated Development Environment (IDE)
You can use a simple text editor to create your
source code or use the built-in editor that comes with compiler. Well use Microsoft Visual Studio 6.0 editor.
Do save your file with the .c, or .cpp extension.
Well save all the source code files with the .cpp extension.
Steps to build a Project
To create and test any program, follow these steps:
Start the Compiler. Choose File, New from the menus. Choose Win32 Console Application and enter a project name,
such as Example 1, and click OK. Choose an Empty Project from the menu of choices and click OK. Choose File, New from the menus. Choose C++ Source File and name it ex 1. Enter the code as indicated above. Choose Build, Build Example 1.exe. Check that you have no build errors. Press Control+ F5 to run the program Press the Spacebar to end the program.
The Development Cycle
Structure of a program
// my first program in C++ #include <iostream> using namespace std; int main () { cout << "Hello World!"; return 0; }
Comment
// my first program in C++
This is a comment line. All lines beginning with two
slash signs (//) are considered comments and do not have any effect on the behavior of the program. The compiler ignore comments so they do not add to the file size. The programmer can use them to include short explanations or observations within the source code itself. In this case, the line is a brief description of what our program is.
Preprocessor Directives
#include <iostream>
Lines beginning with a hash sign (#) are preprocessor
directives. They are not regular code lines with expressions but indications for the compiler's preprocessor. In this case the directive #include<iostream> tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program.
using Directive
using namespace std;
All the elements of the standard C++ library are
declared within what is called a namespace, the namespace with the name std. So in order to access its functionality we declare with this expression that we will be using these entities. If we didnt use the using directive , we would need to append the std name to many program element. e.g.
std::cout << "Hello World!";
int main()
This line corresponds to the beginning of the definition of
the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. The word main is followed in the code by a pair of parentheses (()). That is because it is a function declaration. Right after these parentheses we can find the body of the main function enclosed in braces ({}). What is contained within these braces is what the function does when it is executed.
Program Statements
The program statement is the fundamental unit of C++
programming. A statement is a simple or compound expression that can actually produce some effect. Here is the statement cout << "Hello World!"; cout represents the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters Notice that the statement ends with a semicolon character (;). This character is used to mark the end of the statement. (one of the most common syntax errors is indeed to forget to include some semicolon after a statement).
Program Statements
return 0;
The return statement causes the main function to
finish. return may be followed by a return code (in our example is followed by the return code 0). A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution.
Output using cout
cout << "Hello World!";
Causes the quotation marks to be displayed on the
screen
The identifier cout is predefined in C++ iostream file. The operator << is called the insertion or put to
operator. It directs the contents of the variable to the object on its left.
Variables and Data type
Variable is a location in computers memory in which you can store a value and from which you can later retrieve that value.
Data Type is a classification that identify one of various types of data.
Example
int number; Here, int is a data type and number is variable.
Data Types & Size
Type
bool char unsigned short int short int unsigned long int long int int float double long double
Size
1 byte 1 byte 2 bytes 2 bytes 4 bytes 4 bytes 4 bytes 4 bytes 8 bytes 10 bytes
Values
true or false 256 character values 0 to 65,535 -32,768 to 32, 767