Introduction to C++ (using Microsoft Visual C++)

Size: px
Start display at page:

Download "Introduction to C++ (using Microsoft Visual C++)"

Transcription

1 Introduction to C++ (using Microsoft Visual C++) By the end of this section, the students should be able to: Understand the basics of C++ programs and its development environment. Develop a simple C++ program of the Console Application type using the Microsoft Visual C++. 1 Introduction The first lab of C++ will describe how to use the Integrated Development Environment (IDE) of the Microsoft Visual C (MSVC). The lab begins with a brief explanation of the IDE where it covers from the beginning of constructing a C++ program until the program s output. Then student will be introduced to the structure of a simple C++ program. Before writing the first C++ program using MSVC, basic command of output will be described. 1

2 1.1 Theoretical Background A Typical C++ Environment A C++ system generally consists of three parts which are; a program development environment, the language, and the C++ Standard Library. Let's consider the steps in creating and executing a C++ program using a C++ development environment (illustrated in Figure. 1.1). Every C++ program typically goes through these five steps: edit, compile, link, load and execute. 1. Text Editor Program is created in the editor and stored on a disk 2.1 Preprocessor 2. Compiler 2.2 Translator Preprocessor program process the code Converting the program to machine language (object code) 3. Linker Links the object code with C++ libraries to form an executable program 4. Loader On OS command (run), Loader will locate the executable program and reads it into memory to begins execution. Figure 1.1 A typical C++ program development environment. A program is also called source code. A preprocessor is another program that removes all comments and modifies source code, according to the commands called preprocessor directives (statements begin with #) found in the source code. 2

3 1.1.2 A simple C++ program Figure 1.2 shows the typical structure of a simple C++ program, with Program 1 as an example. Meanwhile, Table 1.1 describes the structure of Program 1. Comments 1 /*Program 1: This program demonstrates a simple C++ program*/ 1 Preprocessor Directive Namespace Global declaration Main function User-defined function (see Lab 4) #include <iostream> using namespace std; void fun(); int main() { cout<< Welcome to C++ Lab\n ; fun(); return 0; } void fun() { cout<< C++ is fun! ; cout<<endl; } Figure 1.2 Typical structure of a C++ program. Table 1.1 Descriptions of Program 1 s structure No. Description 1 Comments: Internal programming documentation. Comments will be ignored by compiler, and comments do not affect the way the program runs. Programmers use them to include short explanations or observations concerning the code or program. 2 Preprocessor Directive: The directive #include <iostream>, instructs the preprocessor to include a section of standard C++ code, from a header file known as iostream, that allows this program to perform standard input and output operations, such as writing the output of this program to the screen. 3 Namespace: Namespace is a collection of classess, objects, functions, variables, constant, and types that are enclosed in the C++ libraries. When you use using namespace std; you are instructing C++ compiler to use the 3

4 standard C++ library. 4 Global declaration: Declarations that are visible to all parts of the program. 5 Main function: The starting point of a C++ program s execution. Contains a group of statements (program instructions). Every C++ program must have this function. 6 User-defined functions: A group of statements that were defined by the programmer. All C++ statements except preprocessor directive end with a semicolon (;). C++ functions must have an opening and matching closing bracket. ( { <statement> } ) Basic Command of Output Below are some basic commands to understand your first program. cout<< : an output object that sends data to the standard output display device, e.g. monitor. Escape sequence: Consists of two characters. First is the backslash character (\), next the character that determines the interpretation of the escape sequence. Table 1.2 below lists some commonly used escape sequences. Table 1.2 Commonly used escape sequence Escape Sequence Name Description \a Audible alert Bell sound \t Horizontal Tab Takes the cursor to the next tab stop \n or endl New line Takes the cursor to the beginning of the next line \" Double Quote Displays a quotation mark (") \' Apostrophe Displays an apostrophe (') 4

5 \? Question mark Displays a question mark \\ Backslash Displays a backslash (\) 1.2 Step by step Examples Your First Program 1. Start the MS Visual Studio 2010 software from the task bar, that has the main window similar to the display below: 5

6 2. The first step in writing a Visual C++ program is to create a project for it. In the menu bar, click File -> New -> Project In the New Project dialog box shown below, select by clicking Visual C++ in the Installed Template pane and Win32 Console Application in the middle pane. 4. In the Name text box, give the project a name, e.g. Exercise1 5. In the Location text box (at the bottom of the dialog box), specify the path of the project (e.g. C:\LAB1), or select the location or folder in which you d like to store the project files by clicking Browse 6. Click OK. Note that there is no need to enter a name in the Solution name text box; the system fills the project name in it by default. 7. In the Win32 Application Wizard - Exercise1 dialog box, click Next > 6

7 8. In the Additional Options section, click the Empty Project check box. Then click Finish. 9. To create a new C++ program file, right click the Source Files Folder on the Solution Explorer. In the popup menu, click Add then New Item or in the menu bar, click Project -> Add -> New Item... 7

8 10. In the Installed Templates list of the Add New Item dialog box, make sure Visual C++ is selected. 11. In the middle pane, click C++ File (.cpp). 12. In the Name text box, give the C++ program file a name, e.g: Exercise. Then click Add. 13. You have just created an empty C++ source file (Exercise.cpp) and added it to the project (Exercise1). The main window will display the text editor, ready for editing 8

9 this new C++ file (Exercise.cpp). Fill the contents of file Exercise with the following C++ program: /*Program 1: This program demonstrate a simple C++ program*/ #include <iostream> using namespace std; int main() { cout << "Welcome to Lab 1!!!\n"; return 0; } 14. To compile and link the program, on the main menu, click Build -> Build Exercise1; or press Ctrl+F If there is no error in your project, the message : ====== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ===== is displayed in the Output window as shown below. 9

10 16. To load, execute and see the output, on the main menu, click Debug-> Start without Debugging; or press Ctrl+F5. 10

11 17. Displayed in a DOS window (C:\Windows\system32\cmd.exe) is the program s output: 18. After viewing the result, press any key on the keyboard to close the program s output and return to MSVC. Syntax error: Syntax is the set of rules for forming valid instructions. Syntax error violates the syntax of a language. Warning message: Message from a compiler advises the programmer that a statement is technically acceptable but might have a potential error. 11

