Introduction to C++ Programming

Save this PDF as:
 WORD  PNG  TXT  JPG

Size: px
Start display at page:

Download "Introduction to C++ Programming"

Transcription

1 2 Introduction to C++ Programming OBJECTIVES In this chapter you will learn: To write simple computer programs in C++. To write simple input and output statements. To use fundamental types. Basic computer memory concepts. To use arithmetic operators. The precedence of arithmetic operators. To write simple decision-making statements. Aristophanes

2

3 Chapter 2 Introduction to C++ Programming 3 Assignment Checklist Date: Section: Exercises Assigned: Circle assignments Date Due Prelab Activities Matching YES NO Fill in the Blank 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 Short Answer 26, 27, 28, 29, 30, 31, 32 Programming Output 33, 34, 35, 36, 37, 38, 39 Correct the Code 40, 41, 42, 43, 44, 45 Lab Exercises Exercise 1 Sum, Average, Product, Smallest and Largest YES Follow-Up Questions and Activities 1, 2 NO Exercise 2 Multiples YES NO Follow-Up Questions and Activities 1, 2, 3, 4 Exercise 3 Separating Digits YES NO Follow-Up Questions and Activities 1, 2, 3 Debugging YES NO Labs Provided by Instructor Postlab Activities Coding Exercises 1, 2, 3, 4, 5, 6, 7 Programming Challenges 1, 2, 3

4

5 Chapter 2 Introduction to C++ Programming 5 Prelab Activities Matching Date: Section: After reading Chapter 2 of C++ How to Program: Fifth Edition, answer the given questions. These questions are intended to test and reinforce your understanding of key concepts and may be done either before the lab or during the lab. For each term in the column on the left, write the corresponding letter for the description that best matches it from the column on the right. Term Description D H I G A L J E B C M K F 1. Integer division 2. Stream extraction operator 3. return 4. Modulus operator 5. A variable of type int 6. Comments 7. Stream insertion operator 8. Preprocessor directive 9. std::endl stream manipulator 10. Semicolon 11. Conditions in if statements 12. Newline escape sequence 13. Syntax error a) Holds whole number values. b) Outputs a newline and flushes the output buffer. c) Appears at the end of every statement. d) An operation that truncates any fractional part of its result. e) Instruction that is performed before the program is compiled. f) Prevents a program from compiling. g) An operation that yields the remainder after integer division. h) >>. i) One of several means to exit a function. j) <<. k) '\n'. l) Text that documents programs and improves their readability. m) Commonly formed by using equality operators and relational operators.

6

7 Chapter 2 Introduction to C++ Programming 7 Prelab Activities Fill in the Blank Fill in the Blank Date: Section: Fill in the blanks in each of the following statements: 14. Both comments and white space are ignored by the C++ compiler. 15. A backslash is combined with the next character to form a(n) escape sequence. 16. A(n) variable is a location in the computer s memory where a value can be stored for use by a program. 17. Output and input in C++ are accomplished with streams of characters. 18. Single-line comments begin with //. 19. main is the first function executed in a C++ program. 20. All variables in a C++ program must be declared before they are used. 21. cin represents the standard input stream. 22. cout represents the standard output stream. 23. The if statement allows a program to make a decision. 24. C++ evaluates arithmetic expressions in a precise sequence determined by the rules of operator precedence and associativity. 25. Equality operators and relational operators can be used in if conditions.

8

9 Chapter 2 Introduction to C++ Programming 9 Prelab Activities Short Answer Short Answer Date: Section: In the space provided, answer each of the given questions. Your answers should be as concise as possible; aim for two or three sentences. 26. What is the difference between stream insertion and stream extraction? What is each used for? Stream insertion is used to insert characters and values into a stream, such as the standard output stream to display data on the screen. Stream extraction is used to extract characters and values from a stream, such as the standard input stream to input data from the keyboard. 27. What is a syntax error? Give an example. A syntax error is a violation of C++ s language rules in the program code. Syntax errors are normally accompanied by error messages from the compiler to help the programmer locate and fix the incorrect code. The program cannot be executed until all syntax errors are corrected. Some examples of syntax errors include forgetting the semicolon at the end of a statement, placing two variable identifiers next to each other without an intervening operator and not closing a parenthetical expression with a right parentheses. 28. What is a logic error? Give an example. A logic error is a mistake in the program code that causes the program to produce incorrect results while executing. A logic error will not be reported by the compiler. Some examples of logic errors include using the incorrect variable in a calculation, using the assignment operator = instead of the equality operator == and spelling a word incorrectly in a message to the user. 29. What are operator precedence and associativity? How do they affect program execution? Operator precedence is the order in which the operations in a statement containing multiple operations will execute. For example, multiplication operations execute before addition operations, unless the addition operations are enclosed within parentheses. Associativity is the order in which multiple operations with the same precedence execute. For example, addition operations are performed from left to right, while assignment operations are performed from right to left. Operator precedence and associativity determine the order in which operations execute and therefore affect the values that are used for each operation. 30. What are redundant parentheses? When might a programmer use them? Redundant parentheses are parentheses in a complex arithmetic expression that do not alter the order of execution for the operations in that arithmetic expression. A programmer might use redundant parentheses to make the order of execution of the expression more clear. For example, parentheses could be placed around the multiplication operations in an arithmetic expression containing both multiplication and addition operations, even though the multiplication operations would be performed first anyway.

10 10 Introduction to C++ Programming Chapter 2 Prelab Activities Short Answer 31. Write an example of a preprocessor directive. #include <iostream> 32. What is a variable? How are they used in computer programs? A variable is a location in the computer s memory. Variables enable a computer program to store values, possibly the results of earlier calculations, for use later later in the program.

11 Chapter 2 Introduction to C++ Programming 11 Prelab Activities Programming Output Programming Output Date: Section: For each of the given program segments, read the code and write the output in the space provided below each program. [ Note: Do not execute these programs on a computer.] 33. What is the output of the following program? 1 #include <iostream> 2 3 using std::cout; 4 using std::endl; 5 6 int main() 7 { 8 int x; 9 int y; x = 30; 12 y = 2 ; 13 cout << x * y + 9 / 3 << endl; return 0 ; } // end main Your answer: What is output by the following line of code? 1 cout << ( 8 * 4 * ) / ; Your answer: 39

12 12 Introduction to C++ Programming Chapter 2 Prelab Activities Programming Output For Programming Output Exercises 35 and 36, use the program in Fig. L #include <iostream> 2 3 using std::cout; 4 using std::cin; 5 using std::endl; 6 7 int main() 8 { 9 int input; cout << "Please enter an integer: " ; 12 cin >> input; if ( input!= 7 ) 15 cout << "Hello" << endl; if ( input == 7 ) 18 cout << "Goodbye" << endl; return 0 ; } // end main Fig. L 2.1 Program used for Programming Output Exercises 35 and What is output by the program in Fig. L 2.1? Assume that the user enters 5 for input. Your answer: Hello 36. What is output by the program in Fig. L 2.1? Assume that the user enters 7 for input. Your answer: Goodbye

13 Chapter 2 Introduction to C++ Programming 13 Prelab Activities Programming Output For Programming Output Exercises 37 and 38, use the program in Fig. L #include <iostream> 2 3 using std::cout; 4 using std::cin; 5 using std::endl; 6 7 int main() 8 { 9 int input; cout << "Please enter an integer: " ; 12 cin >> input; if ( input >= 0 ) 15 cout << "Hello" << endl; cout << "Goodbye" << endl; return 0 ; } // end main Fig. L 2.2 Program used for Programming Output Exercises37 and What is output by the program in Fig. L 2.2? Assume the user enters 2 for input. Your answer: Hello Goodbye 38. What is output by the program in Fig. L 2.2? Assume the user enters -2 for input. Your answer: Goodbye

14 14 Introduction to C++ Programming Chapter 2 Prelab Activities Programming Output 39. What is output by the following program? 1 #include <iostream> 2 3 using std::cout; 4 using std::cin; 5 using std::endl; 6 7 int main() 8 { 9 int x = 3 ; 10 int y = 9 ; 11 int z = 77; if ( x == ( y / 3 ) ) 14 cout << "H" ; if ( z!= 77 ) 17 cout << "q"; if ( z == 77 ) 20 cout << "e"; if ( z * y + x < 0 ) 23 cout << "g"; if ( y == ( x * x ) ) 26 cout << "ll"; cout << "o!" << endl; return 0 ; } // end main Your answer: Hello!

15 Chapter 2 Introduction to C++ Programming 15 Prelab Activities Correct the Code Correct the Code Date: Section: For each of the given program segments, determine if there is an error in the code. If there is an error, specify whether it is a logic, syntax or compilation error, circle the error in the code and write the corrected code in the space provided after each problem. If the code does not contain an error, write no error. For code segments, assume the code appears in main and that using directives are provided for cin, cout and endl. [ Note : It is possible that a program segment may contain multiple errors.] 40. The following program should print an integer to the screen: 1 #include <iostream>; 2 3 using std::cout 4 using std::endl 5 6 int main() 7 { 8 int x = 30; 9 int y = 2 ; cout << x * y + 9 / 3 << endl; return 0 ; } // end main Your answer: 1 #include <iostream> 2 3 using std::cout; 4 using std::endl; 5 6 int main() 7 { 8 int x = 30; 9 int y = 2 ; cout << x * y + 9 / 3 << endl; return 0 ; } // end main Errors: Line 1: There should not be a semicolon after a #include preprocessor directive. Compilation error. Lines 3 4: There should be semicolons after using directives. Compilation error.

