The following list represents the written codes in order:

Size: px
Start display at page:

Download "The following list represents the written codes in order:"

Transcription

1 The following list represents the written codes in order: 1

2 #include<iostream> #include<cmath> void main() double x; double y; char ch; while (true) cout << "Enter a value of x: "; cin >> x; cout << "round(" << x << ") = " << round(x) << endl; cout << "ceil( " << x << ") = " << ceil(x) << endl; cout << "floor(" << x << ") = " << floor(x) << endl; cout << "fabs (" << x << ") = " << fabs(x) << endl; cout << "sqrt(" << x << ") = " << sqrt(x) << endl; //square root cout << "Enter a value of y: "; cin >> y; cout << "pow(" << x << "," << y << ") = " << pow(x,y) << endl; //power y = sqrt(pow(x, 3) + 2); // equation cout << y << endl; cout << "Exit (y/n):"; cin >> ch; ch = (char)tolower(ch); if (ch == 'y') 2

3 #include<iostream> #include<algorithm> void main() double x, y; cout << "Enter 2 numbers: "; cin >> x >> y; cout << "max(" << x << "," << y << ") = " << max(x, y) << endl; cout << "min(" << x << "," << y << ") = " << min(x, y) << endl; 3

4 #include<iostream> #include<iomanip> void main() char letter = 'A'; char nch = 97; cout << setw(15) << "letter = " << letter << endl; cout << setw(15) << "next letter = " << ++letter << endl; //Every character has a unique ASCII code between 0 & 127 cout << "ASCII of " << letter << " is " << (int)letter << endl; cout << "ASCII of " << letter << " is " << static_cast<int>(letter) << endl; cout << "nch " << nch << endl; int i = '2' + '3' + 'A' + 4; cout << "\'2\' + \'3\' + \'A\' + 4 = " << i << endl; cout << "Enter a Character : "; cin >> letter; if (isdigit(letter)) cout << "It is a digit." << endl; if (isspace(letter)) cout << "It is a space." << endl; if (isalpha(letter)) cout << "It is an alpha." << endl; if (islower (letter)) cout << "It is a lowercase letter, the uppercase is " << (char)toupper(letter) << endl; if (isupper(letter)) cout << "It is an uppercase letter, the lowercase is " << static_cast<char>(tolower(letter)) << endl; 4

5 #include<iostream> #include<string> void main() string s1 = "Introdution "; string s2 = "to Programming in C++"; cout << "S1: " << s1 << endl; cout << "Length of S1 is " << s1.length() << endl << endl; cout << "S2: " << s2 << endl; cout << "Length of S2 is " << s2.length() << endl << endl; string s3 = s1 + s2; int L = s3.length(); cout << "S3 (Concatenating s1 and s2) : " << s3 << endl; cout << "Length of S3 is " << L << endl << endl; string str = "ABCDE"; str[0] = 'M'; cout << "str = " << str << endl; char c = str[3]; cout << "str[3] " << c << endl; c = str.at(4); cout << "str.at(4) " << c << endl; cout << endl; string input; cout << "Enter a string: "; cin >> input >> skipws; cout << "The entered string using cin is " << input << endl; cin.ignore(int_max, '\n'); cout << "Enter a new string: "; getline(cin, input); cout << "The entered string using getline is " << input << endl; string name = "Mohamed Bassam"; for (int i = 0; i < name.length(); i++) name[i] = toupper(name[i]); cout << "To Uppercase string: " << name << endl; if (input == name) cout << "This is my name." << endl; else cout << "This is not my name." << endl; if (input.compare(name)) cout << "This is my name." << endl; else cout << "This is not my name." << endl; 5

6 #include<iostream> #include<cstdlib> #include<ctime> void main() int i = rand(); cout << "Random number: " << i << endl; cout << "\ngenerate a random number between 0 and 5 " << rand() % 6; cout << "\ngenerate a random number between 10 and 50 " << 10 + rand() % 41; while (true) i = rand(); cout << "Random number: " << i << endl; endl; << endl; cout << "Generate a random number between 0 and 5 " << rand() % 6 << cout << "Generate a random number between 10 and 50 " << 10 + rand() % 41 _sleep(1000); if (rand() % 16 == 0) while (true) int seed; cout << "Enter seed: "; cin >> seed; srand(seed); i = rand(); cout << "Random number after srand(seed): " << i << endl; if (seed == 0) while (true) srand(time(0)); i = rand(); cout << "Random number srand(time(0)): " << i << endl; _sleep(2000); cout << endl; 6