12 1.3 Exercises 1. Write a program that can print as the following: University : Faculty : Programme : Universiti Teknikal Malaysia, Melaka FKE BENC My name is Muhammad Bin Abdul Rahman. I am from Johor. 2. Write programs that can give the following output: a) b) The background / foreground colour for the output screen, its font, size etc can be set in the output windows property. 12

13 1.4 Self-Review Questions 1. Give appropriate programming terms for the following: a) A program written in high-level or assembly language. b) A mistake that is a direct violation of the syntax rules. c) A program compiles and runs, but gives an incorrect output. d) It links object code with the C++ library to form executable code. e) The language, made up of binary coded instructions, that is used directly by the computer. f) A program that translates a program written in a high-level language into machine language. g) A machine language version of source code. h) An interactive program used to create and modify programs or data. i) Statement that begins with #. j) Punctuation that signifies the end of a C++ statement. Answer: a) b) c) d) e) f) g) h) i) j) 13

14 2. State whether the following statements is TRUE or FALSE. a) The purpose of a header file such as iostream is to store a program s source code. b) The C++ standard function to read data from keyboard is cin. c) In general, C++ statements are case-sensitive. d) One and only one function may be named main. e) Comments are used by the preprocessor to help format the program. 3. a) List the sequence of steps associated with implementing a program. b) State the difference between: c) Define the term test data. Answer: i. syntax error and logic error ii. logic error and run-time error iii. testing and desk checking 14

15 4. Give the output of the following program: //Program 1: This program that will print lines #include <iostream> using namespace std; int main() { cout << "ONE\tTWO"<<endl; cout << "THREE\n\n\FOUR\n\n\n"; cout << "\tfive\tsix"; cout << endl<<endl<<"seven EIGHT NINE\n\n"; return 0; } Output: 5. Give the output of the following program: //Program 2: This program will print asterisk #include <iostream> using namespace std; int main() { cout << "o\t*o\n"; cout << "***o\n*******o"; cout << "\n*****"; cout << "*******o\n*******************o\n"; return 0; } Output: 15

16 Introduction to Problem Solving By the end of this section, the students should be able to: Understand the basic techniques of problem solving. Develop structure chart, flowchart and pseudocode. Use Microsoft Visio 2003 for creating structure chart, flowchart and pseudocode. 2 Introduction Programming is a problem-solving activity. Problem solving method varies according to the subject area. For example, engineering problems are solved through scientific method, while business problems are solved using business system approach. For programming problem, it is solved using software development method. A basic software development method consists of four common steps as follows: a) Step 1: Analysis b) Step 2: Design the algorithm c) Step 3: Code d) Step 4: Test In Lab 2, you will learn the activities that need to be done in Step 1 and Step 2. 16

17 2.1 Theoretical Background Step 1: Analysis In this step, you have to highlight the problem statement and eliminate unimportant aspects in the problem. There are two main activities in this step: a) Identify what are the input, processes and output (IPO) of the problem. b) Draw a structure chart (if needed). The structure chart is developed if you want to use top-down design, in which you divide the problem into several sub problems Step 2: Design the Algorithm This step is to define the detail actions/instructions to solve the problem or subproblem, based on the IPOs outlined in Step 1. The actions consists of using the input, and executing a series of process in a specific order to produce the desired output. This group of actions is referred as algorithm. Algorithm is a procedure for solving a problem or subproblem in terms of the actions to be executed, and the order in which these actions are to be executed. Carefully prepared algorithm may be converted easily to program. Algorithm can be written in pseudocode or flowchart: a) Pseudocode Pseudocode is an informal language that helps programmers develop algorithms for programs or subroutines. It is not a programming language, and it is similar to everyday language. Its purpose is to describe in precise words the algorithm details. Pseudocode defines the steps of action to accomplish the tasks in sufficient details. 17

18 b) Flowchart A diagram that helps programmers develop algorithms for programs and subroutines. Table 2.1 shows the common flowchart s symbols. Table 2.13 Common flowchart s symbols Symbol Name Description Terminal/Terminator The beginning or end of a program. Process Computation or data manipulation. Data An input or output operation. Decision A program branch point. On-page reference/ Connector An entry to, or an exit from, another part of the flow chart. Predefined process Call for a function (subroutine). Flow lines Connecting the flowchart s symbols. Each symbol indicates the type of operation to be performed. Flowchart graphically illustrates the sequence in which the operations are to be performed (executed). Therefore the algorithm flow of execution is better understood with a flowchart. 18

19 There are three kinds of basic control structures, use in designing problem s solution: a) Sequence: is a series of statements that execute one after another. Statement 1 Statement 2 Statement 3 b) Selection (branch): is used to execute different statements depending on certain conditions. Eg: IF condition evaluates to true THEN executes statement1 ELSE executes statement2. Condition TRUE Statement 1 FALSE Statement 2 c) Repetition (loop or iteration): is used to repeat statements while certain conditions are met. CONDITION FALSE TRUE Statement 19

20 2.2 Step by step Examples Drawing flowchart using Microsoft Visio 1. Start Visio 2003 : Click Start -> Program -> Microsoft Office > Microsoft Visio 2. Choose the drawing type: On the Category section (on the left), click on the Flowchart folder, and select Basic Flowchart on template column. 20

21 3. A drawing canvas and basic flowchart s shapes will be shown. 4. To create a new symbol, (e.g. process symbol),, click on the process item and drag it on to the grid paper. 21

22 5. To save the drawing, click on File menu ->Save as -> click the Save button. 6. To add others drawing type. Click File ->New -> and select the drawing type you want. Other software that can be used to draw a flowchart is Dia( Dia is an open source application, and works cross platform. 22

23 2.3 Problem Solving (Step 1 and Step 2) Example 1: Employee s Salary (using top-down approach) Given the employee s working hour, a program will calculate and display the salary for an employee. The pay rate for an hour is RM Step 1: Analysis i. Identify input, process, and output. ii. a) Input: Employee s working hour, HOUR b) Process: i. Accept HOUR. ii. Calculate salary based on the pay rate per hour, SALARY = HOUR x 6.5 iii. Display SALARY. c) Output: SALARY Structure chart. Employee s salary problem Input the employee s working hour Calculate the salary Display the salary Step 2: Design the Algorithm i. Pseudocode of the main module of the Employee s Salary Problem: main() 1. Start 2. Call input() 3. Call calculatesalary() 4. Call display() 5. End ii. Pseudocode of submodule to input the employee s working hour: input() 1. Start 23