16 16 Introduction to C++ Programming Chapter 2 Prelab Activities Correct the Code 41. The following code should declare an integer variable and assign it the value 6. 1 int 1stPlace 2 1stPlace = 6 ; Your answer: 1 int firstplace ; 2 firstplace = 6 ; Errors: Lines 1 2: Variable names cannot begin with digits. The name of the variable must be changed. Compilation error. Line 1: A variable declaration should be followed by a semicolon. Compilation error. 42. The following code should determine whether variable x is less than or equal to 9. 1 int x = 9 ; 2 3 if ( x < = 9 ) 4 cout << "Less than or equal to."; Your answer: 1 int x = 9 ; 2 3 if ( x <= 9 ) 4 cout << "Less than or equal to."; Errors: Line 3: Placing a space between < and = is a syntax error. 43. The following code should determine whether q is equal to int q = 10 ; 2 3 cout << "q is: " << q << endl; 4 5 if ( q = 10 ) 6 cout << "q is equal to 10" ;

17 Chapter 2 Introduction to C++ Programming 17 Prelab Activities Correct the Code Your answer: 1 int q = 10 ; 2 3 cout << "q is: " << q << endl; 4 5 if ( q == 10 ) 6 cout << "q is equal to 10" ; Error: Line 5: Testing for equality is performed with the equality operator ==, not the assignment operator =. Logic error. 44. The following code segment should determine whether an integer variable s value is greater than zero and display an appropriate message. 1 int x = 9 ; 2 3 if ( x > 0 ); 4 cout << "Greater than zero"; Your answer: 1 int x = 9 ; 2 3 if ( x > 0 ) 4 cout << "Greater than zero"; Error: Line 3: Placing a semicolon after the if statement s condition causes the messsage "Greater than zero" to display even if the condition is false. Logic error.

18 18 Introduction to C++ Programming Chapter 2 Prelab Activities Correct the Code 45. The following program should print 302 to the screen: 1 #include <iostream> 2 3 using std::cout; 4 using std::end; 5 6 int ma in() 7 { 8 int x = 30; 9 int y = 2 ; cout << y << x << endl; return 0 ; } // end main Your answer: 1 #include <iostream> 2 3 using std::cout; 4 using std:: endl; 5 6 int main() 7 { 8 int x = 30; 9 int y = 2 ; cout << x << y << endl; return 0 ; } // end main Errors: Line 4: The stream manipulator endl is spelled incorrectly. Compilation error. Line 6: Placing a space in the middle of function name main is a syntax error. Line 11: To display 302, the value of x should be displayed, followed by the value of y. Logic error.

19 Chapter 2 Introduction to C++ Programming 19 Lab Exercises Lab Exercise 1 Sum, Average, Product, Smallest and Largest Date: Section: This problem is intended to be solved in a closed-lab session with a teaching assistant or instructor present. The problem is divided into six parts: 1. Lab Objectives 2. Description of the Problem 3. Sample Output 4. Program Template (Fig. L 2.3) 5. Problem-Solving Tips 6. Follow-Up Questions and Activities The program template represents a complete working C++ program, with one or more key lines of code replaced with comments. Read the problem description and examine the sample output; then study the template code. Using the problem-solving tips as a guide, replace the /* */ comments with C++ code. Compile and execute the program. Compare your output with the sample output provided. Then answer the follow-up questions. The source code for the template is available at and /deitel. Lab Objectives This lab was designed to reinforce programming concepts from Chapter 2 of C++ How To Program: Fifth Edition. In this lab, you will practice: Using cout to output text and variables. Using cin to input data from the user. Using if statements to make decisions based on the truth or falsity of a condition. Using the arithmetic operators to perform calculations. Using relational operators to compare values. The follow-up questions and activities also will give you practice: Comparing < to <=. Modifying existing code to perform the same task in a different manner. Description of the Problem Write a program that inputs three integers from the keyboard, and prints the sum, average, product, smallest and largest of these numbers. The screen dialogue should appear as follows: [ Note: 13, 27 and 14 are input by the user.] Sample Output Input three different integers: Sum is 54 Average is 18 Product is 4914 Smallest is 13 Largest is 27

20 20 Introduction to C++ Programming Chapter 2 Lab Exercises Lab Exercise 1 Sum, Average, Product, Smallest and Largest Template 1 // Lab 1: numbercompare.cpp 2 #include <iostream> // allows program to perform input and output 3 4 using std::cout; // program uses cout 5 using std::endl; // program uses endl 6 using std::cin; // program uses cin 7 8 int main() 9 { 10 int number1; // first integer read from user 11 int number2; // second integer read from user 12 int number3; // third integer read from user 13 int smallest; // smallest integer read from user 14 int largest; // largest integer read from user cout << "Input three different integers: "; // prompt 17 /* Write a statement to read in values for number1, number2 and 18 number3 using a single cin statement */ largest = number1; // assume first integer is largest /* Write a statement to determine if number2 is greater than 23 largest. If so assign number2 to largest */ /* Write a statement to determine if number3 is greater than 26 largest. If so assign number3 to largest */ smallest = number1; // assume first integer is smallest /* Write a statement to determine if number2 is less than 31 smallest. If so assign number2 to smallest */ /* Write a statement to determine if number3 is less than 34 smallest. If so assign number3 to smallest */ /* Write an output statement that prints the sum, average, 37 product, largest and smallest */ return 0 ; // indicate successful termination } // end main Fig. L 2.3 numbercompare.cpp. Problem-Solving Tips 1. Prompt the user to input three integer values. You will use a single cin statement to read all three values. 2. Sometimes it is useful to make an assumption to help solve or simplify a problem. For example, we assume number1 is the largest of the three values and assign it to largest. You will use if statements to determine whether number2 or number3 are larger. 3. Using an if statement, compare largest to number2. If the content of number2 is larger, then store the variable s value in largest. 4. Using an if statement, compare largest to number3. If the content of number3 is larger, then store the variable s value in largest. At this point you are guaranteed to have the largest value stored in largest.

21 Chapter 2 Introduction to C++ Programming 21 Lab Exercises Lab Exercise 1 Sum, Average, Product, Smallest and Largest Solution 5. Perform similar steps to those in Steps 2 4 to determine the smallest value. 6. Write a cout statement that outputs the sum, average, product (i.e., multiplication), largest and smallest values. 7. Be sure to follow the spacing and indentation conventions mentioned in the text. 8. If you have any questions as you proceed, ask your lab instructor for assistance. 1 // Lab 1: numbercompare.cpp 2 #include <iostream> // allows program to perform input and output 3 4 using std::cout; // program uses cout 5 using std::endl; // program uses endl 6 using std::cin; // program uses cin 7 8 int main() 9 { 10 int number1; // first integer read from user 11 int number2; // second integer read from user 12 int number3; // third integer read from user 13 int smallest; // smallest integer read from user 14 int largest; // largest integer read from user cout << "Input three different integers: " ; // prompt 17 cin >> number1 >> number2 >> number3; // read three integers largest = number1; // assume first integer is largest if ( number2 > largest ) // is number2 larger? 22 largest = number2; // number2 is now the largest if ( number3 > largest ) // is number3 larger? 25 largest = number3; // number3 is now the largest smallest = number1; // assume first integer is smallest if ( number2 < smallest ) // is number2 smaller? 30 smallest = number2; // number2 is now the smallest if ( number3 < smallest ) // is number3 smaller? 33 smallest = number3; // number3 is now the smallest cout << "Sum is " << number1 + number2 + number3 36 << "\naverage is " << ( number1 + number2 + number3 ) / 3 37 << "\nproduct is " << number1 * number2 * number3 38 << "\nsmallest is " << smallest 39 << "\nlargest is " << largest << endl; return 0 ; // indicate successful termination } // end main

