Introduction to Algorithms and Programming (COMP151)

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

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

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.

Unit 3. Operators. School of Science and Technology INTRODUCTION

Programming for Engineers Iteration

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

REPETITION CONTROL STRUCTURE LOGO

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

Add Subtract Multiply Divide

CSI33 Data Structures

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

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

C++ Programming for Non-C Programmers. Supplement

LAB 4.1 Relational Operators and the if Statement

Computer Programming : C++

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

C++ Programming for Non-C Programmers. Supplement

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

In this chapter you will learn:

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

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

A Freshman C++ Programming Course

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

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

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

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

CHAPTER 4 FUNCTIONS. 4.1 Introduction

UNIT- 3 Introduction to C++

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Looping. Arizona State University 1

Programming in C++ The manager of a company. Lecture Notes 6. Functions (Procedures) 4/24/2018. he he he. Does Does Does

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

The Unix Environment for Programming (COMP433)

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

Errata for C++ and Algorithmic Thinking for the Complete Beginner

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Why Is Repetition Needed?

Chapter1 Overview of computers

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

Other operators. Some times a simple comparison is not enough to determine if our criteria has been met.

POINTERS. Pointer is a memory variable which can store address of an object of specified data type. For example:

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

COMPUTER SCIENCE (083)

True or False (14 Points)

Basic Source Character Set for C++ Language:

Computer Engineering Department CMPE110 Midterm Sample Questions, 2017/ Fall

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

Programming. C++ Basics

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

DELHI PUBLIC SCHOOL TAPI

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

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

Objectives. In this chapter, you will:

CS2141 Software Development using C/C++ C++ Basics

Name SECTION: 12:45 2:20. True or False (12 Points)

Lecture 2:- Functions. Introduction

Chapter 2: Basic Elements of C++

Introduction to Programming Using Java (98-388)

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

C++ PROGRAMMING SKILLS Part 2 Programming Structures

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

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

CS242 COMPUTER PROGRAMMING

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Operators in C. Staff Incharge: S.Sasirekha

Unit 7. 'while' Loops

Scientific Computing

WARM UP LESSONS BARE BASICS

Introduction to Programming using C++

Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

Computers Programming Course 6. Iulian Năstac

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Assignment: 1. (Unit-1 Flowchart and Algorithm)

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Object Oriented Design

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

CS313D: ADVANCED PROGRAMMING LANGUAGE

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Fundamentals of Programming CS-110. Lecture 2

Chapter 2 - Control Structures

Engineering Problem Solving with C++, Etter/Ingber

Structured Programming. Flowchart Symbols. Structured Programming. Selection. Sequence. Control Structures ELEC 330 1

C++ for Python Programmers

CS111: PROGRAMMING LANGUAGE II

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Object-Oriented Programming. Chapter 4

A First Program - Greeting.cpp

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Lecture 3 Tao Wang 1

Transcription:

Introduction to Algorithms and Programming (COMP151) A Student's Manual for Practice Exercises Dr. Mohamed Aissa m.issa@unizwa.edu.om 11i13 Summer 2014

Practice Exercises #1 Introduction Page 2 Practice Exercises #1 Introduction to C++ STUDENT LEARNING OUTCOMES: Upon completion of these exercises, a successful student will be able to: state the followed steps to execute C++ program, understand header files, differentiate between local and global variables, use comment lines, declare variables Questions 1. Complete the following table to show the difference between an interpreter and a compiler: Compiler Interpreter 1. Fill the following table using numbers from 1 to 4 to indicate the order of executing of C++ program. Linking Compiling Execution Program Editing

Practice Exercises #1 Introduction Page 3 3. Write in the following table the operation yielding to each mentioned file. Operation Source Code Object file Executed Program 4. What header file must you #include with your source file to use cout and cin? 5. What header file must you #include with your program to use setw? 6. What is the difference between local and global variables? 7. What is the scope of local and global variables?