7 #include<iostream> int sum(int, int); // function prototype void main() //without using function! cout << "without using function!\n"; int s = 0; cout << "The sum of integers from 1 to 10 is "; for (int i = 1; i <= 10; i++) s += i; cout << s << endl; s = 0; cout << "The sum of integers from 10 to 100 is "; for (int i = 10; i <= 100; i++) s += i; cout << s << endl; s = 0; cout << "The sum of integers from 33 to 99 is "; for (int i = 33; i <= 99; i++) s += i; cout << s << endl; //define and invoke functions cout << "Using Functions\n"; cout << "The sum of integers from 1 to 10 is " << sum(1, 10) << endl; cout << "The sum of integers from 10 to 100 is " << sum(10, 100) << endl; cout << "The sum of integers from 33 to 99 is " << sum(33, 99) << endl; cout << endl; int sum(int x, int y) int result = 0; for (int i = x; i <= y; i++) result += i; return result; 7

8 // Return the max between two numbers int max(int num1, int num2) int result; if (num1 > num2) result = num1; else result = num2; return result; int i = 5; int j = 2; int k = max(i, j); cout << "The maximum between " << i << " and " << j << " is " << k << endl; 8

9 // Print grade for the score void printgrade(double score) if (score >= 90.0) cout << 'A' << endl; else if (score >= 80.0) cout << 'B' << endl; else if (score >= 70.0) cout << 'C' << endl; else if (score >= 60.0) cout << 'D' << endl; else cout << 'F' << endl; cout << "Enter a score: "; double score; cin >> score; cout << "The grade is "; printgrade(score); 9

10 // Return the grade for the score char getgrade(double score) if (score >= 90.0) return 'A'; else if (score >= 80.0) return 'B'; else if (score >= 70.0) return 'C'; else if (score >= 60.0) return 'D'; else return 'F'; cout << "Enter a score: "; double score; cin >> score; cout << "The grade is "; cout << getgrade(score) << endl; 10

11 #include <iomanip> // Check whether number is prime bool isprime(int number) for (int divisor = 2; divisor <= number/2 ; divisor++) if (number % divisor == 0) // If true, number is not prime return false; // number is not a prime return true; // number is prime void printprimenumbers(int numberofprimes) const int NUMBER_OF_PRIMES_PER_LINE = 10; // Display 10 per line int count = 0; // Count the number of prime numbers int number = 2; // A number to be tested for primeness // Repeatedly find prime numbers while (count < numberofprimes) // Print the prime number and increase the count if (isprime(number)) count++; // Increase the count if (count % NUMBER_OF_PRIMES_PER_LINE == 0) // Print the number and advance to the new line cout << setw(4) << number << endl; else cout << setw(4) << number; // Check if the next number is prime number++; cout << "The first 50 prime numbers are \n"; printprimenumbers(50); 11

12 // Return the max between two int values int max(int num1, int num2) if (num1 > num2) return num1; else return num2; // Find the max between two double values double max(double num1, double num2) if (num1 > num2) return num1; else return num2; // Return the max among three double values double max(double num1, double num2, double num3) return max(max(num1, num2), num3); // Invoke the max function with int parameters cout << "The maximum between 3 and 4 is " << max(3, 4) << endl; // Invoke the max function with the double parameters cout << "The maximum between 3.0 and 5.4 is " << max(3.0, 5.4) << endl; // Invoke the max function with three double parameters cout << "The maximum between 3.0, 5.4, and is " << max(3.0, 5.4, 10.14) << endl; 12

13 // Function prototype int max(int num1, int num2); double max(double num1, double num2); double max(double num1, double num2, double num3); // Invoke the max function with int parameters cout << "The maximum between 3 and 4 is " << max(3, 4) << endl; // Invoke the max function with the double parameters cout << "The maximum between 3.0 and 5.4 is " << max(3.0, 5.4) << endl; endl; // Invoke the max function with three double parameters cout << "The maximum between 3.0, 5.4, and is " << max(3.0, 5.4, 10.14) << // Return the max between two int values int max(int num1, int num2) if (num1 > num2) return num1; else return num2; // Find the max between two double values double max(double num1, double num2) if (num1 > num2) return num1; else return num2; // Return the max among three double values double max(double num1, double num2, double num3) return max(max(num1, num2), num3); 13

14 void printarea(double = 1); printarea(); printarea(4); // Display area of a circle void printarea(double radius) double area = radius * radius * ; cout << "area is " << area << endl; 14

15 // Display area of a circle void printarea(double radius = 1) double area = radius * radius * ; cout << "area is " << area << endl; printarea(); printarea(4); 15

16 inline void f(int month, int year) cout << "month is " << month << endl; cout << "year is " << year << endl; int month = 10, year = 2008; f(month, year); // Invoke inline function f(9, 2010); // Invoke inline function 16

17 void t1(); // Function prototype void t2(); // Function prototype t1(); t2(); int y; // Global variable, default to 0 void t1() int x = 1; cout << "x is " << x << endl; cout << "y is " << y << endl; x++; y++; void t2() int x = 1; cout << "x is " << x << endl; cout << "y is " << y << endl; 17