22 22 Introduction to C++ Programming Chapter 2 Lab Exercises Lab Exercise 1 Sum, Average, Product, Smallest and Largest Follow-Up Questions and Activities 1. Modify your solution to use three separate cin statements rather than one. Write a separate prompt for each cin. 1 // Lab 1: numbercompare.cpp 2 #include <iostream> // allows program to perform input and output 3 4 using std::cout; // program uses cout 5 using std::endl; // program uses endl 6 using std::cin; // program uses cin 7 8 int main() 9 { 10 int number1; // first integer read from user 11 int number2; // second integer read from user 12 int number3; // third integer read from user 13 int smallest; // smallest integer read from user 14 int largest; // largest integer read from user cout << "Input first integer: " ; // prompt 17 cin >> number1; // read an integer 18 cout << "Input second integer: " ; // prompt 19 cin >> number2; // read a second integer 20 cout << "Input third integer: " ; // prompt 21 cin >> number3; // read a third integer largest = number1; // assume first integer is largest if ( number2 > largest ) // is number2 larger? 26 largest = number2; // number2 is now the largest if ( number3 > largest ) // is number3 larger? 29 largest = number3; // number3 is now the largest smallest = number1; // assume first integer is smallest if ( number2 < smallest ) // is number2 smaller? 34 smallest = number2; // number2 is now the smallest if ( number3 < smallest ) // is number3 smaller? 37 smallest = number3; // number3 is now the smallest cout << "Sum is " << number1 + number2 + number3 40 << "\naverage is " << ( number1 + number2 + number3 ) / 3 41 << "\nproduct is " << number1 * number2 * number3 42 << "\nsmallest is " << smallest 43 << "\nlargest is " << largest << endl; return 0 ; // indicate successful termination } // end main

23 Chapter 2 Introduction to C++ Programming 23 Lab Exercises Lab Exercise 1 Sum, Average, Product, Smallest and Largest Input first integer: 13 Input second integer: 27 Input third integer: 14 Sum is 54 Average is 18 Product is 4914 Smallest is 13 Largest is Does it matter whether < or <= is used when making comparisons to determine the smallest integer? Which did you use and why? In this program, it does not matter whether < or <= is used when making comparisons to determine the smallest integer. The only instance when using < as opposed to <= makes a difference is if smallest and the number it is being compared to are equal, in which case either value can be used as the smallest with the same result.

24

25 Chapter 2 Introduction to C++ Programming 25 Lab Exercises Lab Exercise 2 Multiples Lab Exercise 2 Multiples Date: Section: This problem is intended to be solved in a closed-lab session with a teaching assistant or instructor present. The problem is divided into six parts: 1. Lab Objectives 2. Description of the Problem 3. Sample Output 4. Program Template (Fig. L 2.4) 5. Problem-Solving Tips 6. Follow-Up Questions and Activities The program template represents a complete working C++ program, with one or more key lines of code replaced with comments. Read the problem description and examine the sample output; then study the template code. Using the problem-solving tips as a guide, replace the /* */ comments with C++ code. Compile and execute the program. Compare your output with the sample output provided. Then answer the follow-up questions. The source code for the template is available at and /deitel. Lab Objectives This lab was designed to reinforce programming concepts from Chapter 2 of C++ How To Program: Fifth Edition. In this lab, you will practice: Using cout to output text and values. Using cin to input data from the user. Using if statements to make decisions based on the truth or falsity of a condition. Using the modulus operator ( % ) to determine the remainder of an integer division operation. The follow-up questions and activities also will give you practice: Understanding the modulus operator. Recognizing common mistakes with the if statement. Adapting a program to solve a similar problem. Description of the Problem Write a program that reads in two integers and determines and prints whether the first is a multiple of the second. [ Hint : Use the modulus operator.] Sample Output Enter two integers: is not a multiple of 8

26 26 Introduction to C++ Programming Chapter 2 Lab Exercises Lab Exercise 2 Multiples Template 1 // Lab 2: multiples.cpp 2 #include <iostream> // allows program to perform input and output 3 4 using std::cout; // program uses cout 5 using std::endl; // program uses endl 6 using std::cin; // program uses cin 7 8 int main() 9 { 10 /* Write variables declarations here */ cout << "Enter two integers: " ; // prompt 13 /* Write a cin statement to read data into variables here */ // using modulus operator 16 if ( /* Write a condition that tests whether number1 is a multiple of 17 number2 */ ) 18 cout << number1 << " is a multiple of " << number2 << endl; if ( /* Write a condition that tests whether number1 is not a multiple 21 of number2 */ ) 22 cout << number1 << " is not a multiple of " << number2 << endl; return 0 ; // indicate successful termination } // end main Fig. L 2.4 multiples.cpp. Problem-Solving Tips 1. The input data consists of two integers, so you will need two int variables to store the input values. 2. Use cin to read the user input into the int variables. 3. Use an if statement to determine whether the first number input is a multiple of the second number input. Use the modulus operator, %. If one number divides into another evenly, the modulus operation results in 0. If the result is 0, display a message indicating that the first number is a multiple of the second number. 4. Use an if statement to determine whether the first number input is not a multiple of the second number input. If one number does not divide into another evenly, the modulus operation results in a non-zero value. If non-zero, display a message indicating that the first number is not a multiple of the second. 5. Be sure to follow the spacing and indentation conventions mentioned in the text. 6. If you have any questions as you proceed, ask your lab instructor for assistance. Solution 1 // Lab 2: multiples.cpp 2 #include <iostream> // allows program to perform input and output 3 4 using std::cout; // program uses cout 5 using std::endl; // program uses endl 6 using std::cin; // program uses cin