24 2. Read the employee s working hour, HOUR 3. Return iii. Pseudocode of submodule to calculate the employee s salary: calculatesalary() 1. Start 2. SALARY = 6.5*HOUR 3. Return iv. Pseudocode of submodule to display the employee s salary: display() 1. Start 2. Display SALARY 3. Return v. Flowchart of the Employee s Salary Problem: START input() displaysalary() input() Read employee s working hour, HOUR Print SALARY calculatesalary() RETURN RETURN displaysalary() calculatesalary() END SALARY = HOUR*6.5 RETURN 24

25 2.3.2 Example 2: Arithmetic Problem (not using top-down approach) A program will add two numbers and print a message whether the sum is an even number or an odd number. The program will repeat for five times. Step 1: Analysis i. Identify input, process, and output. a) Input: Five sets of two numbers, NUM1 and NUM2. b) Process: i. Accept two numbers, NUM1 and NUM2 ii. Add the two numbers, SUM = NUM1+NUM2 iii. Check SUM whether it is odd or even number. iv. Display the suitable message. v. Repeat the process for five times. c) Output: Message sum is an even number or sum is an odd number. Step 2: Design the Algorithm i. Pseudocode: 1. Start 2. Set COUNTER=0 3. While COUNTER < 5 4. Read two numbers: NUM1, NUM2 5. SUM = NUM1+ NUM2 6. If SUM is an even number Print EVEN NUMBER 7. Else Print ODD NUMBER 8. Add 1 to COUNTER 9. Endwhile 10. End 25

26 ii. Flowchart: START COUNTER = 0 COUNTER < 5? False END True READ NUM1, NUM2 SUM = NUM1 + NUM2 SUM IS EVEN NUMBER? True False PRINT ODD NUMBER PRINT EVEN NUMBER COUNTER = COUNTER

27 2.4 Exercises For each of the following problems, identify the problem inputs, processes, and problem outputs. Then write pseudocode and create flowchart for each solution. 1. A program will change a temperature reading from Celsius to Fahrenheit. Prompt the user to enter the temperature in degree Celsius and display the temperature in Fahrenheit. (Fahrenheit = 32 + (9*Celsius/5)) 2. Let a runner insert how many steps he has run in one minute, and the running duration in minutes and seconds. Find how many miles he has run. Assume each of his step length is 2.5 feet. (1 mile = 5280 feet) 3. Accept two numbers and find their sum and difference. If the sum is more than the difference, display the sum otherwise display the difference. 4. This is a program to calculate and display the body mass index (BMI). The BMI is to determine whether a person is overweight, underweight or has an optimal weight for his/her height. A person s BMI is calculated using the formula BMI = weight / height 2. A sedentary person s weight is considered to be optimal if his/her BMI is between 18.5 and 25. If the BMI is less than 18.5, the person is considered to be underweight. If the BMI value is greater than 25, the person is considered to be overweight.to determine the BMI, let the organiser enters the total number of people participating in this program. Then let each participant enters his/her weight (in kg) and height (in meters); and as a result, the participant gets a message indicating whether he/she has optimal weight, is underweight, or is overweight. Repeat the process for all participants. 5. Insert a few integer numbers between -100 and 100 (insert the value 0 to stop). Determine how many negative numbers and how many positive numbers have been inserted, the smallest number and the biggest number, their total and average. Print out what have been determined. (Consider to use the top-down approach in your design). 27

28 2.5 Self-Review Questions 1. Answer the questions below: a) The following are problem-solving stages. List the stages in their correct order. i. Analyse the problem ii. Suggest possible solutions iii. Test and validate the algorithm iv. Define the problem v. Represent the algorithm of the solution vi. Select the best solution b) Which one of these two is more difficult to do and should not be taken lightly, problem-solving or programming? Explain your answer. c) What is an algorithm? d) List four characteristics of a good algorithm. e) What are the three main things to identify before creating an algorithm? f) Is an algorithm the same as a program? g) What is the difference between pseudocode and flowchart? h) Which of the following are algorithms? i. Directions on a box for preparing one bowl of oatmeal. ii. List of nutrients in the oatmeal. iii. List of health recommendations for a long life. iv. Directions for filling out a registration form. v. Rules for dividing one number by another. i) Name three types of control structures that are commonly used to solve problem. 28

29 j) What is a top-down design approach? k) Explain why a top-down design approach is sometimes used to solve problems. l) A hotel wants to have a room booking system. When a guest arrives the receptionist would be able to see which rooms are free and allocate one to the guest. When the guest leaves, the room would be marked as free again. At any time the receptionist would be able to use the room number to find the name and home address of a guest, or could search the room system for a named guest. Draw a top-down design diagram (structure chart) to show how the room booking system could be developed. 2. Create a trace table to determine the output of the following pseudocode: a) Step 1: start Step 2: set x = 5 Step 3: set y = 10 Step 4: set z = 3 Step 5: set x = x + y Step 6: set y = x + z Step 7: set z = x + y + z Step 8: display x, y, z Step 9: stop 29

30 b) Step 1: start Step 2: set a = 10 Step 3: set b = 12 Step 4: set c = 23 Step 5: set a = a + b + c Step 6: set b = a - b Step 7: set c = b - c Step 8: if a>b then set m = a set n = b set p = c else set m = c set n = a set p = b endif Step 9: display m,n,p Step 10: stop c) Step 1: start Step 2: set a = 7 Step 3: set x = 1 Step 4: while a<>0 set x = x + a set a = a 1 endwhile Step 5: write x Step 6: end 30

31 d) Step 1: start Step 2: set x = 5 Step 3: for i = 1 to 10 set x = x + 5 endfor Step 4: write x Step 5: end e) Step 1: start Step 2: read 2 integers; A, B Step 3: do print A A = A - 1 while (A > B) Step 4: print B Step 5: stop (Note: Assume that the user enters 5 and 3) 3. Create flowchart for the pseudocode in question 2 a)-e). 31