18 int v1 = 10; int v1 = 5; cout << "local variable v1 is " << v1 << endl; cout << "global variable v1 is " << ::v1 << endl; 18

19 void t1(); // Function prototype t1(); t1(); void t1() static int x = 1; int y = 1; x++; y++; cout << "x is " << x << endl; cout << "y is " << y << endl; 19

20 void increment(int n) n++; cout << "\tn inside the function is " << n << endl; int x = 1; cout << "Before the call, x is " << x << endl; increment(x); cout << "after the call, x is " << x << endl; 20

21 void increment(int& n) n++; cout << "n inside the function is " << n << endl; int x = 1; cout << "Before the call, x is " << x << endl; increment(x); cout << "After the call, x is " << x << endl; 21

22 int count = 1; int& r = count; cout << "count is " << count << endl; cout << "r is " << r << endl; r++; cout << "count is " << count << endl; cout << "r is " << r << endl; count = 10; cout << "count is " << count << endl; cout << "r is " << r << endl; 22

23 // Attempt to swap two variables does not work! void swap(int n1, int n2) cout << "\tinside the swap function" << endl; cout << "\tbefore swapping n1 is " << n1 << " n2 is " << n2 << endl; // Swap n1 with n2 int temp = n1; n1 = n2; n2 = temp; cout << "\tafter swapping n1 is " << n1 << " n2 is " << n2 << endl; // Declare and initialize variables int num1 = 1; int num2 = 2; cout << "Before invoking the swap function, num1 is " << num1 << " and num2 is " << num2 << endl; // Invoke the swap function to attempt to swap two variables swap(num1, num2); cout << "After invoking the swap function, num1 is " << num1 << " and num2 is " << num2 << endl; 23

24 // Swap two variables void swap(int& n1, int& n2) cout << "\tinside the swap function" << endl; cout << "\tbefore swapping n1 is " << n1 << " n2 is " << n2 << endl; // Swap n1 with n2 int temp = n1; n1 = n2; n2 = temp; cout << "\tafter swapping n1 is " << n1 << " n2 is " << n2 << endl; // Declare and initialize variables int num1 = 1; int num2 = 2; cout << "Before invoking the swap function, num1 is " << num1 << " and num2 is " << num2 << endl; // Invoke the swap function to attempt to swap two variables swap(num1, num2); cout << "After invoking the swap function, num1 is " << num1 << " and num2 is " << num2 << endl; 24

25 #include <iomanip> // Function prototypes void printmonth(int year, int month); void printmonthtitle(int year, int month); void printmonthname(int month); void printmonthbody(int year, int month); int getstartday(int year, int month); int gettotalnumberofdays(int year, int month); int getnumberofdaysinmonth(int year, int month); bool isleapyear(int year); // Prompt the user to enter year cout << "Enter full year (e.g., 2001): "; int year; cin >> year; // Prompt the user to enter month cout << "Enter month in number between 1 and 12: "; int month; cin >> month; // Print calendar for the month of the year printmonth(year, month); // Print the calendar for a month in a year void printmonth(int year, int month) // Print the headings of the calendar printmonthtitle(year, month); // Print the body of the calendar printmonthbody(year, month); // Print the month title, e.g., May, 1999 void printmonthtitle(int year, int month) printmonthname(month); cout << " " << year << endl; cout << " " << endl; cout << " Sun Mon Tue Wed Thu Fri Sat" << endl; // Get the English name for the month void printmonthname(int month) switch (month) case 1: 25

26 cout << "January"; case 2: cout << "February"; case 3: cout << "March"; case 4: cout << "April"; case 5: cout << "May"; case 6: cout << "June"; case 7: cout << "July"; case 8: cout << "August"; case 9: cout << "September"; case 10: cout << "October"; case 11: cout << "November"; case 12: cout << "December"; // Print month body void printmonthbody(int year, int month) // Get start day of the week for the first date in the month int startday = getstartday(year, month); // Get number of days in the month int numberofdaysinmonth = getnumberofdaysinmonth(year, month); // Pad space before the first day of the month int i = 0; for (i = 0; i < startday; i++) cout << " "; for (i = 1; i <= numberofdaysinmonth; i++) cout << setw(4) << i; if ((i + startday) % 7 == 0) cout << endl; 26

27 // Get the start day of the first day in a month int getstartday(int year, int month) // Get total number of days since 1//1//1800 int startday1800 = 3; int totalnumberofdays = gettotalnumberofdays(year, month); // Return the start day return (totalnumberofdays + startday1800) % 7; // Get the total number of days since January 1, 1800 int gettotalnumberofdays(int year, int month) int total = 0; // Get the total days from 1800 to year - 1 for (int i = 1800; i < year; i++) if (isleapyear(i)) total = total + 366; else total = total + 365; // Add days from Jan to the month prior to the calendar month for (int i = 1; i < month; i++) total = total + getnumberofdaysinmonth(year, i); return total; // Get the number of days in a month int getnumberofdaysinmonth(int year, int month) if (month == 1 month == 3 month == 5 month == 7 month == 8 month == 10 month == 12) return 31; if (month == 4 month == 6 month == 9 month == 11) return 30; if (month == 2) return isleapyear(year)? 29 : 28; // If month is incorrect // Determine if it is a leap year bool isleapyear(int year) return year % 400 == 0 (year % 4 == 0 && year % 100!= 0); 27