27 Chapter 2 Introduction to C++ Programming 27 Lab Exercises Lab Exercise 2 Multiples 7 8 int main() 9 { 10 int number1; // first integer read from user 11 int number2; // second integer read from user cout << "Enter two integers: " ; // prompt 14 cin >> number1 >> number2; // read two integers from user // using modulus operator 17 if ( number1 % number2 == 0 ) 18 cout << number1 << " is a multiple of " << number2 << endl; if ( number1 % number2!= 0 ) 21 cout << number1 << " is not a multiple of " << number2 << endl; return 0 ; // indicate successful termination } // end main Follow-Up Questions and Activities 1. Can the modulus operator be used with non-integer operands? Can it be used with negative numbers? Assume that the user entered the sets of numbers in Fig. L 2.5. For each set, what does the expression in the third column output? If there is an error, explain why. Integer 1 Integer 2 Expression Output cout << 73 % 22; cout << 0 % 100; cout << 100 % 0; error 3 3 cout << -3 % 3; cout << 9 % 4.5; error 16 2 cout << 16 % 2; 0 Fig. L 2.5 Determine the output of the cout statements in the third column. The modulus operator cannot be used with non-integer operands. It can be used with negative numbers. An error occurs in cout << 100 % 0; because you cannot divide by 0. An error occurs in cout << 9 % 4.5; because you cannot use modulus with a double value.

28 28 Introduction to C++ Programming Chapter 2 Lab Exercises Lab Exercise 2 Multiples 2. Place a semicolon at the end of the if statement in your solution that corresponds to the if statement in lines in the template. What happens? Explain. The line cout << number1 << " is a multiple of " << number2 << endl; always executes, even if number1 is not a multiple of number2, because the semicolon is treated as the empty statement and is considered to be the if s body. The output statement is just another statement that will execute after the if statement executes. 3. Rewrite the cout statement in your solution that corresponds to the cout statement in line 18 in the template. This statement should now look as follows: cout << number1; cout << " is a multiple of " ; cout << number2 << endl; Rerun the program and observe the differences. Why is the output different? The output is different because only the first cout statement is considered to be the if body. The second and third cout statements are now just regular statements that will be executed after the if statement is finished. 4. Modify the program to determine whether a number entered is even or odd. [ Note: Now, the user needs to enter only one number.] 1 // Lab 2: multiples.cpp 2 #include <iostream> // allows program to perform input and output 3 4 using std::cout; // program uses cout 5 using std::endl; // program uses endl 6 using std::cin; // program uses cin 7 8 int main() 9 { 10 int number; // integer read from user cout << "Enter an integer: " ; // prompt 13 cin >> number; // read an integer from user // using modulus operator 16 if ( number % 2 == 0 ) 17 cout << number1 << " is even" << endl; if ( number % 2!= 0 ) 20 cout << number1 << " is odd" << endl; return 0 ; // indicate successful termination } // end main Enter an integer: is even

29 Chapter 2 Introduction to C++ Programming 29 Lab Exercises Lab Exercise 3 Separating Digits Lab Exercise 3 Separating Digits Date: Section: This problem is intended to be solved in a closed-lab session with a teaching assistant or instructor present. The problem is divided into six parts: 1. Lab Objectives 2. Description of the Problem 3. Sample Output 4. Program Template (Fig. L 2.6) 5. Problem-Solving Tips 6. Follow-Up Questions and Activities The program template represents a complete working C++ program, with one or more key lines of code replaced with comments. Read the problem description and examine the sample output; then study the template code. Using the problem-solving tips as a guide, replace the /* */ comments with C++ code. Compile and execute the program. Compare your output with the sample output provided. Then answer the follow-up questions. The source code for the template is available at and /deitel. Lab Objectives This lab was designed to reinforce programming concepts from Chapter 2 of C++ How To Program: Fifth Edition. In this lab, you will practice: Using the modulus operator ( % ) to determine the remainder of a division operation. Integer division, which differs from floating-point division because integer division truncates the decimal portion of the result. The follow-up questions and activities also will give you practice: Using the division and modulus operators. Examining what happens during program execution when the user enters invalid input. Adapting a program to solve a similar problem. Problem Description Write a program that inputs a five-digit number, separates the number into its individual digits and prints the digits separated from one another by three spaces each. [ Hint: Use integer division and the modulus operator.] For example, if the user inputs 42339, the program should print what is shown in the sample output. Sample Output

30 30 Introduction to C++ Programming Chapter 2 Lab Exercises Lab Exercise 3 Separating Digits Template 1 // Lab 3: digits.cpp 2 #include <iostream> // allows program to perform input and output 3 4 using std::cout; // program uses cout 5 using std::endl; // program uses endl 6 using std::cin; // program uses cin 7 8 int main() 9 { 10 int number; // integer read from user cout << "Enter a five-digit integer: " ; // prompt 13 cin >> number; // read integer from user /* Write a statement to print the left-most digit of the 16 5-digit number */ 17 /* Write a statement that changes number from 5-digits 18 to 4-digits */ 19 /* Write a statement to print the left-most digit of the 20 4-digit number */ 21 /* Write a statement that changes number from 4-digits 22 to 3-digits */ 23 /* Write a statement to print the left-most digit of the 24 3-digit number */ 25 /* Write a statement that changes number from 3-digits 26 to 2-digits */ 27 /* Write a statement to print the left-most digit of the 28 2-digit number */ 29 /* Write a statement that changes number from 2-digits 30 to 1-digit */ 31 cout << number << endl; return 0 ; // indicate successful termination } // end main Fig. L 2.6 digits.cpp. Problem-Solving Tips 1. The input data consists of one integer, so you will use an int variable ( number ) to represent it. Note that the description indicates that one five-digit number is to be input not five separate digits. 2. You will use a series of statements to break down the number into its individual digits using modulus ( % ) and division ( / ) calculations. 3. After the number has been input using cin, divide the number by to get the leftmost digit. Why does this work? Because the number input is five digits long, it is divided by to obtain the leftmost digit. In C++, dividing an integer by an integer results in an integer. For example, /10000 evaluates to 4 because divides evenly into four times. The remainder 2339 is truncated. 4. Change the number to a 4-digit number using the modulus operator. The number modulus evaluates to the integer remainder in this case, the right-most four digits. For example, %10000 results in Assign the result of this modulus operation to the variable that stores the five-digit number input.

31 Chapter 2 Introduction to C++ Programming 31 Lab Exercises Lab Exercise 3 Separating Digits Solution 5. Repeat this pattern of division and modulus reducing the divisor by a factor of 10 each time (i.e., 1000, 100, 10 ). After the number is changed to a four-digit number, divide/modulus by After the number is changed to a three-digit number, divide/modulus by 100. And so on. 6. Be sure to follow the spacing and indentation conventions mentioned in the text. 7. If you have any questions as you proceed, ask your lab instructor for assistance. 1 // Lab 3: digits.cpp 2 #include <iostream> // allows program to perform input and output 3 4 using std::cout; // program uses cout 5 using std::endl; // program uses endl 6 using std::cin; // program uses cin 7 8 int main() 9 { 10 int number; // integer read from user cout << "Enter a five-digit integer: " ; // prompt 13 cin >> number; // read integer from user cout << number / << " " ; 16 number = number % 10000; 17 cout << number / 1000 << " " ; 18 number = number % 1000; 19 cout << number / 100 << " " ; 20 number = number % 100; 21 cout << number / 10 << " " ; 22 number = number % 10 ; 23 cout << number << endl; return 0 ; // indicate successful termination } // end main Follow-Up Questions and Activities 1. What are the results of the following expressions? 24 / 5 = 4 18 % 3 = 0 13 % 9 = 4 13 / 2 % 2 = 0 2. What happens when the user inputs a number which has fewer than five digits? Why? What is the output when 1763 is entered? If the user inputs a number which has fewer than five digits, then the missing digits (the leftmost digits) are displayed as 0. This is because any number with fewer than five digits must be less than so dividing by will result in 0. Thus when 1763 is entered, the output is:

32 32 Introduction to C++ Programming Chapter 2 Lab Exercises Lab Exercise 3 Separating Digits 3. The program you completed in this lab exercise inputs a number with multiple digits and separates the digits. Write the inverse program, a program which asks the user for three one-digit numbers and combines them into a single three-digit number. [ Hint: Use multiplication and addition to form the three-digit number.] 1 // Lab 3: digits.cpp 2 #include <iostream> // allows program to perform input and output 3 4 using std::cout; // program uses cout 5 using std::endl; // program uses endl 6 using std::cin; // program uses cin 7 8 int main() 9 { 10 int number; // three-digit number 11 int digit1; // hundred's digit 12 int digit2; // ten's digit 13 int digit3; // one's digit cout << "Enter the hundred's digit: " ; // prompt 16 cin >> digit1; // read digit from user 17 cout << "Enter the ten's digit: " ; // prompt 18 cin >> digit2; // read digit from user 19 cout << "Enter the one's digit: " ; // prompt 20 cin >> digit3; // read digit from user number = digit3; // start with just the one's digit 23 number = number + digit2 * 10 ; // add the ten's digit * number = number + digit1 * 100; // add the hundred's digit * cout << number << endl; return 0 ; // indicate successful termination } // end main Enter the hundred s digit: 4 Enter the ten s digit: 2 Enter the one s digit: 7 427

33 Chapter 2 Introduction to C++ Programming 33 Lab Exercises Debugging Debugging Date: Section: The program in this section does not run properly. Fix all the syntax errors so that the program will compile successfully. Once the program compiles, compare the output with the sample output, and eliminate any logic errors that may exist. The sample output demonstrates what the program s output should be once the program s code has been corrected. debugging02.cpp (Fig. L 2.7) is available at and at Sample Output Enter two integers to compare: 5 2 5!= 2 5 > 2 5 >= 2 Enter two integers to compare: 2 7 2!= 7 2 < 7 2 <= 7 Enter two integers to compare: == 4 4 <= 4 4 >= 4 Broken Code 1 // Debugging 2 include <iostream> 3 4 using std::cout; 5 using std::endl; 6 using std::cin; 7 8 int main() 9 { int number1; 12 int number2; cout << "Enter two integers to compare: " ; 15 cout >> number1 >> number2; 16 Fig. L 2.7 debugging02.cpp. (Part 1 of 2.)

34 34 Introduction to C++ Programming Chapter 2 Lab Exercises Debugging 17 if ( number1 == number2 ) 18 cout << number1 << ' == ' << number2 << endl; if ( number1 <> number2 ) 21 cout << number1 << " <> " << number2 << endl; if ( number2 < number1 ) 24 cout << number1 << " < " << number2 << endl; if number1 > number2 ) 27 cout << number1 << " > " << number2 << endl; if ( number1 < number2 ) 30 cout << number1 << " <= " << number2 << endl; if ( number1 >= number2 ) 33 cout << number1 << " >= " << number2 << endl return 0; } // end main Fig. L 2.7 debugging02.cpp. (Part 2 of 2.) Debugging Solution 1 // Debugging 2 # include <iostream> 3 4 using std::cout; 5 using std::endl; 6 using std::cin; 7 8 int main() 9 { int number1; 12 int number2; cout << "Enter two integers to compare: " ; 15 cin >> number1 >> number2; if ( number1 == number2 ) 18 cout << number1 << " == " << number2 << endl; if ( number1!= number2 ) 21 cout << number1 << "!= " << number2 << endl; if ( number1 < number2 ) 24 cout << number1 << " < " << number2 << endl; if ( number1 > number2 ) 27 cout << number1 << " > " << number2 << endl; if ( number1 < = number2 ) 30 cout << number1 << " <= " << number2 << endl;

35 Chapter 2 Introduction to C++ Programming 35 Lab Exercises Debugging if ( number1 >= number2 ) 33 cout << number1 << " >= " << number2 << endl; return 0 ; } // end main Errors: Line 2: The program must #include the iostream header file. Line 15: Input from the keyboard requires the use of cin not cout. Line 18: A string literal must be enclosed with double-quotes ( " ); single-quotes ( ' ) can only be used for a single character literal. Lines 20 21: The inequality operator is!=, not <>. Line 23: number2 and number1 were switched in the less than conditional expression. Line 26: The left parentheses in the if condition was missing. Line 29: The < should be <= to match the cout statement on the next line. Line 33: Every statement must end with a semicolon.

36

37 Chapter 2 Introduction to C++ Programming 37 Postlab Activities Coding Exercises Date: Section: These coding exercises reinforce the lessons learned in the lab and provide additional programming experience outside the classroom and laboratory environment. They serve as a review after you have completed the Prelab Activities and Lab Exercises successfully. For each of the following problems, write a program or a program segment that performs the specified action. 1. Write the preprocessor directive which includes the iostream file in a program. Also write the appropriate using directives for cin, cout and endl. 1 #include <iostream> 2 3 using std::cin; 4 using std::cout; 5 using std::endl; 2. Define a main function which declares three integer variables. Remember to terminate the main function with a return statement. 1 #include <iostream> 2 3 using std::cin; 4 using std::cout; 5 using std::endl; 6 7 int main() 8 { 9 int number1; 10 int number2; 11 int number3; return 0 ; 14 } // end main function