32 Introduction to C++ By the end of this section, the students should be able to: Understand the fundamental data types, constant, variables, as well as operators and its precedence. Develop a simple C++ program. 3 Introduction C++ is a structured programming language, and considered as a high level language because it allows the programmer to concentrate on the problem and not worry about the machine that the program will be using. C++ is actually evolved from C over a period of several years. C was designed by Dennis Ritchie in While the evolve version of C is named C++ and largely the work of Bjarne Stroustrup. In 1997, American National Standard Institute (ANSI) and the International Standard Organization (ISO) jointly develop the standard for C++ which is being used until today. 32

33 3.1 Theoretical Background Data Types Data type is the classification of type of data, that identify a value. Standard data types are fundamental data types, which include: a) Integers (int/long/short) : whole number. E.g. 20, 100, 35 b) Floating point (float/double) : real number with integral and fractional part separated by decimal point. E.g. 3.14, , c) Character (char) : individual character value which enclosed in a set of single apostrophes. E.g. D, r, 4 d) Boolean (bool) : Value either true (nonzero) or false (zero) Identifiers Used to name constants, variables, function names and labels. Identifiers are formed by combining letters (both upper and lower case), digits and underscore. Rules : First character of an identifier must be letters or underscore ( _ ), casesensitive, blank spaces and special symbols are not allowed. Reserved word or keywords cannot be used to name an identifier. Examples of valid identifiers are: Nombor1, V, _ARUS, Proses( ), Rintangan_1, name, INT Examples of invalid identifiers are: 17th, COUNTER NUMBER, B*, int, return Variable: Identifiers whose value may change during execution. Variables represent memory location where values such as digits and characters can be stored. 33

34 3.1.3 Keyword/Reserved Word & Special Symbols A word that has special meaning in C++. Used only for their intended purpose. All reserved word appears in lowercase. E.g: const, goto, continue, do, double, else, float, while, int, switch Special symbols in C++ : [ ] ( ) { }, ; : * # Constant Values that do not change during execution of program. Two ways of constant declaration in a program: by using #define and reserved word const E.g: #define PI 3.14 #define IC "A " const double PI = 3.14; The naming of a constant should follow the rules of naming identifiers. 34

35 3.1.5 Operators A. Unary Operators Predecrement/Preincrement --r / ++r : decrease / increase r by 1 then use the new value of r for next execution Postdecrement/Postincrement r-- / r++ : the current value of r will be used first, and then only the r will be decrease / increase by 1 B. Arithmetic Operators Operation Symbol Syntax Addition + x + y Subtraction - x y Multiplication * x * y Division / x / y Modulus % x % y Plus + + x Minus - - x C. Assignment Operators Symbol Syntax Equal to = x = 5 - += x+=2 x = x + 2 -= x -=3 x = x 3 *= x*=7 x = x * 7 /= x/=5 x = x / 5 %= x%=4 x = x % 4 35

36 D. Relational Operators Operator Example True if Expression Value = = x = = y x and y are equal x = = y False (0)!= x!= y x and y are not equal x!= y True (1) > x > y x is greater than y x > y False (0) >= x >= y x is greater than or equal to x >= y False (0) < x < y x is less than y x < y True (1) <= x <= y x is less than or equal to y x <= y True (1) E. Logical Operators Operator Name Example True if && Conjunction x && y x and y are both true Disjunction x y x or y (or both) is true! Negation! x x is false Operator Precedence Hierarchy Operator category Operator Parentheses ( ) Unary ++ --! - Arithmetic * / % Arithmetic binary + - Relational < <= > >= Relational (Equality Operators) = =!= Logical AND && Logical OR Assignment = += -= *= /= %= 36

37 3.2 Step by step Examples Example 1: Arithmetic Operators /*Program 1: This program demonstrates C++ elements in converting distances from miles to kilometers*/ #include <iostream> using namespace std; #define MILE_TO_KM int main( ) { double miles, km; cout<<"enter the distance in miles: " <<endl; cin >> miles; km = miles * MILE_TO_KM; cout<<"\nthat equals " << km << " kilometers " <<endl ; return 0; } Output: Enter the distance in miles: 20 That equals kilometers Line Description 4 #include <iostream> Preprocesor directive 6 #define MILE_TO_KM MILE_TO_KM : constant declaration 8 int main( ) main : reserved word 10 double miles, km; miles, km : variables 14 km = miles * MILE_TO_KM; = : Assignment operator * : Arithmetic operator 16 return 0; return : reserved word 37

38 3.2.2 Example 2: Operator Precedence /*Program 2: This program demonstrate unary, arithmetic, assignment operator, and its precedence*/ #include <iostream> using namespace std; int main( ) { int a=5, b=6, c=7; int x, y = 3; x = --a * (3 + b) / 2 - c++ * b; y %= 5; y += c; cout << "Value of x is " << x << endl; cout << "Value of y is " << y <<endl ; cout << "Value of a is " << a << endl; cout << "Value of b is " << b <<endl ; cout << "Value of c is " << c << endl; return 0; } Output: Value of x is -24 Value of y is 11 Value of a is 4 Value of b is 6 Value of c is 8 Line Description 11 x = --a * (3 + b) / 2 c++ * b; Steps in evaluating the expression : 1. Parentheses : (3 + b) = = 9 2. Pre-unary operator : --a = --5 = 4 3. Multiplication (from left to right): 4*9 = Division (from left to right): 36/2 =18 5. Multiplication (from left to right): c*b=7*6=42 6. Subtraction: = Assign the result to x: x = Post-unary operator: c++ = 7++ = 8 Values after expression: a=4, b=6, c=8, x=-24, y=3 38

39 12 y %= 5 Steps in evaluating the expression : 1. y = y % 5 2. y = 3 % 5 = 3 y += c; Steps in evaluating the expression : 1. y = y y = = 11 Values after expression: a=4, b=6, c=8, x=-24, y= Example 3: Relational and Logical Operators /*Program 3 : This program demonstrate the relational and logical operators*/ #include <iostream> using namespace std; void main() { int num1, num2; cout << "Enter two integers to know " <<"the relationship that they satisfy"<< endl; cin >> num1 >> num2; if (num1 == num2) cout << num1 << " is equal to " << num2; else if ((num1!= num2) && (num1 < num2)) cout << num1 << " is less than " << num2; else if ((num1!= num2) && (num1 > num2)) cout << num1 << " is greater than " << num2; } Output: Enter two integers to know relationship that they satisfy: is greater than 12 39