Introduction to programming using Python

Introduction to programming using Python Introduction to programming using Python Matthieu Choplin matthieu.choplin@city.ac.uk http://moodle.city.ac.uk/ Session 5 1 Objectives To come back on the definition of functions To invoke value-returning

More information

Lab Instructor : Jean Lai

Lab 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 information

Elements of C in C++ data types if else statement for loops. random numbers rolling a die

Elements of C in C++ data types if else statement for loops. random numbers rolling a die Elements of C in C++ 1 Types and Control Statements data types if else statement for loops 2 Simulations random numbers rolling a die 3 Functions and Pointers defining a function call by value and call

More information

C++ Quick Reference. switch Statements

C++ Quick Reference. switch Statements C++ Quick Reference if Statements switch Statements Array/Initializer if (condition) if (condition) else if (condition1) else if (condition2) else Loop Statements while (condition) do while (condition);

More information

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

CPE 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 information

Benefits of Methods. Chapter 5 Methods

Benefits of Methods. Chapter 5 Methods Chapter 5 Methods Benefits of Methods Write a method once and reuse it anywhere. Information hiding: hide the implementation from the user Reduce complexity 1 4 Motivating Example Often we need to find

More information

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

UEE1302(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 information

Consider the following statements. string str1 = "ABCDEFGHIJKLM"; string str2; After the statement str2 = str1.substr(1,4); executes, the value of str2 is " ". Given the function prototype: float test(int,

More information

Tutorial 8 (Array I)

Tutorial 8 (Array I) Tutorial 8 (Array I) 1. Indicate true or false for the following statements. a. Every element in an array has the same type. b. The array size is fixed after it is created. c. The array size used to declare

More information

Do not turn to the next page until the start of the exam.

Do not turn to the next page until the start of the exam. Introduction to Programming, PIC10A E. Ryu Fall 2017 Midterm Exam Friday, November 3, 2017 50 minutes, 11 questions, 100 points, 8 pages While we don t expect you will need more space than provided, you

More information

Using Free Functions

Using Free Functions Chapter 3 Using Free Functions 3rd Edition Computing Fundamentals with C++ Rick Mercer Franklin, Beedle & Associates Goals Evaluate some mathematical and trigonometric functions Use arguments in function

More information

Chapter 6 - Notes User-Defined Functions I

Chapter 6 - Notes User-Defined Functions I Chapter 6 - Notes User-Defined Functions I I. Standard (Predefined) Functions A. A sub-program that performs a special or specific task and is made available by pre-written libraries in header files. B.

More information

CPE Summer 2015 Exam I (150 pts) June 18, 2015

CPE Summer 2015 Exam I (150 pts) June 18, 2015 Name Closed notes and book. If you have any questions ask them. Write clearly and make sure the case of a letter is clear (where applicable) since C++ is case sensitive. You can assume that there is one

More information

UNIVERSITY OF SWAZILAND SUPPLEMENTARY EXAMINATION, JULY 2013

UNIVERSITY OF SWAZILAND SUPPLEMENTARY EXAMINATION, JULY 2013 UNIVERSITY OF SWAZILAND SUPPLEMENTARY EXAMINATION, JULY 2013 Title of Paper : STRUCTURED PROGRAMMING - I Course number: CS243 Time allowed Instructions : Three (3) hours. : (1) Read all the questions in

More information

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

6-1 (Function). (Function) !*+!#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x (Function) -1.1 Math Library Function!"#! $%&!'(#) preprocessor directive #include !*+!"#!, Function Description Example sqrt(x) square root of x sqrt(900.0) is 30.0 sqrt(9.0) is 3.0 exp(x) log(x)

More information

SOLUTIONS TO EXERCISES PART ONE: Problem-Solving Techniques

SOLUTIONS 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 information

Engineering Problem Solving with C++, Etter/Ingber

Engineering Problem Solving with C++, Etter/Ingber Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs C++, Second Edition, J. Ingber 1 Simple C++ Programs Program Structure Constants and Variables C++ Operators Standard Input

More information

Functions and Recursion

Functions and Recursion Functions and Recursion 1 Outline Introduction Program Components in C++ Math Library Functions Functions Function Definitions Function Prototypes Header Files Random Number Generation Example: A Game

More information

Outline. Functions. Functions. Predefined Functions. Example. Example. Predefined functions User-defined functions Actual parameters Formal parameters

Outline. Functions. Functions. Predefined Functions. Example. Example. Predefined functions User-defined functions Actual parameters Formal parameters Outline Functions Predefined functions User-defined functions Actual parameters Formal parameters Value parameters Variable parameters Functions 1 Functions 2 Functions Predefined Functions In C++ there

More information

A. Introduction to Function 1. Modular Programming input processing output functions library functions 2. Function 1. Benefit of Using Functions

A. Introduction to Function 1. Modular Programming input processing output functions library functions 2. Function 1. Benefit of Using Functions Lesson Outcomes At the end of this chapter, student should be able to: Use pre-defined functions: (sqrt(), abs(), pow(), toupper(), tolower(), strcmp(), strcpy(), gets()) Build independent functions or

More information

EECS 183, Week 5. General. Variables I/O. 0. At which location do you have to take the exam? 1. Source code vs. object code? 2. What s a library?

EECS 183, Week 5. General. Variables I/O. 0. At which location do you have to take the exam? 1. Source code vs. object code? 2. What s a library? EECS 183, Week 5 General 0. At which location do you have to take the exam? 1. Source code vs. object code? 2. What s a library? Variables 3. Name main data types in C++. 4. Is string a native data type

More information

Arrays. Arrays (8.1) Arrays. One variable that can store a group of values of the same type. Storing a number of related values.

Arrays. Arrays (8.1) Arrays. One variable that can store a group of values of the same type. Storing a number of related values. Arrays Chapter 8 page 471 Arrays (8.1) One variable that can store a group of values of the same type Storing a number of related values o all grades for one student o all temperatures for one month o

More information

Chapter 3 - Functions

Chapter 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 information

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type. CS 7A - Fall 2016 - Midterm 1 10/20/16 Write responses to questions 1 and 2 on this paper or attach additional sheets, as necessary For all subsequent problems, use separate paper Do not use a computer

More information

Arrays. What if you have a 1000 line file? Arrays

Arrays. What if you have a 1000 line file? Arrays Arrays Chapter 8 page 477 11/8/06 CS150 Introduction to Computer Science 1 1 What if you have a 1000 line file? Read in the following file and print out a population graph as shown below. The maximum value

More information

AIMMS Function Reference - Date Time Related Identifiers

AIMMS Function Reference - Date Time Related Identifiers AIMMS Function Reference - Date Time Related Identifiers This file contains only one chapter of the book. For a free download of the complete book in pdf format, please visit www.aimms.com Aimms 3.13 Date-Time

More information

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

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski) Class 2: Variables and Memory Variables A variable is a value that is stored in memory It can be numeric or a character C++ needs to be told what type it is before it can store it in memory It also needs

More information

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1 300580 Programming Fundamentals 3 With C++ Variable Declaration, Evaluation and Assignment 1 Today s Topics Variable declaration Assignment to variables Typecasting Counting Mathematical functions Keyboard

More information

Honors Computer Science C++ Mr. Clausen Program 3A, 3B, 3C

Honors Computer Science C++ Mr. Clausen Program 3A, 3B, 3C Honors Computer Science C++ Mr. Clausen Program 3A, 3B, 3C Program 3A Cone Heads (25 points) Write a program to calculate the volume and surface area of a right circular cone. Allow the user to enter values

More information

The 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 &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 information

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

Multiple 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 information

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces Basic memory model Using functions Writing functions Basics Prototypes Parameters Return types Functions and memory Names and namespaces When a program runs it requires main memory (RAM) space for Program

More information

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

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

More information

Study Guide for Test 2

Study Guide for Test 2 Study Guide for Test 2 Topics: decisions, loops, arrays, c-strings, linux Material Selected from: Chapters 4, 5, 6, 7, 10.1, 10.2, 10.3, 10.4 Examples 14 33 Assignments 4 8 Any syntax errors are unintentional

More information

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

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following: Computer Department Program: Computer Midterm Exam Date : 19/11/2016 Major: Information & communication technology 1 st Semester Time : 1 hr (10:00 11:00) Course: Introduction to Programming 2016/2017

More information

Name Section: M/W T/TH Number Definition Matching (6 Points)

Name Section: M/W T/TH Number Definition Matching (6 Points) Name Section: M/W T/TH Number Definition Matching (6 Points) 1. (6 pts) Match the words with their definitions. Choose the best definition for each word. Event Counter Iteration Counter Loop Flow of Control

More information

Introduction. What is function? Multiple functions form a larger program Modular programming

Introduction. What is function? Multiple functions form a larger program Modular programming FUNCTION CSC128 Introduction What is function? Module/mini program/sub-program Each function/module/sub-program performs specific task May contains its own variables/statements Can be compiled/tested independently

More information

Chapter 4 - Notes Control Structures I (Selection)

Chapter 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 information

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

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

More information

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No. The American University in Cairo Computer Science & Engineering Department CSCE 106 Instructor: Final Exam Fall 2010 Last Name :... ID:... First Name:... Section No.: EXAMINATION INSTRUCTIONS * Do not

More information

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4 More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4 Ziad Matni Dept. of Computer Science, UCSB Administrative CHANGED T.A. OFFICE/OPEN LAB HOURS! Thursday, 10 AM 12 PM

More information

CS31 Discussion 1E. Jie(Jay) Wang Week5 Oct.27

CS31 Discussion 1E. Jie(Jay) Wang Week5 Oct.27 CS31 Discussion 1E Jie(Jay) Wang Week5 Oct.27 Outline Project 3 Debugging Array Project 3 Read the spec and FAQ carefully. Test your code on SEASnet Linux server using the command curl -s -L http://cs.ucla.edu/classes/fall16/cs31/utilities/p3tester

More information

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

Multiple 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 information

Programming Language. Functions. Eng. Anis Nazer First Semester

Programming 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 information

3.1. Chapter 3: The cin Object. Expressions and Interactivity

3.1. Chapter 3: The cin Object. Expressions and Interactivity Chapter 3: Expressions and Interactivity 3.1 The cin Object Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-1 The cin Object Standard input stream object, normally the keyboard,

More information

CSCE Practice Midterm. Data Types

CSCE 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 information

CSci 1113, Fall 2015 Lab Exercise 4 (Week 5): Write Your Own Functions. User Defined Functions

CSci 1113, Fall 2015 Lab Exercise 4 (Week 5): Write Your Own Functions. User Defined Functions CSci 1113, Fall 2015 Lab Exercise 4 (Week 5): Write Your Own Functions User Defined Functions In previous labs, you've encountered useful functions, such as sqrt() and pow(), that were created by other

More information

Scheduling. Scheduling Tasks At Creation Time CHAPTER

Scheduling. Scheduling Tasks At Creation Time CHAPTER CHAPTER 13 This chapter explains the scheduling choices available when creating tasks and when scheduling tasks that have already been created. Tasks At Creation Time The tasks that have the scheduling

More information

Example. Section: PS 709 Examples of Calculations of Reduced Hours of Work Last Revised: February 2017 Last Reviewed: February 2017 Next Review:

Example. Section: PS 709 Examples of Calculations of Reduced Hours of Work Last Revised: February 2017 Last Reviewed: February 2017 Next Review: Following are three examples of calculations for MCP employees (undefined hours of work) and three examples for MCP office employees. Examples use the data from the table below. For your calculations use

More information

CS115 Principles of Computer Science

CS115 Principles of Computer Science CS115 Principles of Computer Science Chapter 5 Methods Prof. Joe X. Zhou Department of Computer Science CS115 Methods.1 Re: Objectives in Loops Sequence and selection aside, we need repetition (loops)

More information

1 Pointer Concepts. 1.1 Pointer Examples

1 Pointer Concepts. 1.1 Pointer Examples 1 1 Pointer Concepts What are pointers? How are they used? Point to a memory location. Call by reference is based on pointers. Operators: & Address operator * Dereferencing operator Machine/compiler dependencies

More information

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition) Structures Programming in C++ Sequential Branching Repeating Loops (Repetition) 2 1 Loops Repetition is referred to the ability of repeating a statement or a set of statements as many times this is necessary.