38 38 Introduction to C++ Programming Chapter 2 Postlab Activities Coding Exercises 3. Write a single line of code that reads values into the three integer variables from Coding Exercise 2. 1 #include <iostream> 2 3 using std::cin; 4 using std::cout; 5 using std::endl; 6 7 int main() 8 { 9 int number1; 10 int number2; 11 int number3; cin >> number1 >> number2 >> number3; return 0 ; 16 } // end main function 4. Write a line of code that prints all three integer values from Coding Exercise 3 separated by hyphens, -. 1 #include <iostream> 2 3 using std::cin; 4 using std::cout; 5 using std::endl; 6 7 int main() 8 { 9 int number1; 10 int number2; 11 int number3; cin >> number1 >> number2 >> number3; cout << number1 << "-" << number2 << "-" << number3 << endl; return 0 ; 18 } // end main function

39 Chapter 2 Introduction to C++ Programming 39 Postlab Activities Coding Exercises 5. Modify your solution to Coding Exercise 4 to write a C++ program that determines which variable s value is the largest. Use variable largest to store the largest value. 1 #include <iostream> 2 3 using std::cin; 4 using std::cout; 5 using std::endl; 6 7 int main() 8 { 9 int number1; 10 int number2; 11 int number3; cin >> number1 >> number2 >> number3; cout << number1 << "-" << number2 << "-" << number3 << endl; int largest; largest = number1; if ( number2 > largest ) 22 largest = number2; if ( number3 > largest ) 25 largest = number3; return 0 ; 28 } // end main function 6. Modify your solution to Coding Exercise 5 to write a C++ program that determines which integer variable s value is the smallest. Use variable smallest to store the smallest value. 1 #include <iostream> 2 3 using std::cin; 4 using std::cout; 5 using std::endl; 6 7 int main() 8 { 9 int number1; 10 int number2; 11 int number3; cin >> number1 >> number2 >> number3; cout << number1 << "-" << number2 << "-" << number3 << endl; int largest; largest = number1; 20

40 40 Introduction to C++ Programming Chapter 2 Postlab Activities Coding Exercises 21 if ( number2 > largest ) 22 largest = number2; if ( number3 > largest ) 25 largest = number3; int smallest; smallest = number1; if ( number2 < smallest ) 32 smallest = number2; if ( number3 < smallest ) 35 smallest = number3; return 0 ; 38 } // end main function 7. Modify your solution to Coding Exercise 6 to test if any of the variable s values are equal and if so print that they are equal. For example, if two variables have the same value, 5, print 5and 5are equal. 1 #include <iostream> 2 3 using std::cin; 4 using std::cout; 5 using std::endl; 6 7 int main() 8 { 9 int number1; 10 int number2; 11 int number3; cin >> number1 >> number2 >> number3; cout << number1 << "-" << number2 << "-" << number3 << endl; int largest; largest = number1; 20 if ( number2 > largest ) 21 largest = number2; 22 if ( number3 > largest ) 23 largest = number3; int smallest; smallest = number1; 28

41 Chapter 2 Introduction to C++ Programming 41 Postlab Activities Coding Exercises 29 if ( number2 < smallest ) 30 smallest = number2; if ( number3 < smallest ) 33 smallest = number3; if ( number1 == number2 ) 36 cout << number1 << " and " << number2 << " are equal." << endl; if ( number1 == number3 ) 39 cout << number1 << " and " << number3 << " are equal." << endl; if ( number2 == number3 ) 42 cout << number2 << " and " << number3 << " are equal." << endl; return 0 ; 45 } // end main function

42

43 Chapter 2 Introduction to C++ Programming 43 Postlab Activities Programming Challenges Programming Challenges Date: Section: The Programming Challenges are more involved than the Coding Exercises and may require a significant amount of time to complete. Write a C++ program for each of the problems in this section. The answers to these problems are available at and Pseudocode, hints and/or sample outputs are provided to aid you in your programming. 1. Write a program that prints the numbers 1 to 4 on the same line with each pair of adjacent numbers separated by one space. Write the program using the following methods: Hints: a) Using one output statement with one stream-insertion operator. b) Using one output statement with four stream-insertion operators. c) Using four output statements. Use comments to separate your program into three clearly marked sections, one for each part (i.e., a c) of the problem. For Part a) the entire output should be contained within one string. Use either endl or "\n" after each part to separate their output. Your output should look like: Solution 1 // Programming Challenge 1: threeoutputs.cpp 2 #include <iostream> 3 4 using std::cin; 5 using std::cout; 6 using std::endl; 7 8 int main() 9 { 10 // part a - one output statement with one stream-insertion operator 11 cout << " \n"; // part b - one output statement with four stream-insertion operators 14 cout << "1 " << "2 " << "3 " << "4\n"; 15

44 44 Introduction to C++ Programming Chapter 2 Postlab Activities Programming Challenges 16 // part c - four output statememts 17 cout << "1 " ; 18 cout << "2 " ; 19 cout << "3 " ; 20 cout << "4\n"; return 0 ; 23 } // end main function 2. Write a program that asks the user to enter two integers, obtains the numbers from the user, then prints the larger number followed by the words is larger. If the numbers are equal, print the message These numbers are equal. Hints: The user should input both integers at once, i.e., cin >> x >> y; Remember to print spaces after printing integers to the screen. A typical run of your program might look as follows: Enter two integers: is larger. Solution 1 // Programming Challenge 2: larger.cpp 2 #include <iostream> 3 4 using std::cin; 5 using std::cout; 6 using std::endl; 7 8 int main() 9 { 10 int number1; // first input 11 int number2; // second input cout << "Enter two integers: " ; // prompt 14 cin >> number1 >> number2; // enter integers if ( number1 > number2 ) // is number1 larger? 17 cout << number1 << " is larger." << endl; if ( number2 > number1 ) // is number2 larger? 20 cout << number2 << " is larger." << endl; if ( number1 == number2 ) // are they equal? 23 cout << "These numbers are equal." << endl; return 0 ; 26 } // end main function

45 Chapter 2 Introduction to C++ Programming 45 Postlab Activities Programming Challenges 3. Write a program that reads in five integers and determines and prints the largest and the smallest integers in the group. Use only the programming techniques you learned in Chapter 2 of C++ How to Program: Fifth Edition. Hints: This program requires seven variables: five for user input and two to store the largest and the smallest, respectively. As soon as the user inputs the values, assign the largest and smallest variables the value of the first input. If instead, largest was initially assigned to zero, this would be a logic error because negative numbers could be input by the user. Ten separate if statements are required to compare each input to largest and smallest. A typical run of your program might look as follows: Enter five integers: Largest integer: 10 Smallest integer: -4 Solution 1 // Programming Challenge 3: fiveintegers.cpp 2 #include <iostream> 3 4 using std::cin; 5 using std::cout; 6 using std::endl; 7 8 int main() 9 { 10 int number1; // first input 11 int number2; // second input 12 int number3; // third input 13 int number4; // fourth input 14 int number5; // fifth input 15 int largest; // the largest integer 16 int smallest; // the smallest integer cout << "Enter five integers: " ; // prompt 19 cin >> number1 >> number2 >> number3 >> number4 >> number5; // read integers largest = number1; // first assume that number1 is the largest 22 smallest = number1; // first assume that number1 is the smallest if ( number2 > largest ) // is number2 larger? 25 largest = number2; if ( number2 < smallest ) // is number2 smaller? 28 smallest = number2; if ( number3 > largest ) // is number3 larger? 31 largest = number3; 32

46 46 Introduction to C++ Programming Chapter 2 Postlab Activities Programming Challenges 33 if ( number3 < smallest ) // is number3 smaller? 34 smallest = number3; if ( number4 > largest ) // is number4 larger? 37 largest = number4; if ( number4 < smallest ) // is number4 smaller? 40 smallest = number4; if ( number5 > largest ) // is number5 larger? 43 largest = number5; if ( number5 < smallest ) // is number5 smaller? 46 smallest = number5; cout << "Largest integer: " << largest << endl; 49 cout << "Smallest integer: " << smallest << endl; return 0 ; 52 } // end main function

Introduction to C++ Programming

Introduction to C++ Programming cpphtp5_02.fm Page 36 Wednesday, December 15, 2004 11:11 AM 2 Introduction to C++ Programming OBJECTIVES In this chapter you will learn: To write simple computer programs in C++. To write simple input

More information

[Page 70 (continued)] a. are used to document a program and improve its readability. [Page 71]

[Page 70 (continued)] a. are used to document a program and improve its readability. [Page 71] [Page 70 (continued)] Exercises 2.7 Discuss the meaning of each of the following objects: a. std::cin b. std::cout 2.8 Fill in the blanks in each of the following: a. are used to document a program and

More information

A First Book of C++ Chapter 2 Data Types, Declarations, and Displays

A First Book of C++ Chapter 2 Data Types, Declarations, and Displays A First Book of C++ Chapter 2 Data Types, Declarations, and Displays Objectives In this chapter, you will learn about: Data Types Arithmetic Operators Variables and Declarations Common Programming Errors