Practice Exercises #1 Introduction Page 4 8. What are the symbols used for comment lines? 9. Why do we use comment lines? 10. Some lines can begin with # in a C++ program. What is the effect of this symbol on such lines? 10. Explain the role of the following line in a C++ program. #include<iostream> 11. Explain the role of the following line in a C++ program.

Practice Exercises #1 Introduction Page 5 12. What are identifiers? 13. Are there any restrictions on variable names? 14. Give the definition of reserved keywords. 15. Explain the following line in a C++ program? int a(0);

Practice Exercises #2 Operators in C++ Page 6 Practice Exercises #2 Operators in C++ STUDENT LEARNING OUTCOMES: Upon completion of this assignment, a successful student will be able to use the following operators in C++ programs: arithmetic operators, assignment operator, compound assignment (or) Arithmetic assignment operators, increment and Decrement Operators, relational and equality operators, logical operators (!, &&, ), conditional operator (? : ) or Ternary Operator, explicit type casting operator, precedence of arithmetic operators Exercises 1. Fill the following table using different methods to write the expression: Classical Form First New Method Second New Method z=z+1 w=w-1

Practice Exercises #2 Operators in C++ Page 7 2. Fill in the following table: x=2, y=1, z=3 Expression Expression Precedence Output Data X+y*z X+(y*z) X + y*z - z%x X + y*z z/y 3. For each expression in the following table, write whether the result is True of False. Expression True (1) or False (0) (8 == 8) && (7!= 9) (9 > 3) (10 < 1) (6 == 2) && (12 == 13)!(3 == 7)

Practice Exercises #2 Operators in C++ Page 8 4. Evaluate the following expressions: v=8*15+10%6*7-9 w=(9*(16+9)%3)*8-15 x=90*10+29/5%2*3-6*2 y=15/3-7*8-6*4+10%3 5. Show the order of evaluation and find the final result for each of the following expression. x=6, y=3, z=8, w=1 a. M= x/(y+z*w-1)%(y+1+w) b. M=(x-3)/(y-2)*(w-z%3)

Practice Exercises #2 Operators in C++ Page 9 4. In the following program, we numerated each printed line. #include <iostream> main( ) int i=7; ( 1 ) cout<<"\n"<<i++; ( 2 ) cout<<"\n"<<i; ( 3 ) cout<<"\n"<<--i; ( 4 ) cout<<"\n"<<i; ( 5 ) cout<<"\n"<<i--; ( 6 ) cout<<"\n"<<i; return (0); a. What is the initial value of " i "? The initial value of " i " is.. b. Write the current printed value of " i " after the execution of each numerated line. Line Number Printed value of " i " 1 2 3 4 5 6

Practice Exercises #2 Chapter 3 Operators in C++ Page 10 5. What are the outputs of the following program? #include<iostream> int main() int x,y; x=10; y=4; x=b; y=7; cout<<"x="<<x<<endl; cout<<"y="<<y; return(0); Output x= y= 6. What are the outputs of the following program? #include<iostream> int main() int x,y,z; x=10; y=4; z=15 x+=3; y*=5; Z/=3; cout<<"x="<<x<<endl; cout<<"y="<<y<<endl; cout<<"z="<<z<<endl; return(0); Output x= y=

Practice Exercises #2 Chapter 3 Operators in C++ Page 11 7. Explain the following line in a C++ program. condition? result1 : result2 8. What are the outputs of the following line? 9==7+2? 4 : 3 7>9? a : b 9. What is the value of i in the following part of a C++ program? int i; float f =229.766; i=(int)f;

Practice Exercises #3 Chapter 4 Control Structures Page 12 Practice Exercises #3 Conditional Structures Related Chapter Chapter Title 4 Control Structures STUDENT LEARNING OUTCOMES: Upon completion of this computer lab, a successful student will be able to: draw a flowchart to calculate the value of a given function containing conditions, translate the flowchart into a C++ program, understand compilation errors and correct them. Questions 1. Suppose the following function: ( ) a. Complete the following flowchart to calculate ( ) b. Based on the flowchart, complete the following C++ program.