40 Line Description cout << "Enter two integers to know " <<"the relationship that they satisfy"<< endl; Program prompt user to enter integers. 12 cin >> num1 >> num2; Read 2 integers, put in num1 and num2. 14 if (num1 == num2) Compares the values of num1 and num2 to test for equality. 15 cout << num1 << " is equal to " << num2; Display a line of text indicating that the numbers are equal. 16 else if ((num1!= num2) && (num1 < num2)) Compares the values of num1 and num2 to test for equality and to test whether num1 is smaller than num2 17 cout << num1 << " is less than " << num2; Display a line of text indicating that the num1 is less than num2 18 else if ((num1!= num2) && (num1 > num2)) Compares the values of num1 and num2 to test for equality and to test whether num1 is larger than num2 19 cout << num1 << " is greater than " << num2; Display a line of text indicating that the num1 is larger than num2 40

41 3.3 Exercises 1. Code the variables definition and the process for each of the following: a) A floating point named price initialized to b) An integer named quantity. c) A constant integer named DISC initialized to 10. d) A floating point name totalprice. e) Get the quantity purchased from the user and then, calculate the totalprice using the following formula: totalprice = (price*quantity) * (100-DISC)/100.00; 2. Write a program that prompts the user to enter the total time in seconds and displays the hours, minutes and seconds for the total time. Declare two constant integers named SCND_TO_HOUR initialized to 3600 and SCND_TO_MINUTE initialized to 60. For example, 5000 seconds are equal to 1 hour, 23 minutes and 20 seconds. Here is a sample run: Input the total time in seconds : seconds are equal to 1 hours, 23 minutes and 20 seconds. 3. Write a program that reads in loan amount, annual interest rate and number of years for the loan and display monthly payment the customer should pay for the loan. Here is a sample run: Input loan amount (RM): Input annual interest rate (%) : 4.2 Input number of years : 10 Monthly payment for this loan is (RM):

42 4. In physics, an object that is in motion is said to have kinetic energy. The following formula can be used to determine a moving object s kinetic energy: KE = (1/2 mv 2 ) Where KE =the kinetic energy m = the object s mass in kilograms v = the object s velocity in meters per second. Write a program to calculate the amount of kinetic energy that the object has when given the object s mass (in kilograms) and velocity (in meters per second). Here is a sample run: Input the object s mass (kg):10.0 Input the object s velocity (m/s): 5 The kinetic energy (Joules):

43 3.4 Self-Review Questions 1. Indicate whether the following identifiers VALID or INVALID. Explain if invalid. Identifier Valid / Invalid? Why Invalid? employee name _int_ tot&salary $RM number1 3rdnumber else EmpPosition number_2 double 2. The logical operator is true only when at least one of the operands is TRUE. 3. If x=false, y=true, and z= true, what is the value of each of the following expressions? a) x && y z b) x y && z c) (x && y) z d) (x y) && z e) (x && z) y 4. Show the value of x after each statement is performed: a) x = * 8 /(2 1); b) x = 6 % (4 + 2) * 4 2 / 2; 43

44 c) x = ( 3 * 7 * ( 3 + ( 2 * 12 / (4) ) ) ); d) x = 6 * (3 + 18) / 2 5 * 4; 5. Find any errors in the following program: integer main() { int 1num, num 2, num3; character id; 1num + num 2 = num3; cout << id << num3 << \n; return; } 6. If originally x=6, y=3, and z=2, what is the value of each of the following expression? a) x - z * 6 / y b) y++ * (x + y) % z + y c) ++y + z-- + x++ d) x++ 3 * ++z + 2 e) ++x 2 * (5 - ++z) + y 44

BITG 1233: Introduction to C++

BITG 1233: Introduction to C++ BITG 1233: Introduction to C++ 1 Learning Outcomes At the end of this lecture, you should be able to: Identify basic structure of C++ program (pg 3) Describe the concepts of : Character set. (pg 11) Token

More information

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Parts of a C++ Program Parts of a C++ Program // sample C++ program

More information

Chapter 1 Introduction to Computers and C++ Programming

Chapter 1 Introduction to Computers and C++ Programming Chapter 1 Introduction to Computers and C++ Programming 1 Outline 1.1 Introduction 1.2 What is a Computer? 1.3 Computer Organization 1.7 History of C and C++ 1.14 Basics of a Typical C++ Environment 1.20

More information

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

More information

Objectives. In this chapter, you will:

Objectives. In this chapter, you will: Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about

More information

6.096 Introduction to C++ January (IAP) 2009

6.096 Introduction to C++ January (IAP) 2009 MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Welcome to 6.096 Lecture

More information

Introduction to Programming

Introduction to Programming Introduction to Programming session 5 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2010 These slides are created using Deitel s slides Sahrif University of Technology Outlines

More information

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm. CHAPTER 1&2 OBJECTIVES After completing this chapter, you will be able to: Understand the basics and Advantages of an algorithm. Analysis various algorithms. Understand a flowchart. Steps involved in designing

More information

Creating a C++ Program

Creating a C++ Program Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer. 1 Creating a C++ Program created using an

More information

Chapter 2: Overview of C++

Chapter 2: Overview of C++ Chapter 2: Overview of C++ Problem Solving, Abstraction, and Design using C++ 6e by Frank L. Friedman and Elliot B. Koffman C++ Background Introduced by Bjarne Stroustrup of AT&T s Bell Laboratories in

More information

Chapter 1 & 2 Introduction to C Language

Chapter 1 & 2 Introduction to C Language 1 Chapter 1 & 2 Introduction to C Language Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 1 & 2 - Introduction to C Language 2 Outline 1.1 The History

More information

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Spring 2005 Lecture 1 Jan 6, 2005 Course Information 2 Lecture: James B D Joshi Tuesdays/Thursdays: 1:00-2:15 PM Office Hours:

More information

Chapter 2: Introduction to C++

