LAB 1 Overview of Programming and Problem Solving
Transcript of LAB 1 Overview of Programming and Problem Solving
C++ Programming Lab Manual LAB1
LAB 1Overview of Programming and
Problem Solving
Objectives:
In this lab, you will:
• log in to a computer successfully
• Be able to use Visual C++ editor and C++ compiler to accomplish the following tasks
successfully :
oLoad a file containing a program
oAlter a file containing a program
oSave a file
oPrint a file
oCompile a file
oRun a file
oChange a program and rerun it
oCorrect a program with errors
oEnter and run a program
1 الطبع حقوق جميع للتطوير – إشعاع مركز محفوظة
1
C++ Programming Lab Manual LAB1
oExit the system.
• Examine a C++ program
• Explore how a C++ program is processed
• Be able to discover the error and how to correct it
• learn how to execute a program to print “Welcome to C++” on the screen successfully
1 الطبع حقوق جميع للتطوير – إشعاع مركز محفوظة
2
C++ Programming Lab Manual LAB1
Assignment Cover Sheet Lab 1
Name: ------------------------------------ Student ID: ----------------------------
Section: ------------------------------------ Date: -----------------------------
ActivitiesAssigned
check or list exercise numbersGrade
InLab
1 الطبع حقوق جميع للتطوير – إشعاع مركز محفوظة
3
C++ Programming Lab Manual LAB1
Outlines:
• Lesson 1-1: Introduction to C++
• Lesson 1-2: Introduction to Programming
• Lesson 1-3: The Phases of writing a program
• Lesson 1-4: Introduction to Visual C++ 2008 express Edition
• Lesson 1-5: How to use Visual C++
• Lesson 1-6: How to write your first program
• Lesson 1-7: How to run a program with errors, read the errors and correct them.
1 الطبع حقوق جميع للتطوير – إشعاع مركز محفوظة
4
C++ Programming Lab Manual LAB1
Introduction to C++ Lesson 11
Welcome to C++! We have worked hard to create what we hope be an appropriate text book
to give you a guidance to program by using C++.
C++, an extension of C, was developed by Bjarne Stroustrup starting in 1979 at Bell Labs as
an enhancement to the C programming language and originally named C with Classes. It was
renamed C++ in 1983.
As one of the most popular programming languages ever created, C++ is widely used in the
software industry. Some of its application domains include systems software, application
software, device drivers, embedded software, high-performance server and client applications,
and entertainment software such as video games. Several groups provide both free and
proprietary C++ compiler software, including the GNU Project, Microsoft, Intel and Borland.
C++ has greatly influenced many other popular programming languages, most notably Java.
C++ is also used for hardware design, where design is initially described in C++, then
analyzed, architecturally constrained, and scheduled to create a register transfer level hardware
description language via high-level synthesis.
Introduction to Programming Lesson 12
1 الطبع حقوق جميع للتطوير – إشعاع مركز محفوظة
5
C++ Programming Lab Manual LAB1
Programming is a process of problem solving. Computer programming is the process of
designing, writing, testing, debugging / troubleshooting, and maintaining the source code of
computer programs. This source code is written in a programming language. The purpose of
programming is to create a program that exhibits a certain desired behaviour (customization).
The process of writing source code often requires expertise in many different subjects, including
knowledge of the application domain, specialized algorithms and formal logic.
The Phases of Writing a Program Lesson 13
Every program need to pass some or all of the following:
• Input: Before a program can operate on data, the data need to be entered by the user.
• Process: To make some calculations and manipulation for the inserted data.
• Output: To show the processed information or any notation on the screen or any proper output device.
Algorithm: A step-by-step problem-solving process in which a solution is arrived at in a finite
amount of time.
Example: One of the simplest algorithms is to print “succeed” if the student’s mark is >=50,
and print “failed” if the student’s mark is <50, the algorithm represented in the flowchart below:
1 الطبع حقوق جميع للتطوير – إشعاع مركز محفوظة
6
C++ Programming Lab Manual LAB1
Fig1-1: Algorithm example
A typical C++
environment:
1- Editor:
program is created and saved on the disk
2- Preprocessor: process the data
3- Compiler: Compiler creates object code and stores in a disk
4- Linker: Links the object code with the libraries
5- Loader: To load the program in memory
6- CPU: execute the instructions
Introduction to Visual C++ 2008 Lesson 14
Microsoft Visual C++ (often abbreviated as MSVC or VC++) is a commercial integrated
development environment (IDE) product engineered by Microsoft for the C, C++, and C+
+/CLI programming languages. It has tools for developing and debugging C++ code, especially
code written for the Microsoft Windows API , the DirectX API, and the Microsoft .NET
Framework.
1 الطبع حقوق جميع للتطوير – إشعاع مركز محفوظة
7
C++ Programming Lab Manual LAB1
In this lab we are going to use Visual C++ version 2008. Any other versions such as 2005 or
visual studio 6 are acceptable versions.
How to Use Visual C++ Lesson 15
You need to follow the following steps to create your first project:
1. From Start go to All Programs then click on Visual C++2008 Express Edition. Then a screen of C++ software will be opened as Fig1-2.
1 الطبع حقوق جميع للتطوير – إشعاع مركز محفوظة
8
C++ Programming Lab Manual LAB1
Fig1-2: The initial screen of C++ 2008
2. From File click on New then choose Project see Fig1-3. A New Project screen will be shown as Fig1-4.
1 الطبع حقوق جميع للتطوير – إشعاع مركز محفوظة
9
C++ Programming Lab Manual LAB1
Fig1-3: Project command from file menu
Fig1-4: New Project Window
1 الطبع حقوق جميع للتطوير – إشعاع مركز محفوظة
10
C++ Programming Lab Manual LAB1
3. From the (New Project) window that appears, select Win32 Console Application. Then choose a name for your project (As start name it Pro1), then choose a location from Browse button to save it there (As start create a new folder with your id). Then click on Ok. After that a pop up window will appear click on Next. See Fig1-5, 1-6.
Fig1-5: Project Name and Location
4. Another popup window will appear, choose Empty Project. Then, click on Finish.
Fig1-6: A window to choose an empty Project
1 الطبع حقوق جميع للتطوير – إشعاع مركز محفوظة
11
Write the name of the
project
Browse a location to the
project
C++ Programming Lab Manual LAB1
5. To create a new form, you can go to Project from menu bar, then choose new item. Another
way, right click on Source Files, then a popup menu will appear choose Add then choose New
Form. See Fig1-7.
Fig1-7: Right click on the source file
6. Select C++ File and write the name of your form. Look at Fig1-8.
Fig1-8: A new Item Window
1 الطبع حقوق جميع للتطوير – إشعاع مركز محفوظة
12
Write the name of the
project
Browse a location to the
project
Choose C++File(.cpp)
C++ Programming Lab Manual LAB1
7. A new screen will be opened to write the code. This window is the editor window. See Fig1-9.
Fig1-9: The Editor Window
8. Write your code in the code area as below, this code is the general structure of any C++ program. (It’s a program that does nothing).
1 الطبع حقوق جميع للتطوير – إشعاع مركز محفوظة
13
Notice that you are ready to write a cod in
Lab1_1.ccp.file
This is the editor area
C++ Programming Lab Manual LAB1
Fig1-10: A code that do nothing
9. To check that your code is free of errors, go to Build from menu bar and click on Build Solution, to insure that the code does not have any syntax errors or warning. See Fig1-11.
Fig1-11: Build Solution command to check the code
Fig1-12: The Build output screen
1 الطبع حقوق جميع للتطوير – إشعاع مركز محفوظة
14
No errors, No warnings in the program
C++ Programming Lab Manual LAB1
10. To execute the program, click on Start Without Debugging from Debug menu or Press Ctrl+F5. See Fig1-12.
Fig1-13: Executing the code
12. There is no output, because the program does not do anything. See Fig1-13.
Fig1-14: The Output
1 الطبع حقوق جميع للتطوير – إشعاع مركز محفوظة
15
C++ Programming Lab Manual LAB1
How to write your f irst program in C++ Lesson 16
Redo the previous steps to write the following C++ program then save, compile and run it.
What is the output?
Now we will discuss the code shown above in details:
#include <iostream>: Preprocessor directives. This tells the preprocessor to include in the program the contents of the input/output stream header file <iostream>. This file should be included for any C++ program.
using namespace std: To give the program the ability to use cin, cout, or endl.
int main(): main is a function that should be written in every C++ program. C++ program will start executing at this main function. The keyword int means that the main will return an integer value. You could replace it with the word void which means to return nothing. The returning values functions will be discussed later on.
{: The left brace must begin the body of every function.
}: The right brace to end every function.
cout<<”Welcome to C++”<<endl; : a C++ statement, each statement should end with semicolon (; ). cout<< is an order to print on the screen the following “Welcome to C++”. endl means to end the line on the screen.
1 الطبع حقوق جميع للتطوير – إشعاع مركز محفوظة
16
#include <iostream> //line1using namespace std; //line2
int main() //line3{ //line4
cout<<"Welcome to C++ "; //line5 return 0; //line6 } //line7
C++ Programming Lab Manual LAB1
return 0; : end the main function.
General form of a C++ program
Fig1-15: General form of a C++ program
1 الطبع حقوق جميع للتطوير – إشعاع مركز محفوظة
17
C++ Programming Lab Manual LAB1
How to deal with the errors? Lesson 17
You need to read the errors from the output window. If this window was not opened, then go
to View from menu bar and choose Output, or Alt+2.
You need to figure out your errors and try to fix it.
•Compilation errors like:-
oDirective error.
o Int main error
o {} braces error
oSemicolon error
o ( ) parenthesis error
o cout error
o “” double quotation error
• Logical errors( will be explained later)
1. Write this program that has some errors.
1 الطبع حقوق جميع للتطوير – إشعاع مركز محفوظة
18
#include <oistream> //line1using namespace std //line2
int main //line3{ //line4
cout>>"Welcome to C++ ; //line5 return ; //line6 { //line7
C++ Programming Lab Manual LAB1
2. Write the errors that come out from the above program in the provided Error sheet, then
correct them and write the corrections.
InLab Assignment
1. To write a new program:
Write a program to print your name and your id on the paper then on visual C++ 2008
2. To Open and edit an existing file:
Open the saved file from the previous question to print your name, your ID, your address, and your age each in a single line.
1 الطبع حقوق جميع للتطوير – إشعاع مركز محفوظة
19
C++ Programming Lab Manual LAB1
Error sheet Lab1
Name: ---------------------------------- Student ID: -----------------------------
Section: ------------------------------- Date: --------------------------------
# OK Type of Error Corrections
1
2
3
4
5
6
7
8
9
1 الطبع حقوق جميع للتطوير – إشعاع مركز محفوظة
20