Practice Exercises #3 Chapter 4 Control Structures Page 13 START #include <iostream> int main( ) yes no int x; int f; cout <<"Input x "<<endl; cin>>x; f=0; if ( ) else ; ; cout << " x = "<<x<<endl; cout << " f="<<f<<endl; return (0); END

Practice Exercises #3 Chapter 4 Control Structures Page 14 2. Suppose the following function: ( ) a. Complete the following flowchart to calculate ( ) b. Based on the flowchart, complete the following C++ program. START #include <iostream> int main() int f; int x; yes no cout <<"Input x "<<endl; cin>>x; f=0; if ( ) else ; ; END cout << " x = "<< <<endl; cout << " f="<< <<endl; return (0);

Practice Exercises #3 Chapter 4 Control Structures Page 15 3. Suppose the following function: ( ) a. Draw the flowchart to calculate ( ) b. Based on the flowchart, write the C++ program.

Practice Exercises #3 Chapter 4 Control Structures Page 16 4. Suppose the following function: ( ) c. Draw the flowchart to calculate ( ) d. Based on the flowchart, write the C++ program. START yes no yes no yes no END

Practice Exercises #3 Chapter 4 Control Structures Page 17 5. Draw a flowchart to find the roots of the following second degree equation in the set of the real numbers: Flowchart ax 2 bx c 0, a, b, c, a 0,

Practice Exercises #3 Chapter 4 Control Structures Page 18 Based on this algorithm, write the corresponding C++program.

Practice Exercises #3 Chapter 4 Control Structures Page 19

Practice Exercises #4 Chapter 4 Control Structures Page 20 Practice Exercises #4 Looping Structures Related Chapter Chapter Title 4 Control Structures STUDENT LEARNING OUTCOMES: Upon completion of this computer lab, a successful student will be able to: draw a flowchart containing a loop, understand the different looping structures in C++, translate the flowchart into a C++ program and write three C++ programs using the three loop types, understand compilation errors and correct them. Questions 1. Draw a flowchart to calculate the following integer summation: ( ) 2. Based on this flowchart, write the C++ program using the three looping types (for, while, and do while).

Practice Exercises #4 Chapter 4 Control Structures Page 21 START // Summation of integer numbers included between 1 and n using for loop n s i s s + i i i + #include <iostream> int main() int sum= 0; int number=1, n; cout << " Input a positive integer number : "<<endl; cin>>n; for(number=1;number<= n;number++) yes i n sum=sum+number; n s END no cout << "\n Sum of numbers includeded between 1 and " <<n<<" = "<<sum<<endl<<endl; return (0); // indicate program ended successfully

Practice Exercises #4 Chapter 4 Control Structures Page 22 // Summation of integer numbers included between 1 and n using while loop // Summation of integer numbers included between 1 and n using do while Loop #include <iostream> int main() int sum= 0; int number=1, n; cout << " Input a positive integer number : "<<endl; cin>>n; while ( number <= n) sum += number; number++; cout << "\n Sum of numbers includeded between 1 and " <<n<<" = "<<sum<<endl<<endl; #include <iostream> int main() int sum= 0; int number=1, n; cout << " Input a positive integer number : " <<endl; cin>>n; do sum += number; number++; while(number<=n); return (0); cout << "\n Sum of numbers includeded between 1 and " <<n<<" = "<<sum<<endl<<endl; return (0);

Practice Exercises #5 Chapter 5 Arrays Page 23 Practice Exercises #5 One-Dimensional Arrays Related Chapter Chapter Title 5 Arrays STUDENT LEARNING OUTCOMES: Upon completion of this computer lab, a successful student will be able to: declare one-dimensional arrays, initialize a one-dimensional arrays, print the elements of one-dimensional arrays, detect syntax errors and correct them Reminder Initializing One-Dimensional Array in Declarations The initialization in declaration the one-dimensional array of yearly expenses, is done as follows: int e [12] = 3200, 2500, 2800, 3100, 4000, 4500, 5000, 3500, 2500, 4100, 6000, 5500; Here e[0] is initialized to 3200; e[1] is initialized to 2500, and so on. Observation We can omit the size of the array when we initialize it in a declaration: Float marks[ ] = 1.5, 9.10, 11, 12;