More information

Chapter 2: Problem Solving Using C++

Chapter 2: Problem Solving Using C++ Chapter 2: Problem Solving Using C++ 1 Objectives In this chapter, you will learn about: Modular programs Programming style Data types Arithmetic operations Variables and declaration statements Common

More information

Adjusted/Modified by Nicole Tobias. Chapter 2: Basic Elements of C++

Adjusted/Modified by Nicole Tobias. Chapter 2: Basic Elements of C++ Adjusted/Modified by Nicole Tobias 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

More information

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 2: Basic Elements of C++

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with the basic components of a C++ program,

More information

Chapter One Introduction to Programming

Chapter One Introduction to Programming Chapter One Introduction to Programming 1-1 Algorithm and Flowchart Algorithm is a step-by-step procedure for calculation. More precisely, algorithm is an effective method expressed as a finite list of

More information

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program. Name: Class: Date: Exam #1 - Prep True/False Indicate whether the statement is true or false. 1. Programming is the process of writing a computer program in a language that the computer can respond to

More information

Chapter 4 Operators & Arithmetic

Chapter 4 Operators & Arithmetic Chapter 4 Operators & Arithmetic 4-1 Arithmetic in C++ Arithmetic expressions can be made up of constants, variables, operators and parentheses. The arithmetic operators in C++ are as follows + (addition)

More information

Chapter 4 Intro to Programming in C++

Chapter 4 Intro to Programming in C++ Chapter 4 Intro to Programming in C++ 4-1 Computer Programming Earlier we defined an algorithm to be a step by step process for solving a problem. Computer programming is the process of implementing the

More information

The Fundamentals of C++

The Fundamentals of C++ The Fundamentals of C++ Basic programming elements and concepts JPC and JWD 2002 McGraw-Hill, Inc. Program Organization Program statement Definition Declaration Action Executable unit Named set of program

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

JavaScript: Introduction to Scripting. 2008 Pearson Education, Inc. All rights reserved.

JavaScript: Introduction to Scripting. 2008 Pearson Education, Inc. All rights reserved. 1 6 JavaScript: Introduction to Scripting 2 Comment is free, but facts are sacred. C. P. Scott The creditor hath a better memory than the debtor. James Howell When faced with a decision, I always ask,

More information

Computer Programming C++ Classes and Objects 15 th Lecture

Computer Programming C++ Classes and Objects 15 th Lecture Computer Programming C++ Classes and Objects 15 th Lecture 엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University Copyrights 2013 Eom, Hyeonsang All Rights Reserved Outline

More information

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output Objectives In this chapter, you will: Learn what a stream is and examine input and output streams Explore

More information

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be... What is a Loop? CSC Intermediate Programming Looping A loop is a repetition control structure It causes a single statement or a group of statements to be executed repeatedly It uses a condition to control

More information

Programming Fundamental. Instructor Name: Lecture-2

Programming Fundamental. Instructor Name: Lecture-2 Programming Fundamental Instructor Name: Lecture-2 Today s Lecture What is Programming? First C++ Program Programming Errors Variables in C++ Primitive Data Types in C++ Operators in C++ Operators Precedence

More information

Lecture 1 Notes: Introduction

Lecture 1 Notes: Introduction Introduction to C++ January 4, 2011 Massachusetts Institute of Technology 6.096 Lecture 1 Notes: Introduction 1 Compiled Languages and C++ 1.1 Why Use a Language Like C++? At its core, a computer is just

More information

Chapter 3 Basic Input & Output

Chapter 3 Basic Input & Output Chapter 3 Basic Input & Output 3-1 Basic Output in C++ cout is a predefined variable in C++ that indicates you are going to output a stream of characters to an output device. cout uses an operator called

More information

C++ Basics. C++ Basics: Names: Identifiers. Names: Identifiers. Display 2.1 A C++ Program (1 of 2) Display 2.1 A C++ Program (2 of 2)

C++ Basics. C++ Basics: Names: Identifiers. Names: Identifiers. Display 2.1 A C++ Program (1 of 2) Display 2.1 A C++ Program (2 of 2) C++ Basics C++ Basics: 2.1 Variables and Assignments Variables and Assignments Input and Output Data Types and Expressions Simple Flow of Control Program Style A C++ variable can hold a number or other

More information

C Programming Dr. Hasan Demirel

C Programming Dr. Hasan Demirel C How to Program, H. M. Deitel and P. J. Deitel, Prentice Hall, 5 th edition (3 rd edition or above is also OK). Introduction to C Programming Dr. Hasan Demirel Programming Languages There are three types

More information

6.096 Lab 1. Due: 7 January 12:30:00. September 20, 2011

6.096 Lab 1. Due: 7 January 12:30:00. September 20, 2011 6.096 Lab 1 Due: 7 January 1:30:00 September 0, 011 1 Additional Material 1.1 Constants A constant is an expressions with a fixed value. Kinds of constants: Literals: used to express particular values

More information

Introduction to Java Applications. 2005 Pearson Education, Inc. All rights reserved.

Introduction to Java Applications. 2005 Pearson Education, Inc. All rights reserved. 1 2 Introduction to Java Applications 2.2 First Program in Java: Printing a Line of Text 2 Application Executes when you use the java command to launch the Java Virtual Machine (JVM) Sample program Displays

More information

Introduction to C++ Programming

Introduction to C++ Programming Introduction to C++ Programming C++ is one of the most popular programming languages in use in industry today. According to the TIOBE Programming Community Index, which can be accessed at this url: http://www.tiobe.com/index.php/content/paperinfo/tpci/

More information

Input and Output! Objectives" (and predefined functions)" 1E3! Topic 4! n Learn what a stream is and examine input and output streams!

Input and Output! Objectives (and predefined functions) 1E3! Topic 4! n Learn what a stream is and examine input and output streams! Input and Output! (and predefined functions)" 1E3! Topic 4! 4 I/O 1 Objectives" n Learn what a stream is and examine input and output streams! n Explore how to read data from the standard input device!

More information

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements 9 Control Statements 9.1 Introduction The normal flow of execution in a high level language is sequential, i.e., each statement is executed in the order of its appearance in the program. However, depending

More information

Dept. of CSE, IIT KGP

Dept. of CSE, IIT KGP Programming in C: Basics CS10001: Programming & Data Structures Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Types of variable We must declare the

More information

C++ Language Tutorial

C++ Language Tutorial cplusplus.com C++ Language Tutorial Written by: Juan Soulié Last revision: June, 2007 Available online at: http://www.cplusplus.com/doc/tutorial/ The online version is constantly revised and may contain

More information

Which of the following is the correct operator to compare two variables? A. := B. = C. equal D. ==

Which of the following is the correct operator to compare two variables? A. := B. = C. equal D. == Group 1 Question 1. What is the correct value to return to the operating system upon the successful completion of a program? A. -1 B. 1 C. 0 D. Programs do not return a value. Question 2. What is the only

More information

Part IA Computing Course Tutorial Guide to C++ Programming

Part IA Computing Course Tutorial Guide to C++ Programming Part IA Computing Course Tutorial Guide to C++ Programming Roberto Cipolla Department of Engineering University of Cambridge September 1, 2004 This document provides an introduction to computing and the

More information

Appendix K Introduction to Microsoft Visual C++ 6.0

Appendix K Introduction to Microsoft Visual C++ 6.0 Appendix K Introduction to Microsoft Visual C++ 6.0 This appendix serves as a quick reference for performing the following operations using the Microsoft Visual C++ integrated development environment (IDE):

More information

Introduction to Programming

Introduction to Programming Introduction to Programming SS 2012 Adrian Kacso, Univ. Siegen adriana.dkacsoa@duni-siegena.de Tel.: 0271/740-3966, Office: H-B 8406 Stand: April 25, 2012 Betriebssysteme / verteilte Systeme Introduction

More information

Solution Exercise 1.1

Solution Exercise 1.1 Solution Exercise 1.1 Part I Fill in the blanks with the most appropriate answers. 1. A Program is a sequence of step-by-step instructions that a computer executes in order to solve a problem or to perform

More information

Chapter 2: Elements of Java

Chapter 2: Elements of Java Chapter 2: Elements of Java Basic components of a Java program Primitive data types Arithmetic expressions Type casting. The String type (introduction) Basic I/O statements Importing packages. 1 Introduction

More information

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

Tutorial-3a: First steps with C++ programming HTTP://WWW.HEP.LU.SE/COURSES/MNXB01 Introduction to Programming and Computing for Scientists (2015 HT) Tutorial-3a: First steps with C++ programming Programming for Scientists Tutorial 3a 1 / 17 Quick

More information

Introduction to Classes and Objects

Introduction to Classes and Objects 3 Introduction to Classes and Objects OBJECTIVES In this chapter you will learn: What classes, objects, member functions and data members are. How to define a class and use it to create an object. How

More information

Repetition and Loops. Additional Python constructs that allow us to effect the (1) order and (2) number of times that program statements are executed.