Chapter 2: Introduction to C++ Chapter 2: Introduction to C++ Copyright 2010 Pearson Education, Inc. Copyright Publishing as 2010 Pearson Pearson Addison-Wesley Education, Inc. Publishing as Pearson Addison-Wesley 2.1 Parts of a C++

More information

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Copyright 2009 Publishing Pearson as Pearson Education, Addison-Wesley Inc. Publishing as Pearson Addison-Wesley

More information

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++ Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++ 0.1 Introduction This is a session to familiarize working with the Visual Studio development environment. It

More information

A First Program - Greeting.cpp

A First Program - Greeting.cpp C++ Basics A First Program - Greeting.cpp Preprocessor directives Function named main() indicates start of program // Program: Display greetings #include using namespace std; int main() { cout

More information

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program 1 By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program variables. Apply C++ syntax rules to declare variables, initialize

More information

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1 NAGERCOIL COMPUTER SCIENCE Grade: IX C++ PROGRAMMING 1 C++ 1. Object Oriented Programming OOP is Object Oriented Programming. It was developed to overcome the flaws of the procedural approach to programming.

More information

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York CSc 10200! Introduction to Computing Lecture 2-3 Edgardo Molina Fall 2013 City College of New York 1 C++ for Engineers and Scientists Third Edition Chapter 2 Problem Solving Using C++ 2 Objectives In this

More information

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

PART I.   Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++. Unit - III CHAPTER - 9 INTRODUCTION TO C++ Choose the correct answer. PART I 1. Who developed C++? (a) Charles Babbage (b) Bjarne Stroustrup (c) Bill Gates (d) Sundar Pichai 2. What was the original name

More information

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language 1 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC

More information

Chapter 2 Basic Elements of C++

Chapter 2 Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2-1 Chapter 2 Basic Elements of C++ At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion

More information

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

Introduction to C++ Programming. Adhi Harmoko S, M.Komp Introduction to C++ Programming Adhi Harmoko S, M.Komp Machine Languages, Assembly Languages, and High-level Languages Three types of programming languages Machine languages Strings of numbers giving machine

More information

Lab # 02. Basic Elements of C++ _ Part1

Lab # 02. Basic Elements of C++ _ Part1 Lab # 02 Basic Elements of C++ _ Part1 Lab Objectives: After performing this lab, the students should be able to: Become familiar with the basic components of a C++ program, including functions, special

More information

C: How to Program. Week /Mar/05

C: How to Program. Week /Mar/05 1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers

More information

Chapter 1 INTRODUCTION

Chapter 1 INTRODUCTION Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware consists of the physical components of the system. The software is the collection of programs that a computer

More information

Fundamentals of Programming CS-110. Lecture 2

Fundamentals of Programming CS-110. Lecture 2 Fundamentals of Programming CS-110 Lecture 2 Last Lab // Example program #include using namespace std; int main() { cout

More information

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols. EEE-117 COMPUTER PROGRAMMING Basic Elements of C++ Objectives General Questions Become familiar with the basic components of a C++ program functions, special symbols, and identifiers Data types Arithmetic

More information

The births of the generations are as follow. First generation, 1945 machine language Second generation, mid 1950s assembly language.

The births of the generations are as follow. First generation, 1945 machine language Second generation, mid 1950s assembly language. Lesson Outcomes At the end of this chapter, student should be able to: Describe what a computer program is Explain the importance of programming to computer use Appreciate the importance of good programs

More information

CHAPTER 3 BASIC INSTRUCTION OF C++

CHAPTER 3 BASIC INSTRUCTION OF C++ CHAPTER 3 BASIC INSTRUCTION OF C++ MOHD HATTA BIN HJ MOHAMED ALI Computer programming (BFC 20802) Subtopics 2 Parts of a C++ Program Classes and Objects The #include Directive Variables and Literals Identifiers

More information

IT 374 C# and Applications/ IT695 C# Data Structures

IT 374 C# and Applications/ IT695 C# Data Structures IT 374 C# and Applications/ IT695 C# Data Structures Module 2.1: Introduction to C# App Programming Xianrong (Shawn) Zheng Spring 2017 1 Outline Introduction Creating a Simple App String Interpolation

More information

REVIEW. The C++ Programming Language. CS 151 Review #2

REVIEW. The C++ Programming Language. CS 151 Review #2 REVIEW The C++ Programming Language Computer programming courses generally concentrate on program design that can be applied to any number of programming languages on the market. It is imperative, however,

More information

Computer Programming : C++

Computer Programming : C++ The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming : C++ Experiment #1 Basics Contents Structure of a program

More information

Chapter 2 - Introduction to C Programming

Chapter 2 - Introduction to C Programming Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic

More information

download instant at Introduction to C++

download instant at  Introduction to C++ Introduction to C++ 2 Programming: Solutions What s in a name? that which we call a rose By any other name would smell as sweet. William Shakespeare When faced with a decision, I always ask, What would

More information

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3 Programming - 1 Computer Science Department 011COMP-3 لغة البرمجة 1 011 عال- 3 لطالب كلية الحاسب اآللي ونظم المعلومات 1 1.1 Machine Language A computer programming language which has binary instructions

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine

More information

LECTURE 02 INTRODUCTION TO C++

LECTURE 02 INTRODUCTION TO C++ PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 02 INTRODUCTION

More information

Lecture 3 Tao Wang 1

Lecture 3 Tao Wang 1 Lecture 3 Tao Wang 1 Objectives In this chapter, you will learn about: Arithmetic operations Variables and declaration statements Program input using the cin object Common programming errors C++ for Engineers

More information

The C++ Language. Arizona State University 1