More information

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

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

More information

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou ADMINISTRATIVE This week s lecture [BRON06] Chapter 6 (6.1) What is a function? Function declaration (prototype) Function definition

More information

Downloaded from

Downloaded from CLASS 11 COMPUTER SCIENCE (83) PRACTICAL LIST 1. Write a C++ Program to find area & circumference of circle. 2. Write a C++ Program to display ASCII character & vice versa. 3. Write a C++ Program to find

More information

Chapter 6 Methods. Dr. Hikmat Jaber

Chapter 6 Methods. Dr. Hikmat Jaber Chapter 6 Methods Dr. Hikmat Jaber 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 Problem int sum = 0; for (int i = 1; i

More information

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. Chapter 6 Methods 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 A Solution int sum = 0; for (int i = 1; i

More information

Computer Science 217

Computer Science 217 Computer Science 217 Midterm Exam October 29, 2014 First Name: Last Name: ID: Class Time (Circle One): 1:00pm 3:00pm Instructions: Neatly print your names and ID number in the spaces provided above. Pick

More information

CMSC 202 Midterm Exam 1 Fall 2015

CMSC 202 Midterm Exam 1 Fall 2015 1. (15 points) There are six logic or syntax errors in the following program; find five of them. Circle each of the five errors you find and write the line number and correction in the space provided below.

More information

A SHORT COURSE ON C++

A 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 information

Chapter 2. Outline. Simple C++ Programs

Chapter 2. Outline. Simple C++ Programs Chapter 2 Simple C++ Programs Outline Objectives 1. Building C++ Solutions with IDEs: Dev-cpp, Xcode 2. C++ Program Structure 3. Constant and Variables 4. C++ Operators 5. Standard Input and Output 6.

More information

Computer Programming, I. Laboratory Manual. Experiment #7. Methods

Computer Programming, I. Laboratory Manual. Experiment #7. Methods Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #7

More information

Sequential Search (Searching Supplement: 1-2)

Sequential Search (Searching Supplement: 1-2) (Searching Supplement: 1-2) A sequential search simply involves looking at each item in an array in turn until either the value being searched for is found or it can be determined that the value is not

More information

String Class in C++ When the above code is compiled and executed, it produces result something as follows: cin and strings

String Class in C++ When the above code is compiled and executed, it produces result something as follows: cin and strings String Class in C++ The standard C++ library provides a string class type that supports all the operations mentioned above, additionally much more functionality. We will study this class in C++ Standard

More information

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

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake Assigning Values // Example 2.3(Mathematical operations in C++) float a; cout > a; cout

More information

CMPS 221 Sample Final

CMPS 221 Sample Final Name: 1 CMPS 221 Sample Final 1. What is the purpose of having the parameter const int a[] as opposed to int a[] in a function declaration and definition? 2. What is the difference between cin.getline(str,

More information

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

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

More information

Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects

Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects Function Input and Output Input through actual parameters Output through return value Only one value can be returned Input/Output through side effects printf scanf 2 tj Function Input and Output int main(void){

More information

C++ Final Exam 2017/2018

C++ Final Exam 2017/2018 1) All of the following are examples of integral data types EXCEPT. o A Double o B Char o C Short o D Int 2) After the execution of the following code, what will be the value of numb if the input value

More information

University of Dublin

University of Dublin University of Dublin TRINITY COLLEGE Faculty of Enginering & Systems Sciences School of Engineering Junior Freshman Engineering Trinity Term 2014 Computer Engineering I (1E3) Date Location Time Dr L. Hederman

More information

Chapter void Test( int, int, int ); // Function prototype int main() // Function heading { int h; // Local variable

Chapter void Test( int, int, int ); // Function prototype int main() // Function heading { int h; // Local variable EXERCISE ANSWERS Chapter 7 Exam Preparation Exercises 1 Function call The mechanism that transfers control to the body of a function Argument list A mechanism by which functions communicate with each other;

More information

Name Section: M/W T/TH Number Definition Matching (8 Points)

Name Section: M/W T/TH Number Definition Matching (8 Points) Name Section: M/W T/TH Number Definition Matching (8 Points) 1. (8 pts) Match the words with their definitions. Choose the best definition for each word. Iteration Counter Event Counter Loop Abstract Step

More information

Question 2. [5 points] Given the following symbolic constant definition

Question 2. [5 points] Given the following symbolic constant definition CS 101, Spring 2012 Mar 20th Exam 2 Name: Question 1. [5 points] Determine which of the following function calls are valid for a function with the prototype: void drawrect(int width, int height); Assume

More information

Summer II Midterm Part I - Algorithms - 3 points each

Summer II Midterm Part I - Algorithms - 3 points each 1 Summer II Midterm Part I - Algorithms - 3 points each a) RemoveNLastDigits returns the number passed in minus digitstoremove digitstoremove must be positive RemoveNLastDigits(123456,2) ==> 1234 RemoveNLastDigits(123456,5)

More information

2.1-First.cpp #include <iostream> // contains information to support input / output using namespace std;

2.1-First.cpp #include <iostream> // contains information to support input / output using namespace std; Lafore: Object-Oriented Programming in C++ (4 th ) 2.1-First.cpp // contains information to support input / output // entry point for every C and C++ program (console) cout

More information

Chapter 5 Methods. Modifier returnvaluetype methodname(list of parameters) { // method body; }

Chapter 5 Methods. Modifier returnvaluetype methodname(list of parameters) { // method body; } Chapter 5 Methods 5.1 Introduction A method is a collection of statements that are grouped together to perform an operation. You will learn how to: o create your own mthods with or without return values,

More information

Page 1 Name: CUNYfirstID: CS111 Summer Term 1- Final 6/27/18

Page 1 Name: CUNYfirstID: CS111 Summer Term 1- Final 6/27/18 Page 1 PART I Basic Questions A,B,C are required to be correctly answered to obtain a grade higher than C-. Part 1 Questions are worth 10 points each. A) // NumbersGreaterThan - loop through an array and

More information

CISC 1110 (CIS 1.5) Introduc2on to Programming Using C++

CISC 1110 (CIS 1.5) Introduc2on to Programming Using C++ CISC 1110 (CIS 1.5) Introduc2on to Programming Using C++ Spring 2012 Instructor : K. Auyeung Email Address: Course Page: Class Hours: kenny@sci.brooklyn.cuny.edu hbp://www.sci.brooklyn.cuny.edu/~kenny/cisc1110

More information

Function I/O. Last Updated 10/30/18

Function I/O. Last Updated 10/30/18 Last Updated 10/30/18 Program Structure Includes Function Declarations void main(void){ foo = fun1(a, b); fun2(2, c); if(fun1(c, d)) { } } Function 1 Definition Function 2 Definition 2 tj Function Input

More information

Notes on the 2008 Exam

Notes on the 2008 Exam Notes on the 2008 Exam A hastily compiled review of this year s exam. Apologies if there are errors. Please also read notes on previous years exams especially the first set of notes. Question 1 Question

More information

Summary of basic C++-commands

Summary 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 information

Definition Matching (10 Points)

Definition Matching (10 Points) Name SOLUTION Closed notes and book. If you have any questions ask them. Write clearly and make sure the case of a letter is clear (where applicable) since C++ is case sensitive. There are no syntax errors

More information

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II Superior University Department of Electrical Engineering CS-115 Computing Fundamentals Experiment No.7 User Defined Functions II Prepared for By: Name: ID: Section: Semester: Total Marks: Obtained Marks:

More information

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science. Instructor: Final Exam Fall 2011

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science. Instructor: Final Exam Fall 2011 The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science Instructor: Final Exam Fall 2011 Last Name :... ID:... First Name:... Section No.: EXAMINATION

More information

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. Chapter 6 Methods 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 A Solution int sum = 0; for (int i = 1; i

More information

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam. Name: Print legibly! Section: COMPUTER SCIENCE 261 PROGRAMMING CONCEPTS EXAM 1 VERSION 1 Fall 2014 150 Points Absolutely no electronic devices may be used during this exam. 1. No cell phones, computers,

More information

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved Chapter Four: Loops Slides by Evan Gallagher The Three Loops in C++ C++ has these three looping statements: while for do The while Loop while (condition) { statements } The condition is some kind of test

More information

Programming Logic and Design Sixth Edition

Programming Logic and Design Sixth Edition Objectives Programming Logic and Design Sixth Edition Chapter 6 Arrays In this chapter, you will learn about: Arrays and how they occupy computer memory Manipulating an array to replace nested decisions

More information

CSCE 2004 Midterm Exam Spring 2017

CSCE 2004 Midterm Exam Spring 2017 CSCE 2004 Midterm Exam Spring 2017 Student Name: Student UAID: Instructions: This is a 50 minute exam. Students are allowed one 8.5 by 11 page of study notes. Calculators, cell phones and computers are

More information

ENGI 1020 Introduction to Computer Programming J U L Y 5, R E Z A S H A H I D I

ENGI 1020 Introduction to Computer Programming J U L Y 5, R E Z A S H A H I D I ENGI 1020 Introduction to Computer Programming J U L Y 5, 2 0 1 0 R E Z A S H A H I D I Passing by value Recall that it is possible to call functions with variable names different than the parameters in

More information

AIR FORCE SCHOOL,BAMRAULI COMPUTER SCIENCE (083) CLASS XI Split up Syllabus (Session ) Contents

AIR FORCE SCHOOL,BAMRAULI COMPUTER SCIENCE (083) CLASS XI Split up Syllabus (Session ) Contents AIR FORCE SCHOOL,BAMRAULI COMPUTER SCIENCE (083) CLASS XI Split up Syllabus (Session- 2017-18) Month July Contents UNIT 1: COMPUTER FUNDAMENTALS Evolution of computers; Basics of computer and its operation;

More information

Outline. Why do we write functions? Introduction to Functions. How do we write functions? Using Functions. Introduction to Functions March 21, 2006

Outline. Why do we write functions? Introduction to Functions. How do we write functions? Using Functions. Introduction to Functions March 21, 2006 Introduction to User-defined Functions Larry Caretto Computer Science 106 Computing in Engineering and Science March 21, 2006 Outline Why we use functions Writing and calling a function Header and body

More information

Downloaded from

Downloaded from Function: A function is a named unit of a group of statements that can be invoked from other parts of the program. The advantages of using functions are: Functions enable us to break a program down into

More information

Lecture #6-7 Methods

Lecture #6-7 Methods Lecture #6-7 s 1. a. group of statements designed to perform a specific function b. may be reused many times i. in a particular program or ii. in multiple programs 2. Examples from the Java Library a.

More information

CS242 COMPUTER PROGRAMMING

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

More information

CSCI 111 First Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, October 18, 2017

CSCI 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 information

Companion C++ Examples

Companion C++ Examples 68 Appendix D Companion C++ Examples D.1 Introduction It is necessary to be multilingual in computer languages today. Since C++ is often used in the OOP literature it should be useful to have C++ versions

More information

BITG 1233: Introduction to C++

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

More information

I 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 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 information

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Computer Programming, I. Laboratory Manual. Experiment #3. Selections Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #3

More information