Repetition and Loops. Additional Python constructs that allow us to effect the (1) order and (2) number of times that program statements are executed. New Topic Repetition and Loops Additional Python constructs that allow us to effect the (1) order and (2) number of times that program statements are executed. These constructs are the 1. while loop and

More information

Control Structure. Pseudocode: Input number If number is even Then Print even Else print odd. Flowchart: Begin. End. Print Odd. number even?

Control Structure. Pseudocode: Input number If number is even Then Print even Else print odd. Flowchart: Begin. End. Print Odd. number even? Control Structure Normally, a program is executed in a sequential manner.however, in many cases, a program has to choose among alternative statements C++ provides constructs that enable the programmer

More information

Python to C/C++ Fall 2011

Python to C/C++ Fall 2011 Python to C/C++ Fall 2011 1. Main Program Python: Program code is indented after colon : def main(): body of program C/C++: Have more setup overhead. C: Both require #include directives to access libraries

More information

if and if-else: Part 1

if and if-else: Part 1 if and if-else: Part 1 Objectives Write if statements (including blocks) Write if-else statements (including blocks) Write nested if-else statements We will now talk about writing statements that make

More information

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++ MS Visual C++ Introduction 1 Quick Introduction The following pages provide a quick tutorial on using Microsoft Visual C++ 6.0 to produce a small project. There should be no major differences if you are

More information

PE1 Worksheet. 3) What are the three control structures for writing an algorithm in pseudocode?

PE1 Worksheet. 3) What are the three control structures for writing an algorithm in pseudocode? PE1 Worksheet Problem solving 1) What are the four stages of problem solving by programming? What shall be done in each stage? Stage 1: Stage 2: Stage 3: Stage 4: 2) What is the top-down design strategy

More information

IN THIS CHAPTER,YOU ARE INTRODUCED to the C language so that you can see what

IN THIS CHAPTER,YOU ARE INTRODUCED to the C language so that you can see what 3 Compiling and Running Your First Program IN THIS CHAPTER,YOU ARE INTRODUCED to the C language so that you can see what programming in C is all about.what better way to gain an appreciation for this language

More information

Common Syntax and Semantic Errors

Common Syntax and Semantic Errors C H A P T E R 2 Common Syntax and Semantic Errors 2.1 CHAPTER OBJECTIVES To understand the fundamental characteristics of syntax and semantic errors To be able to identify specific common syntax and semantic

More information

CS101 Computer programming and utilization

CS101 Computer programming and utilization CS101 Computer programming and utilization Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay Lecture 3, Basic features of C++ Tuesday, 2 August 2011

More information

6.1. Introduction to Void Functions (Procedures) L E S S O N S E PURPOSE PROCEDURE

6.1. Introduction to Void Functions (Procedures) L E S S O N S E PURPOSE PROCEDURE L E S S O N S E 6.1 T Introduction to Void Functions (Procedures) PURPOSE PROCEDURE 1. To introduce the concept of void functions (procedures) 2. To work with void functions (procedures) that have no parameters

More information

Basics of I/O Streams and File I/O

Basics of I/O Streams and File I/O Basics of This is like a cheat sheet for file I/O in C++. It summarizes the steps you must take to do basic I/O to and from files, with only a tiny bit of explanation. It is not a replacement for reading

More information

Passing 1D arrays to functions.

Passing 1D arrays to functions. Passing 1D arrays to functions. In C++ arrays can only be reference parameters. It is not possible to pass an array by value. Therefore, the ampersand (&) is omitted. What is actually passed to the function,

More information

JavaScript: Control Statements I

JavaScript: Control Statements I 1 7 JavaScript: Control Statements I 7.1 Introduction 2 The techniques you will learn here are applicable to most high-level languages, including JavaScript 1 7.2 Algorithms 3 Any computable problem can

More information

Comp181 Midterm Examination, Fall Name Surname: Number: 90 min. 18/11/09

Comp181 Midterm Examination, Fall Name Surname: Number: 90 min. 18/11/09 Comp181 Midterm Examination, Fall 09 10 Name Surname: Number: 90 min. 18/11/09 Answer ALL off the following questions. 1.What does the term hardware refer to? a. The relative difficulty of programming

More information

F ahrenheit = 9 Celsius + 32

F ahrenheit = 9 Celsius + 32 Problem 1 Write a complete C++ program that does the following. 1. It asks the user to enter a temperature in degrees celsius. 2. If the temperature is greater than 40, the program should once ask the

More information

download instant at

download instant at 2 Introduction to C 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 be

More information

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java Lecture Set 2: Starting Java 1. Java Concepts 2. Java Programming Basics 3. User output 4. Variables and types 5. Expressions 6. User input 7. Uninitialized Variables CMSC 131 - Lecture Outlines - set

More information

Tutorial 9 Income Tax Calculator Application: Introducing the switch Multiple-Selection Statement

Tutorial 9 Income Tax Calculator Application: Introducing the switch Multiple-Selection Statement Tutorial 9 Income Tax Calculator Application: Introducing the switch Multiple-Selection Statement Outline 9.1 Test-Driving the Income Tax Calculator Application 9.2 Introducing the switch Multiple-Selection

More information

Informatica e Sistemi in Tempo Reale

Informatica e Sistemi in Tempo Reale Informatica e Sistemi in Tempo Reale Introduction to C programming Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 25, 2010 G. Lipari (Scuola Superiore Sant Anna)

More information

C programming. Intro to syntax & basic operations

C programming. Intro to syntax & basic operations C programming Intro to syntax & basic operations Example 1: simple calculation with I/O Program, line by line Line 1: preprocessor directive; used to incorporate code from existing library not actually

More information

C++ Input/Output: Streams

C++ Input/Output: Streams C++ Input/Output: Streams 1 The basic data type for I/O in C++ is the stream. C++ incorporates a complex hierarchy of stream types. The most basic stream types are the standard input/output streams: istream

More information

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language Simple C++ Programs Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs Program Structure Constants and Variables C++ Operators Standard Input and Output Basic Functions from

More information

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void 1. Explain C tokens Tokens are basic building blocks of a C program. A token is the smallest element of a C program that is meaningful to the compiler. The C compiler recognizes the following kinds of

More information

conditional statements

conditional statements L E S S O N S E T 4 Conditional Statements PU RPOSE PROCE DU RE 1. To work with relational operators 2. To work with conditional statements 3. To learn and use nested if statements 4. To learn and use

More information

cin >> num1 >> num2 >> num3 >> num4 >> num5; sum = num1 + num2 + num3 + num4 + num5; average = sum / 5.

cin >> num1 >> num2 >> num3 >> num4 >> num5; sum = num1 + num2 + num3 + num4 + num5; average = sum / 5. Lesson Outcomes At the end of this chapter, student should be able to: Understand the requirement of a loop Understand the Loop Control Variable (LCV) Use increment (++) and decrement (--) operators Program

More information

Problem Solving With C++ Ninth Edition

Problem Solving With C++ Ninth Edition CISC 1600/1610 Computer Science I Programming in C++ Professor Daniel Leeds dleeds@fordham.edu JMH 328A Introduction to programming with C++ Learn Fundamental programming concepts Key techniques Basic

More information

Chapter 5. Control Statements: Part 2

Chapter 5. Control Statements: Part 2 Page 1 of 63 [Page 185] Chapter 5. Control Statements: Part 2 Not everything that can be counted counts, and not every thing that counts can be counted. Albert Einstein Who can control his fate? William

More information

Appendix M Introduction to Microsoft Visual C++.NET

Appendix M Introduction to Microsoft Visual C++.NET Appendix M Introduction to Microsoft Visual C++.NET This appendix serves as a quick reference for performing the following operations using the Microsoft Visual C++.NET integrated development environment

More information

Floating Point Numbers

Floating Point Numbers Floating Point Numbers Objectives Use floating point literals Use floating point operators Declare floating point variables Declare and initialize floating point variables Use assignment operator for floating

More information

KENDRIYA VIDYALAYA TIRUMALAGIRI HOLIDAY HOMEWORK CLASS XII COMPUTER SCIENCE (083) C++ Practice Problems

KENDRIYA VIDYALAYA TIRUMALAGIRI HOLIDAY HOMEWORK CLASS XII COMPUTER SCIENCE (083) C++ Practice Problems (i).there are 39 problems. KENDRIYA VIDYALAYA TIRUMALAGIRI HOLIDAY HOMEWORK CLASS XII COMPUTER SCIENCE (083) C++ Practice Problems 1. What is the exact output of the program below? Indicate a blank space

More information

()A Crash Course in Programming with C++ and the Ubuntu O.S. June 8, / 47

()A Crash Course in Programming with C++ and the Ubuntu O.S. June 8, / 47 A Crash Course in Programming with C++ and the Ubuntu O.S. Dr. Daniel A. Ray MCS Dept UVa-Wise June 8, 2010 ()A Crash Course in Programming with C++ and the Ubuntu O.S. June 8, 2010 1 / 47 Outline What

More information