Practice Exercises #5 Chapter 5 Arrays Page 24 Example 1 Let's shows how to declare an array of five integers. 1 2 3 4 5 6 7 8 9 10 11 12 13 //How to declare an array and to fill it #include <iostream> int main() int A[5]=-1, 2, 9, -3, 10 ; int i; for (i = 0; i<5; i++) cout << "A[" << i << "]: "<< A[i] << "\n"; return(0); Program Output A[0]: -1 A[1]: 2 A[2]: 9 A[3]: -3 A[4]: 10 Press any key to continue

Practice Exercises #5 Chapter 5 Arrays Page 25 Example 2 Let's shows how to declare an array of five integers and fill each with a value. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 /How to declare an array and to fill it #include <iostream> int main() int A[5]; int i; cout <<"\n Input the values of the array elements \n"<<endl; for ( i=0; i<5; i++) // 0-4 cout << "A[" << i << "]= "; cin >> A[i]; cout <<"\n Printing the array elements \n"; cout <<endl; for (i = 0; i<5; i++) cout << "A[" << i << "]= " << A[i] << "\n"; cout <<endl; return 0; Program Output Input the values of the array elements A[0]= 1 A[1]= 2 A[2]= 3 A[3]= 9 A[4]= 3 Printing the array elements A[0]= 1 A[1]= 2 A[2]= 3 A[3]= 9 A[4]= 3 Press any key to continue

Practice Exercises #5 Chapter 5 Arrays Page 26 Analysis: Line 6 declares an array called A, which holds five integer variables. Line 11 establishes a loop that counts from 0 through 4, which is the proper set of offsets for a five-element array. The user is prompted for a value, and that value is saved at the correct offset into the array. The first value is saved at A[0], the second at A[1], and so forth. The second for loop (line 19) prints each value to the screen. Questions 1. Fill in the following table with the values of the variable sum at the indicated iterations. Program i sum #include <iostream> int main() int myarray[5]=-1, 2, 9, -3, 10 ; int i; int sum=0; for (i = 0; i<5; i++) sum=sum+ myarray[i]; cout << sum << "\n"; return(0); 0 1 2 3 4

Practice Exercises #5 Chapter 5 Arrays Page 27 2. Fill in the following table to fix the values of of "i" and "sum" at lines 1 and 2. #include <iostream> int main() int sum=0; int i=0; int myarray[5]=-1, 2, 9, -3, 10 ; 1. sum=sum+i; for (i = 0; i<5; i++) sum=sum+ myarray[i]; 2. cout << i<<" "<<sum << "\n"; return(0); Line Number Value of i Value of sum 1 2 3. How many times the following loop will be executed and what is the value of the output sum? Program #include <iostream> int main() int a[3]=-1, 2, 3 ; int i; int sum=0; for (i = 0; i<3; i++) sum=sum+a[i]; cout << sum << "\n"; return 0; Maximum number of iterations: Value of Sum at the end of program execution:

Practice Exercises #5 Chapter 5 Arrays Page 28 4. The following programs contain errors. Put the symbol "X" in the sentence containing errors, then rewrite this sentence correctly. Program with errors X Corrected sentences #include <iostream> Int main() Int a[5]=-1, 2, 3 ; int sum=0; while (i <7) sum=sum+a (i); cout << sum << "/n"; return(0); Program with errors X Corrected Sentences #include <iostream> int main() int a[3][2]=-1, 2, 3,1,0,2,2,5 ; int i; int sum=0; for (I = 0; i--) sum=sum+a[i]; cout << sum << "\n"; return 0