The C++ Language. Arizona State University 1 The C++ Language CSE100 Principles of Programming with C++ (based off Chapter 2 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

More information

CHAPTER-6 GETTING STARTED WITH C++

CHAPTER-6 GETTING STARTED WITH C++ CHAPTER-6 GETTING STARTED WITH C++ TYPE A : VERY SHORT ANSWER QUESTIONS 1. Who was developer of C++? Ans. The C++ programming language was developed at AT&T Bell Laboratories in the early 1980s by Bjarne

More information

Introduction to Programming EC-105. Lecture 2

Introduction to Programming EC-105. Lecture 2 Introduction to Programming EC-105 Lecture 2 Input and Output A data stream is a sequence of data - Typically in the form of characters or numbers An input stream is data for the program to use - Typically

More information

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja

More information

CS242 COMPUTER PROGRAMMING

CS242 COMPUTER PROGRAMMING CS242 COMPUTER PROGRAMMING I.Safa a Alawneh Variables Outline 2 Data Type C++ Built-in Data Types o o o o bool Data Type char Data Type int Data Type Floating-Point Data Types Variable Declaration Initializing

More information

Chapter 3 Problem Solving and the Computer

Chapter 3 Problem Solving and the Computer Chapter 3 Problem Solving and the Computer An algorithm is a step-by-step operations that the CPU must execute in order to solve a problem, or to perform that task. A program is the specification of an

More information

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics 1 Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Slide 2-3 2.1 Variables and Assignments 2

More information

VARIABLES & ASSIGNMENTS

VARIABLES & ASSIGNMENTS Fall 2018 CS150 - Intro to CS I 1 VARIABLES & ASSIGNMENTS Sections 2.1, 2.2, 2.3, 2.4 Fall 2018 CS150 - Intro to CS I 2 Variables Named storage location for holding data named piece of memory You need

More information

Your First C++ Program. September 1, 2010

Your First C++ Program. September 1, 2010 Your First C++ Program September 1, 2010 Your First C++ Program //*********************************************************** // File name: hello.cpp // Author: Bob Smith // Date: 09/01/2010 // Purpose:

More information

Introduction to the C++ Programming Language

Introduction to the C++ Programming Language LESSON SET 2 Introduction to the C++ Programming Language OBJECTIVES FOR STUDENT Lesson 2A: 1. To learn the basic components of a C++ program 2. To gain a basic knowledge of how memory is used in programming

More information

1. C++ Overview. C++ Program Structure. Data Types. Assignment Statements. Input/Output Operations. Arithmetic Expressions.

1. C++ Overview. C++ Program Structure. Data Types. Assignment Statements. Input/Output Operations. Arithmetic Expressions. 1. C++ Overview 1. C++ Overview C++ Program Structure. Data Types. Assignment Statements. Input/Output Operations. Arithmetic Expressions. Interactive Mode, Batch Mode and Data Files. Common Programming

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

BCA-105 C Language What is C? History of C

BCA-105 C Language What is C? History of C C Language What is C? C is a programming language developed at AT & T s Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie. C seems so popular is because it is

More information

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char Week 1 Operators, Data Types & I/O Gaddis: Chapters 1, 2, 3 CS 5301 Fall 2016 Jill Seaman Programming A program is a set of instructions that the computer follows to perform a task It must be translated

More information

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING KEY CONCEPTS COMP 10 EXPLORING COMPUTER SCIENCE Lecture 2 Variables, Types, and Programs Problem Definition of task to be performed (by a computer) Algorithm A particular sequence of steps that will solve

More information

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

Introduction to C++ Programming Pearson Education, Inc. All rights reserved. 1 2 Introduction to C++ Programming 2 What s in a name? that which we call a rose By any other name would smell as sweet. William Shakespeare When faced with a decision, I always ask, What would be the

More information

BASIC ELEMENTS OF A COMPUTER PROGRAM

BASIC ELEMENTS OF A COMPUTER PROGRAM BASIC ELEMENTS OF A COMPUTER PROGRAM CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING LOGO Contents 1 Identifier 2 3 Rules for naming and declaring data variables Basic data types 4 Arithmetic operators

More information

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Introduction to C Programming Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline Printing texts Adding 2 integers Comparing 2 integers C.E.,

More information

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style 3 2.1 Variables and Assignments Variables and

More information

Chapter 2. C++ Basics

Chapter 2. C++ Basics Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Slide 2-2 2.1 Variables and Assignments Variables

More information

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Copyright 2011 Pearson Addison-Wesley. All rights

More information

UEE1302 (1102) F10: Introduction to Computers and Programming

UEE1302 (1102) F10: Introduction to Computers and Programming Computational Intelligence on Automation Lab @ NCTU Learning Objectives UEE1302 (1102) F10: Introduction to Computers and Programming Programming Lecture 00 Programming by Example Introduction to C++ Origins,

More information

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING Dr. Shady Yehia Elmashad Outline 1. Introduction to C++ Programming 2. Comment 3. Variables and Constants 4. Basic C++ Data Types 5. Simple Program: Printing

More information

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++ CHAPTER 9 C++ 1. WRITE ABOUT THE BINARY OPERATORS USED IN C++? ARITHMETIC OPERATORS: Arithmetic operators perform simple arithmetic operations like addition, subtraction, multiplication, division etc.,

More information

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq CS 241 Computer Programming Introduction Teacher Assistant Hadeel Al-Ateeq 1 2 Course URL: http://241cs.wordpress.com/ Hadeel Al-Ateeq 3 Textbook HOW TO PROGRAM BY C++ DEITEL AND DEITEL, Seventh edition.

More information

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 110 PROGRAMMING FUNDAMENTALS CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 2. Overview of C++ Prof. Amr Goneid, AUC 1 Overview of C++ Prof. Amr Goneid, AUC 2 Overview of C++ Historical C++ Basics Some Library

More information

Introduction to C# Applications

Introduction to C# Applications 1 2 3 Introduction to C# Applications OBJECTIVES To write simple C# applications To write statements that input and output data to the screen. To declare and use data of various types. To write decision-making

More information

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay C++ Basics Data Processing Course, I. Hrivnacova, IPN Orsay The First Program Comments Function main() Input and Output Namespaces Variables Fundamental Types Operators Control constructs 1 C++ Programming

More information

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program Overview - General Data Types - Categories of Words - The Three S s - Define Before Use - End of Statement - My First Program a description of data, defining a set of valid values and operations List of

More information

2 nd Week Lecture Notes

2 nd Week Lecture Notes 2 nd Week Lecture Notes Scope of variables All the variables that we intend to use in a program must have been declared with its type specifier in an earlier point in the code, like we did in the previous

More information

1. In C++, reserved words are the same as predefined identifiers. a. True

1. In C++, reserved words are the same as predefined identifiers. a. True C++ Programming From Problem Analysis to Program Design 8th Edition Malik TEST BANK Full clear download (no formatting errors) at: https://testbankreal.com/download/c-programming-problem-analysis-program-design-8thedition-malik-test-bank/

More information

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath UNIT - I Introduction to C Programming Introduction to C C was originally developed in the year 1970s by Dennis Ritchie at Bell Laboratories, Inc. C is a general-purpose programming language. It has been

More information

Getting started with C++ (Part 2)

Getting started with C++ (Part 2) Getting started with C++ (Part 2) CS427: Elements of Software Engineering Lecture 2.2 11am, 16 Jan 2012 CS427 Getting started with C++ (Part 2) 1/22 Outline 1 Recall from last week... 2 Recall: Output

More information

Differentiate Between Keywords and Identifiers

Differentiate Between Keywords and Identifiers History of C? Why we use C programming language Martin Richards developed a high-level computer language called BCPL in the year 1967. The intention was to develop a language for writing an operating system(os)

More information

1.1 Introduction to C Language. Department of CSE

1.1 Introduction to C Language. Department of CSE 1.1 Introduction to C Language 1 Department of CSE Objectives To understand the structure of a C-Language Program To write a minimal C program To introduce the include preprocessor command To be able to

More information

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are

More information

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++ CS101: Fundamentals of Computer Programming Dr. Tejada stejada@usc.edu www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++ 10 Stacks of Coins You have 10 stacks with 10 coins each that look and feel

More information

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

Unit-II Programming and Problem Solving (BE1/4 CSE-2) Unit-II Programming and Problem Solving (BE1/4 CSE-2) Problem Solving: Algorithm: It is a part of the plan for the computer program. An algorithm is an effective procedure for solving a problem in a finite

More information

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

A Freshman C++ Programming Course

A Freshman C++ Programming Course A Freshman C++ Programming Course Dr. Ali H. Al-Saedi Al-Mustansiria University, Baghdad, Iraq January 2, 2018 1 Number Systems and Base Conversions Before studying any programming languages, students

More information

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.1

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.1 Superior University Department of Electrical Engineering CS-115 Computing Fundamentals Experiment No.1 Introduction of Compiler, Comments, Program Structure, Input Output, Data Types and Arithmetic Operators

More information

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol. 1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol. B. Outputs to the console a floating point number f1 in scientific format

More information

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM Contents 1. Statements 2. C++ Program Structure 3. Programming process 4. Control Structure STATEMENTS ASSIGNMENT STATEMENTS Assignment statement Assigns a value

More information

Introduction to Programming

Introduction to Programming Introduction to Programming session 6 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Spring 2011 These slides are created using Deitel s slides Sharif University of Technology Outlines

More information

DEPARTMENT OF MATHS, MJ COLLEGE

DEPARTMENT OF MATHS, MJ COLLEGE T. Y. B.Sc. Mathematics MTH- 356 (A) : Programming in C Unit 1 : Basic Concepts Syllabus : Introduction, Character set, C token, Keywords, Constants, Variables, Data types, Symbolic constants, Over flow,

More information

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards Language Reference Manual Introduction The purpose of

More information

Programming. C++ Basics

Programming. C++ Basics Programming C++ Basics Introduction to C++ C is a programming language developed in the 1970s with the UNIX operating system C programs are efficient and portable across different hardware platforms C++

More information

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++ CSCE 206: Structured Programming in C++ 2017 Spring Exam 1 Monday, February 13, 2017 Total - 100 Points B Instructions: Total of 11 pages, including this cover and the last page. Before starting the exam,

More information

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++ CSCE 206: Structured Programming in C++ 2017 Spring Exam 1 Monday, February 13, 2017 Total - 100 Points A Instructions: Total of 11 pages, including this cover and the last page. Before starting the exam,

More information

Chapter 2 C++ Fundamentals

Chapter 2 C++ Fundamentals Chapter 2 C++ Fundamentals 3rd Edition Computing Fundamentals with C++ Rick Mercer Franklin, Beedle & Associates Goals Reuse existing code in your programs with #include Obtain input data from the user

More information

Flow of Control. bool Data Type. Flow Of Control. Expressions. C++ Control Structures < <= > >= ==!= ! &&

Flow of Control. bool Data Type. Flow Of Control. Expressions. C++ Control Structures < <= > >= ==!= ! && Flow of Control Sequential unless a control structure is used to change the order Conditions, Logical s, And Selection Control Structures Two general types of control structures Selection (also called

More information

Introduction to C++ IT 1033: Fundamentals of Programming

Introduction to C++ IT 1033: Fundamentals of Programming 2 Introduction to C++ IT 1033: Fundamentals of Programming Budditha Hettige Department of Computer Science C++ C++ is a middle-level programming language Developed by Bjarne Stroustrup Starting in 1979

More information

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program? Intro to Programming & C++ Unit 1 Sections 1.1-4 and 2.1-10, 2.12-13, 2.15-17 CS 1428 Spring 2019 Jill Seaman 1.1 Why Program? Computer programmable machine designed to follow instructions Program a set

More information

CSCI 123 Introduction to Programming Concepts in C++

CSCI 123 Introduction to Programming Concepts in C++ CSCI 123 Introduction to Programming Concepts in C++ Brad Rippe C++ Basics C++ layout Include directive #include using namespace std; int main() { } statement1; statement; return 0; Every program

More information

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual Contents 1 Introduction...2 2 Lexical Conventions...2 3 Types...3 4 Syntax...3 5 Expressions...4 6 Declarations...8 7 Statements...9

More information

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah Lecturer Department of Computer Science & IT University of Balochistan 1 Outline p Introduction p Program development p C language and beginning with

More information

Expressions, Input, Output and Data Type Conversions

Expressions, Input, Output and Data Type Conversions L E S S O N S E T 3 Expressions, Input, Output and Data Type Conversions PURPOSE 1. To learn input and formatted output statements 2. To learn data type conversions (coercion and casting) 3. To work with

More information

Tutorial-2a: First steps with C++ programming

Tutorial-2a: First steps with C++ programming Programming for Scientists Tutorial 2a 1 / 18 HTTP://WWW.HEP.LU.SE/COURSES/MNXB01 Introduction to Programming and Computing for Scientists Tutorial-2a: First steps with C++ programming Programming for

More information