VB.NET Programming Fundamentals

VB.NET Programming Fundamentals Chapter 3 Objectives Programming Fundamentals In this chapter, you will: Learn about the programming language Write a module definition Use variables and data types Compute with Write decision-making statements

More information

Common Beginner C++ Programming Mistakes

Common Beginner C++ Programming Mistakes Common Beginner C++ Programming Mistakes This documents some common C++ mistakes that beginning programmers make. These errors are two types: Syntax errors these are detected at compile time and you won't

More information

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement? 1. Distinguish & and && operators. PART-A Questions 2. How does an enumerated statement differ from a typedef statement? 3. What are the various members of a class? 4. Who can access the protected members

More information

Moving from C++ to VBA

Moving from C++ to VBA Introduction College of Engineering and Computer Science Mechanical Engineering Department Mechanical Engineering 309 Numerical Analysis of Engineering Systems Fall 2014 Number: 15237 Instructor: Larry

More information

Python 3 and IDLE. We will use version 3.x of Python (where x is the most recent version

Python 3 and IDLE. We will use version 3.x of Python (where x is the most recent version Just Enough Python Python 3 and IDLE We will use version 3.x of Python (where x is the most recent version Differences between Python 2 and Python 3 are mostly minor, but can be confusing Python comes

More information

Sequential Program Execution

Sequential Program Execution Sequential Program Execution Quick Start Compile step once always g++ -o Realtor1 Realtor1.cpp mkdir labs cd labs Execute step mkdir 1 Realtor1 cd 1 cp../0/realtor.cpp Realtor1.cpp Submit step cp /samples/csc/155/labs/1/*.

More information

Conditions & Boolean Expressions

Conditions & Boolean Expressions Conditions & Boolean Expressions 1 In C++, in order to ask a question, a program makes an assertion which is evaluated to either true (nonzero) or false (zero) by the computer at run time. Example: In

More information

Chapter 8 Selection 8-1

Chapter 8 Selection 8-1 Chapter 8 Selection 8-1 Selection (Decision) The second control logic structure is selection: Selection Choosing between two or more alternative actions. Selection statements alter the sequential flow

More information

.NET Standard DateTime Format Strings

.NET Standard DateTime Format Strings .NET Standard DateTime Format Strings Specifier Name Description d Short date pattern Represents a custom DateTime format string defined by the current ShortDatePattern property. D Long date pattern Represents

More information

PRINT USING Procedures

PRINT USING Procedures -1 CHAPTER PRINT USING Procedures True BASIC normally prints numbers in a form convenient for most purposes. But on occasion you may prefer a more elaborate form. For eample, you may want to print financial

More information

NUMBERING SYSTEMS C HAPTER 1.0 INTRODUCTION 1.1 A REVIEW OF THE DECIMAL SYSTEM 1.2 BINARY NUMBERING SYSTEM

NUMBERING SYSTEMS C HAPTER 1.0 INTRODUCTION 1.1 A REVIEW OF THE DECIMAL SYSTEM 1.2 BINARY NUMBERING SYSTEM 12 Digital Principles Switching Theory C HAPTER 1 NUMBERING SYSTEMS 1.0 INTRODUCTION Inside today s computers, data is represented as 1 s and 0 s. These 1 s and 0 s might be stored magnetically on a disk,

More information

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures The C++ Language Loops Loops! Recall that a loop is another of the four basic programming language structures Repeat statements until some condition is false. Condition False True Statement1 2 1 Loops

More information

1 Description of The Simpletron

1 Description of The Simpletron Simulating The Simpletron Computer 50 points 1 Description of The Simpletron In this assignment you will write a program to simulate a fictional computer that we will call the Simpletron. As its name implies

More information

6. Control Structures

6. Control Structures - 35 - Control Structures: 6. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat code or take decisions. For that purpose,

More information

INDEX. C programming Page 1 of 10. 5) Function. 1) Introduction to C Programming

INDEX. C programming Page 1 of 10. 5) Function. 1) Introduction to C Programming INDEX 1) Introduction to C Programming a. What is C? b. Getting started with C 2) Data Types, Variables, Constants a. Constants, Variables and Keywords b. Types of Variables c. C Keyword d. Types of C

More information

Course Topics - Outline

Course Topics - Outline Course Topics - Outline Lecture 1 - Introduction Lecture 2 - Lexical conventions Lecture 3 - Data types Lecture 4 - Operators Lecture 5 - Behavioral modeling Lecture 6 - Data flow modeling Lecture 7 -

More information

Automatic Threshold Values and Variables

Automatic Threshold Values and Variables Automatic Threshold Values and Variables In this lesson, we re going to look a little deeper into the world of values, and pay special attention to the programming structures that are used to represent

More information

switch Multiple-Selection Statement

switch Multiple-Selection Statement switch Multiple-Selection Statement (This feature is rarely useful, although it s perfect for programming the iterative song The Twelve Days of Christmas!) If no match occurs, the default case is executed,

More information

Unit 2: Number Systems, Codes and Logic Functions

Unit 2: Number Systems, Codes and Logic Functions Unit 2: Number Systems, Codes and Logic Functions Introduction A digital computer manipulates discrete elements of data and that these elements are represented in the binary forms. Operands used for calculations

More information

UEE1302 (1102) F10 Introduction to Computers and Programming

UEE1302 (1102) F10 Introduction to Computers and Programming Computational Intelligence on Automation Lab @ NCTU UEE1302 (1102) F10 Introduction to Computers and Programming Programming Lecture 03 Flow of Control (Part II): Repetition while,for & do..while Learning

More information

(d) Ask the user enter a name. If the user says Freddy, force the user to keep entering a name until something else is received.

(d) Ask the user enter a name. If the user says Freddy, force the user to keep entering a name until something else is received. Problem 1 Write a complete C++ program that asks a user to enter their day and month of birth. If the user s birthday is March 14 th, the program wishes the user a Happy Birthday, otherwise it just says

More information

4.2 The while Loop. ***Same as Fig 3.6 in intro5e p91

4.2 The while Loop. ***Same as Fig 3.6 in intro5e p91 Chapter 4 Loops Objectives To use while, do-while, and for loop statements to control the repetition of statements ( 4.2-4.4). To understand the flow of control in loop statements ( 4.2-4.4). To use Boolean

More information

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals 1 Recall From Last Time: Java Program import java.util.scanner; public class EggBasket { public static void main(string[]

More information

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall-2015 1

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall-2015 1 QUIZ-II Write a program that mimics a calculator. The program should take as input two integers and the operation to be performed. It should then output the numbers, the operator, and the result. (For

More information

Introduction to Programming

Introduction to Programming Introduction to Programming SS 2012 Adrian Kacso, Univ. Siegen adriana.dkacsoa@duni-siegena.de Tel.: 0271/740-3966, Office: H-B 8406 Stand: April 12, 2012 Betriebssysteme / verteilte Systeme Introduction

More information

Visual Logic Instructions and Assignments

Visual Logic Instructions and Assignments Visual Logic Instructions and Assignments Visual Logic can be installed from the CD that accompanies our textbook. It is a nifty tool for creating program flowcharts, but that is only half of the story.

More information

Deitel Dive-Into Series: Dive Into Microsoft Visual C++ 6

Deitel Dive-Into Series: Dive Into Microsoft Visual C++ 6 1 Deitel Dive-Into Series: Dive Into Microsoft Visual C++ 6 Objectives To understand the relationship between C++ and Visual C++. To be able to use Visual C++ to create, compile and execute C++ console

More information

GETTING STARTED WITH C++ C++ BASICS - 1 -

GETTING STARTED WITH C++ C++ BASICS - 1 - - 1 - GETTING STARTED WITH C++ Programming is a core activity in the process of performing tasks or solving problems with the aid of a computer. An idealised picture is: PROBLEM COMPUTER SOLUTION Unfortunately

More information

Lab Exercise Polymorphic Banking

Lab Exercise Polymorphic Banking Chapter 13 Object-Oriented Programming: Polymorphism 19 Date: Section: This problem is intended to be solved in a closed-lab session with a teaching assistant or instructor present. The problem is divided

More information

Formatting Numbers with C++ Output Streams

Formatting Numbers with C++ Output Streams Formatting Numbers with C++ Output Streams David Kieras, EECS Dept., Univ. of Michigan Revised for EECS 381, Winter 2004. Using the output operator with C++ streams is generally easy as pie, with the only

More information

Java Basics: Data Types, Variables, and Loops

Java Basics: Data Types, Variables, and Loops Java Basics: Data Types, Variables, and Loops If debugging is the process of removing software bugs, then programming must be the process of putting them in. - Edsger Dijkstra Plan for the Day Variables

More information

Programming II with C++ (CSNB244) Lab 12. Topics: C++ Basic Input/Output

Programming II with C++ (CSNB244) Lab 12. Topics: C++ Basic Input/Output Topics: C++ Basic Input/Output In this lab session, you will learn very basic and most common I/O operations required for C++ programming. C++ I/O occurs in streams, which are sequences of bytes. If bytes

More information