1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?
|
|
- Dwayne McDonald
- 6 years ago
- Views:
Transcription
1 Exercises with solutions. 1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl? #include <iostream> b) What using statement do you always put at the top of your programs (to get standard naming conventions)? using namespace std; c) Declare an integer variable N and initialize it to 10. Any of these: i) int N = 10; ii) int N(10); iii) int N; N = 10; d) Give a statement to increase the value of variable N by 10. Any of these: i) N = N + 10; ii) N += 10; e) What library would you include if you wanted to use the sin, cos, and tan functions? <cmath> f) What library would you include if you wanted to use pseudo-random numbers? <cstdlib> 2. Complete the code segment below to print There are XXX days. if integer x has a value 1 through 7 inclusive. Replace XXX with the value of variable x; note the period. int x; cin >> x; // add your code here if ((x >=1) && (x <= 7)) { cout << There are " << x << " days.\n"; 3. What is the final value of each expression? Watch for integer division. a) / b) 15 / c) (int) / 4; // casting has precedence over division 7 4. You have an integer variable x that holds a four-digit positive integer. Write C++ code to store the middle two digits in integer variable y. Any of these: i) y = x / 10 % 100; ii) y = x % 1000 / 10; 5. What is the output from each code segment below? a) int x = 1, y = 0;
2 if (x > 0 && y < 0) { x = y = 23; cout << x << " " << y << endl; Ans: 1 0 b) int x = 1, y = 0; if (x > 0 y < 0) { x = y = 23; cout << x << " " << y << endl; Ans: c) x = 10; y = 40; if (x >= 10) { if (y < 40) { y++; else { y--; cout << x << " " << y << endl; Ans: d) x = 10; y = 40; if (x >= 10) { if (y < 40) { y++; else { y--; cout << x << " " << y << endl; Ans: e) In part d) above, under what condition(s) would y be incremented? Ans: If x is greater than or equal to 10 and y is less than How many times does each loop execute? a) for (i = 0; i <= 100; i++) { statements; Ans: 101 times b) for (i = 1; i < 100; i++) { statements; Ans: 99 times 7. Examine the code below. What (not how) does the following function do? int mystery(int x, int y, int z) { int temp; // ultimately, holds returned value temp = x; // assume the largest is x if(y > temp) { temp = y; if(z > temp) { temp = z; return temp; Ans: It returns the maximum of x, y, and z.
3 8. The Fibonacci numbers are defined: fibonacci (0) = 1 fibonacci (1) = 1 fibonacci (n) = fibonacci (n 1) + fibonacci (n 2); Write a recursive C++ function to compute the Fibonacci numbers: Ans: int fibonacci (int n) { // fill in if ((n == 0) (n == 1)) { return 1; 9. What is printed in each case: a) cout << 3 * 4+5 << endl; Ans: 17 else { return fibonacci(n-1) + fibonacci(n-2); b) cout << / 4 << endl; Ans: 19 c) string N = "17"; cout << N + "10 / 4" << endl; Ans: 1710 / 4 d) int N = 17; cout << (N = 16) << endl; Ans: Suppose that every time currenttime() is called, it returns the current time in the form of an integer (typically the number of seconds since a certain day in 1970). Using currenttime() and motors(), write C++ code to make the robot move forward at half speed for 45 seconds. Ans: int start = currenttime(); while ((currenttime() - start < 45) { robot.motors(0.5,0.5); 11. If A and B are boolean variables, then the expression (!A) (!B) is equivalent to which of the following conditions: a) (! (A && B)) b) (! (A B))
4 c) (!A) && (!B) d) true Ans: (a) 12. Study the following code segment. int N = 7; int i, x; for (i = 1; i <= N; i++) { cin >> x; if(i % 3 == 0) { cout << endl; cout << endl; cout << i << ". " << x << " "; a) What is displayed with the given input? Watch the endl characters. Input: Ans: b) In terms of N, what is the value of the loop control variable upon termination? Ans: N a) Rewrite the following C++ statement to make it more robust (less prone to error). if (x / y!= 0 && y!= 0) { cout << x / y << endl; Ans: The operands of && are evaluated in order (not mentioned -5 of 6- previously, but now you know!), and we don't want to divide by 0, so: if (y!= 0 && x / y!= 0) { cout << x / y << endl; b) Rewrite the following C++ code segment to make it more efficient and to remove any unnecessary code. if (x < y && y > x) { cout << "Not good.\n"; Ans: "x < y" and "y > x" always have the same truth value, so: if (x < y) { cout << "Not good.\n"; c) Rewrite the following C++ code segment to make it more efficient and to remove any unnecessary code. if (x < y && y >= x) { cout << "Not good.\n"; Ans: Since x<y implies x<=y, which is equivalent to y>=x:
5 if (x < y) { cout << "Not good.\n"; d) Rewrite the following C++ code segment to make it more efficient and to remove any unnecessary code. if (x < y y >= x) { cout << "Not good.\n"; Ans: Similar explanation to previous. if (y >= x) { cout << "Not good.\n"; 14. Give 3 reasons why it's important to comment your code. What are the 2 different ways to comment code in C++ and how are they different? It's important to comment your code because:- 1. it makes easier for other people to understand your program. 2. it helps you while programming to organize and write your code. 3. it helps you track down and debug errors. The // comment style comments out any text on that line to the right of the slashes. The /* */ comment style comments out all code contained between the asterisks. 15. Write the output of the following segments of code. a.) int x = 3; cout << x << 2*x; Ans: 36 b.) cout << "\"Hello\\\n Gandalf!"; Ans: "Hello\ Gandalf! (Note the space before Gandalf.) c.) int x = 7; int y = 3; cout << x/y << " and " << x%y; Ans: 2 and Briefly explain and correct the error(s) in each of the code segments below. a.) string word; cout << "Enter a word: "; cin << word;
6 Ans: The input pull << goes the wrong way. Should be: cin >> word; b.) cout << "Two plus two is " 2+2; Ans: Should be another output push << cout << "Two plus two is " << 2+2; c.) if ( x = 1 ); cout << x; Ans: The = sign is an assignment, not a comparison. After the condition, there should not be a semicolon. if ( x == 1) d.) if ( x = 1 or 2 ) cout << x; Ans: Again uses = sign. Also need to use for "or" and 2 boolean statements: if ( x == 1 x == 2 ) e.) //This code is supposed to compute 10! int N = 10; int factorial = 1; while ( N >= 1 ) { factorial = factorial * N; N--; cout << "10! is " << factorial << ".\n"; Ans: Correctly computes 10!, but the final output statement should occur outside the loop. Otherwise, it prints out 10 different answers for 10! N--; cout << "10! is " << factorial << ".\n"; 17. The program below is supposed to compute the average of two numbers. The program compiles without error, but does not correctly compute the average. Describe the logic error(s) and write the corrected lines. You do not need to rewrite the entire program. The line numbers are written along the left column to help you write your answer; they are not part of the program code. Make as few changes as possible. 1 # include <iostream> 2 using namespace std; 3 int main ( ) {
7 4 int total; 5 cout << "Enter a number: "; 6 int a; 7 cin >> a; 8 total = total + a; 9 cout << "Enter another number: "; 10 int b; 11 cin >> b; 12 total = total + a; 13 double average = total / 2; 14 cout << "The average is " << average << ".\n"; 15 return 0; 16 Ans: Line 8: Adds a to total before total is initialized. Should be: total = a; Line 12: Again adds a to the total. Should have added b, or else total = 2*a. total = total + b; Line 13: Performs integer division, so we will lose fractional part. If the sum is odd, then the average should end.5. We can correct this by casting the variables. double average = (double) total / 2; 18. Write a program (starting from #include) that repeatedly collects positive integers from the user, stopping when the user enters a negative number or zero. After that, output the product of all positive entries. A sample run should appear on the screen like the text below. Enter a number: 3 Enter a number: 10 Enter a number: 2 Enter a number: -213 The product of all your positive numbers is 60. # include <iostream> using namespace std; int main ( ) {
8 int x = 1; int product = 1; while ( x > 0 ) { product *= x; cout << "Enter a number: "; cin >> x; cout << "The product of all your positive numbers is " << product << ".\n"; return 0; Note: This code assumes the user entered at least one positive number, which is a reasonable assumption. We could add an if statement at the beginning to handle the case when the first number entered is non-positive. But the solution above would receive full credit. 19. What does the following code print out? a.) int x = 4; do { cout << x << "+"; x = x+3; while (x%4!= 0); Ans: b.) for (int i=10; i > 5; i--) { if ( i %2 == 0) cout << 2*i << "a"; else cout << i << "b"; Ans: 20a9b16a7b12a c.) string first_name = "Frodo"; string last_name = "Baggins"; if (!(first_name=="frodo" && last_name=="smith")) cout << "true";
9 else cout << "false"; Ans: true 20. Rewrite the following do loop as a for loop. What does it print out? int x = -10; do { cout << x << " & "; x += 2; while (x < 100); Ans: for (int x = -10; x < 100; x +=2) { cout << x << " & "; Prints out the even numbers -10 through 98 with an & in between. 21. What is the output of the following program? Track the variables in a walkthrough table. int my_fun (int a, int& b, int c) { a = 2*a+b; b = c; cout << "In my_fun, a=" << a << " b=" << b << ".\n"; return a%2; int main( ) { int x = 1; int y = -2; int z = 3; x = my_fun(x,y,z); cout << "In main, x=" << x << " y=" << y << " z=" << z << ".\n"; return 0; Ans: In my_fun, a=0 b=3. In main, x=0 y=3 z= What is the output of the following program? Track the variables in a walkthrough table. bool test_fun (int x, int& y) {
10 if (x < y y > 1) return true; else { y = x%2; return false; int main( ) { int y = 1; for (int x= 0; x < 4; x++) { if ( test_fun(x, y) ) cout << "TRUE y=" << y << "\n"; else cout << "FALSE y=" << y << "\n"; return 0; Ans: TRUE y=1 FALSE y=1 FALSE y=0 FALSE y=1 23. Write a function grade2number that takes a student's letter grade without +/- (A,B,C,D,F) as a char and returns the corresponding numerical score in the GPA (4.0,3.0,2.0,1.0,0.0). You should handle the case when the student enters an invalid character (e.g. G) by return the number Use a switch statement. The function will be called as in the code below. char grade; cout << "Enter your letter grade (no +/-): "; cin >> grade; cout << "The corresponding numeric grade is: " << grade2number(grade); (As a follow-up practice exercise, you might want to consider how to handle grades with a + or -. It's a little tricky, because we can't switch on strings.) double grade2number ( char grade ) {
11 switch (grade) { case 'A': return 4.0; break; case 'B': return 3.0; break; case 'C': return 2.0; break; case 'D': return 1.0; break; case 'F': return 0.0; break; default: return -1.0; 24. Write a function largest2primes that finds the 2 largest prime factors of a given integer greater than one. Here we will report 1 as a prime number, if no larger prime factor is available. Your function should use a for loop. The function will be called as in the code below. int x; cout << "Enter an integer larger than one: "; cin >> x ; int factor1, factor2; largest2primes (x, factor1, factor2); cout << "The two largest prime factors of your number are "<< factor1 << " and " << factor2; void largest2primes (int x, int& factor1, int& factor2) { factor2 = 1; int divisor = 2; while (divisor <= x) { if (x%divisor == 0) { else return; x = x/divisor; factor1 = factor2; factor2 = divisor; divisor++;
Review Questions I Spring 2010
Review Questions I Spring 2010 The following review questions are similar to the kinds of questions you will be expected to answer on Exam I (tentatively scheduled for Mar. 4), which will cover LCR, chs.
More informationExam I Review Questions Fall 2010
Exam I Review Questions Fall 2010 The following review questions are similar to the kinds of questions you will be expected to answer on Exam I (scheduled for Oct. 14), which will cover LCR, chs. 1 7,
More informationo Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement
Last Time Let s all Repeat Together 10/3/05 CS150 Introduction to Computer Science 1 1 We covered o Counter and sentinel controlled loops o Formatting output Today we will o Type casting o Top-down, stepwise
More informationMultiple 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 information5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators
Chapter 5: 5.1 Looping The Increment and Decrement Operators The Increment and Decrement Operators The Increment and Decrement Operators ++ is the increment operator. It adds one to a variable. val++;
More informationReview: 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 informationExam I KEY (corrected)
CS 102 * * * Sections 2 5 * * * Spring 2010 Exam I KEY (corrected) Name: KEY Section: Use the back for scratch paper if you need it. Make sure your final answer is clear to us (put a circle or box around
More informationDiscussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:
Discussion 1H Notes (Week 3, April 14) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 More on Arithmetic Expressions The following two are equivalent:! x = x + 5;
More informationChapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the
More information1- 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 informationProgramming Language. Functions. Eng. Anis Nazer First Semester
Programming Language Functions Eng. Anis Nazer First Semester 2016-2017 Definitions Function : a set of statements that are written once, and can be executed upon request Functions are separate entities
More informationComputer 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 informationUnit 7. 'while' Loops
1 Unit 7 'while' Loops 2 Control Structures We need ways of making decisions in our program To repeat code until we want it to stop To only execute certain code if a condition is true To execute one segment
More information#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }
1. (9 pts) Show what will be output by the cout s in this program. As in normal program execution, any update to a variable should affect the next statement. (Note: boolalpha simply causes Booleans to
More informationA 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 informationIncrement and the While. Class 15
Increment and the While Class 15 Increment and Decrement Operators Increment and Decrement Increase or decrease a value by one, respectively. the most common operation in all of programming is to increment
More informationCS2141 Software Development using C/C++ C++ Basics
CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short
More informationMultiple 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 informationCSCE 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 informationObjectives. 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 informationLaboratory 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 informationCSCE Practice Midterm. Data Types
CSCE 2004 - Practice Midterm This midterm exam was given in class several years ago. Work each of the following questions on your own. Once you are done, check your answers. For any questions whose answers
More informationC++ 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 informationC++ Programming: From Problem Analysis to Program Design, Third Edition
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection) Control Structures A computer can proceed: In sequence Selectively (branch) - making
More informationLab Instructor : Jean Lai
Lab Instructor : Jean Lai Group related statements to perform a specific task. Structure the program (No duplicate codes!) Must be declared before used. Can be invoked (called) as any number of times.
More informationThe following expression causes a divide by zero error:
Chapter 2 - Test Questions These test questions are true-false, fill in the blank, multiple choice, and free form questions that may require code. The multiple choice questions may have more than one correct
More informationChapter 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 informationCSCE Practice Midterm. Data Types
CSCE 2004 - Practice Midterm This midterm exam was given in class several years ago. Work each of the following questions on your own. Once you are done, check your answers. For any questions whose answers
More informationMultiple Choice (Questions 1 14) 28 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 informationSummary of basic C++-commands
Summary of basic C++-commands K. Vollmayr-Lee, O. Ippisch April 13, 2010 1 Compiling To compile a C++-program, you can use either g++ or c++. g++ -o executable_filename.out sourcefilename.cc c++ -o executable_filename.out
More informationProgramming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester
Programming Language Control Structures: Repetition (while) Eng. Anis Nazer Second Semester 2017-2018 Repetition statements Control statements change the order which statements are executed Selection :
More informationCAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE
CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE a) Mention any 4 characteristic of the object car. Ans name, colour, model number, engine state, power b) What
More informationBEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1
BEng (Hons) Electronic Engineering Cohort: BEE/10B/FT Resit Examinations for 2016-2017 / Semester 1 MODULE: Programming for Engineers MODULE CODE: PROG1114 Duration: 3 Hours Instructions to Candidates:
More informationChapter 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 informationA SHORT COURSE ON C++
Introduction to A SHORT COURSE ON School of Mathematics Semester 1 2008 Introduction to OUTLINE 1 INTRODUCTION TO 2 FLOW CONTROL AND FUNCTIONS If Else Looping Functions Cmath Library Prototyping Introduction
More informationdo { statements } while (condition);
Topic 4 1. The while loop 2. Problem solving: hand-tracing 3. The for loop 4. The do loop 5. Processing input 6. Problem solving: storyboards 7. Common loop algorithms 8. Nested loops 9. Problem solving:
More informationWhy Is Repetition Needed?
Why Is Repetition Needed? Repetition allows efficient use of variables. It lets you process many values using a small number of variables. For example, to add five numbers: Inefficient way: Declare a variable
More informationThe American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010
The American University in Cairo Computer Science & Engineering Department CSCE 106-08 Dr. KHALIL Exam II Spring 2010 Last Name :... ID:... First Name:... Form - I EXAMINATION INSTRUCTIONS * Do not turn
More informationModule Contact: Dr Pierre Chardaire, CMP Copyright of the University of East Anglia Version 1
UNIVERSITY OF EAST ANGLIA School of Computing Sciences Main Series UG Examination 2015/16 INTRODUCTORY PROGRAMMING CMP-0005B Time allowed: 2 hours. Answer BOTH questions from section A and ONE question
More informationOperations. Making Things Happen
Operations Making Things Happen Object Review and Continue Lecture 1 2 Object Categories There are three kinds of objects: Literals: unnamed objects having a value (0, -3, 2.5, 2.998e8, 'A', "Hello\n",...)
More informationDHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++
DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++ Objective: To Learn Basic input, output, and procedural part of C++. C++ Object-orientated programming language
More informationProgramming. 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 informationThe American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011
The American University in Cairo Department of Computer Science & Engineering CSCI 106-07&09 Dr. KHALIL Exam-I Fall 2011 Last Name :... ID:... First Name:... Form I Section No.: EXAMINATION INSTRUCTIONS
More informationObjectives. 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 informationChapter 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 informationChapter 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 informationChapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++
Repetition Contents 1 Repetition 1.1 Introduction 1.2 Three Types of Program Control Chapter 5 Introduction 1.3 Two Types of Repetition 1.4 Three Structures for Looping in C++ 1.5 The while Control Structure
More informationYour first C++ program
Your first C++ program #include using namespace std; int main () cout
More informationGE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004
GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004 Functions and Program Structure Today we will be learning about functions. You should already have an idea of their uses. Cout
More informationCPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)
Name Definition Matching (8 Points) 1. (8 pts) Match the words with their definitions. Choose the best definition for each word. Relational Expression Iteration Counter Count-controlled loop Loop Flow
More informationThe 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 informationOther operators. Some times a simple comparison is not enough to determine if our criteria has been met.
Lecture 6 Other operators Some times a simple comparison is not enough to determine if our criteria has been met. For example: (and operation) If a person wants to login to bank account, the user name
More informationLAB 4.1 Relational Operators and the if Statement
LAB 4.1 Relational Operators and the if Statement // This program tests whether or not an initialized value of num2 // is equal to a value of num1 input by the user. int main( ) int num1, // num1 is not
More informationWARM UP LESSONS BARE BASICS
WARM UP LESSONS BARE BASICS CONTENTS Common primitive data types for variables... 2 About standard input / output... 2 More on standard output in C standard... 3 Practice Exercise... 6 About Math Expressions
More informationComputer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons
Computer Programming Basic Control Flow - Loops Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Objectives To learn about the three types of loops: while for do To avoid infinite
More informationChapter 3. More Flow of Control
Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-2 Flow Of Control Flow of control refers to the
More informationChapter 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 informationCHAPTER 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 informationCSCI 111 First Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, October 18, 2017
QUEENS COLLEGE Department of Computer Science CSCI 111 First Midterm Exam Fall 2017 10.18.17 Solutions 09.00am 09.50am, Wednesday, October 18, 2017 Problem 1 (10 points) The following C++ program has errors
More informationSOLUTIONS TO EXERCISES PART ONE: Problem-Solving Techniques
SOLUTIONS TO EXERCISES PART ONE: Problem-Solving Techniques 1 Principles of Programming and Software Engineering 1 const CENTS_PER_DOLLAR = 100; void computechange(int dollarcost, int centscost, int& d,
More informationChapter 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 informationChapter 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 informationCSCE 206: Structured Programming in C++
CSCE 206: Structured Programming in C++ 2017 Spring Exam 2 Monday, March 20, 2017 Total - 100 Points B Instructions: Total of 13 pages, including this cover and the last page. Before starting the exam,
More informationChapter 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 informationChapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II (Repetition) 1 Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled, sentinel-controlled, flag-controlled, and
More informationFORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):
FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each): 1. The basic commands that a computer performs are input (get data), output (display result),
More information5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!
FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each): 1. Assume that all variables are properly declared. The following for loop executes 20 times.
More information! 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 informationSolving a 2D Maze. const int WIDTH = 10; const int HEIGHT = 10;
Solving a 2D Maze Let s use a 2D array to represent a maze. Let s start with a 10x10 array of char. The array of char can hold either X for a wall, for a blank, and E for the exit. Initially we can hard-code
More informationIntroduction to Programming
Introduction to Programming Summer Term 2015 Dr. Adrian Kacso, Univ. Siegen adriana.dkacsoa@duni-siegena.de Tel.: 0271/740-3966, Office: H-B 8406 State: May 6, 2015 Betriebssysteme / verteilte Systeme
More information1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?
Instructions: This homework assignment focuses primarily on some of the basic syntax and semantics of C++. The answers to the following questions can be determined from Chapters 6 and 7 of the lecture
More informationBasic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable
Basic C++ Overview C++ is a version of the older C programming language. This is a language that is used for a wide variety of applications and which has a mature base of compilers and libraries. C++ is
More informationChapter 4 - Notes Control Structures I (Selection)
Chapter 4 - Notes Control Structures I (Selection) I. Control Structures A. Three Ways to Process a Program 1. In Sequence: Starts at the beginning and follows the statements in order 2. Selectively (by
More informationLoops / Repetition Statements
Loops / Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops C has three kinds of repetition statements: the while loop the for
More informationGovernment Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING
Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING THROUGH C++ Practical: OOPS THROUGH C++ Subject Code: 1618407 PROGRAM NO.1 Programming exercise on executing a Basic C++
More informationChapter 3 - Functions
Chapter 3 - Functions 1 Outline 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3.4 Functions 3.5 Function Definitions 3.6 Function Prototypes 3.7 Header Files 3.8 Random Number
More informationAdd Subtract Multiply Divide
ARITHMETIC OPERATORS if AND if/else AND while LOOP Order of Operation (Precedence Part 1) Copyright 2014 Dan McElroy Add Subtract Multiply Divide + Add - Subtract * Multiply / Divide = gives the quotient
More informationTopic 2: C++ Programming fundamentals
Topic 2: C++ Programming fundamentals Learning Outcomes Upon successful completion of this topic you will be able to: describe basic elements of C++ programming language compile a program identify and
More informationChapter 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 informationChapter 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 informationChapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions
Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Copyright 2011 Pearson Addison-Wesley. All rights reserved.
More informationC++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved. Using a Debugger WE5.
Using a Debugger WE5 W o r k E D E x a m p l e 5.2 Using a Debugger As you have undoubtedly realized by now, computer programs rarely run perfectly the first time. At times, it can be quite frustrating
More informationLECTURE 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 informationIntroduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)
Introduction to C++ 1. General C++ is an Object oriented extension of C which was derived from B (BCPL) Developed by Bjarne Stroustrup (AT&T Bell Labs) in early 1980 s 2. A Simple C++ Program A C++ program
More informationCS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12
CS31 Discussion 1E Jie(Jay) Wang Week3 Oct.12 Outline Problems from Project 1 Review of lecture String, char, stream If-else statements Switch statements loops Programming challenge Problems from Project
More information7/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 informationFundamentals 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 informationGetting 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 informationUEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter
UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter What you will learn from Lab 7 In this laboratory, you will understand how to use typical function prototype with
More informationA 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 informationI SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.
I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK 70. a) What is the difference between Hardware and Software? Give one example for each. b) Give two differences between primary and secondary memory.
More informationCS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM
CS 117 Programming II, Spring 2018 Dr. Ghriga Midterm Exam Estimated Time: 2 hours March 21, 2018 DUE DATE: March 28, 2018 at 12:00 PM INSTRUCTIONS: Do all exercises for a total of 100 points. You are
More informationChapter 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 informationADARSH 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 information1 Unit 8 'for' Loops
1 Unit 8 'for' Loops 2 Control Structures We need ways of making decisions in our program To repeat code until we want it to stop To only execute certain code if a condition is true To execute one segment
More information5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.
Chapter 5: Looping 5.1 The Increment and Decrement Operators Copyright 2009 Pearson Education, Inc. Copyright Publishing as Pearson 2009 Addison-Wesley Pearson Education, Inc. Publishing as Pearson Addison-Wesley
More informationCh 6. Functions. Example: function calls function
Ch 6. Functions Part 2 CS 1428 Fall 2011 Jill Seaman Lecture 21 1 Example: function calls function void deeper() { cout
More information8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );
- 50 - Control Structures: 8. Functions (II) Arguments passed by value and by reference. Until now, in all the functions we have seen, the arguments passed to the functions have been passed by value. This
More informationThe 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 informationPseudo-code vs. Assembly. Introduction to High-Level Language Programming. Disadvantages of Assembly. Pseudo-code vs. High-level Programs
Pseudo-code vs. Assembly Introduction to High-Level Language Programming Chapter 7 Set sum to 0 While i 5 do Get value for N Add N to sum Increase value of i by 1 Print the value of sum.begin -- Sum 5
More information