Lab 1. largest = num1; // assume first number is largest

Similar documents
CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

CS242 COMPUTER PROGRAMMING

Objectives. In this chapter, you will:

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

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

CS 216 Exam 1 Fall SOLUTION

VARIABLE, OPERATOR AND EXPRESSION [SET 1]

WARM UP LESSONS BARE BASICS

B. V. Patel Institute of Business Management, Computer & Information Technology, UTU 2013

Chapter 3 - Functions

Homework #3 CS2255 Fall 2012

Lab ACN : C++ Programming Exercises

Functions and Recursion

Chapter 6 - Notes User-Defined Functions I

C Functions. Object created and destroyed within its block auto: default for local variables

by Pearson Education, Inc. All Rights Reserved. 2

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

C++ Programming Lecture 11 Functions Part I

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

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

CSCI 111 Midterm 1, version A Exam Fall Solutions 09.00am 09.50am, Tuesday, October 13, 2015

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

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

Chapter 2: Basic Elements of C++

PROGRAMMING IN C AND C++:

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

Subject: Computer Science

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

Exercise 1.1 Hello world

Memory and Pointers written by Cathy Saxton

CS2255 HOMEWORK #1 Fall 2012

1.1 Introduction. 1.2 Model

download instant at Introduction to C++

Introduction to C++ Introduction to C++ 1

Lab01: C++ Expressions ES036a: Programming Fundamentals Fall 2007

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

Chapter 3 Problem Solving and the Computer

The Ultimate Maths Vocabulary List

[Page 177 (continued)] a. if ( age >= 65 ); cout << "Age is greater than or equal to 65" << endl; else cout << "Age is less than 65 << endl";

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

LAB: INTRODUCTION TO FUNCTIONS IN C++

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

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

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.

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

Computer Science II Lecture 1 Introduction and Background

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

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

Functions that Return a Value. Approximate completion time Pre-lab Reading Assignment 20 min. 92

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

FORM 2 (Please put your name and form # on the scantron!!!!)

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Numbers. John Perry. Spring 2017

The C++ Language. Arizona State University 1

3rd Grade Mathematics

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

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

11.3 Function Prototypes

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

Arrays in C++ Instructor: Andy Abreu

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

Fundamentals of Programming CS-110. Lecture 2

Introduction to Programming

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Introduction to Java Unit 1. Using BlueJ to Write Programs

Help File on C++ and Programming at CSUSM by Rika Yoshii

Expressions and Casting

MML Contest #1 ROUND 1: VOLUME & SURFACES

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

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

Downloaded from

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

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

EE 109 Lab 8a Conversion Experience

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Number/Computation. addend Any number being added. digit Any one of the ten symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9

Section we will not cover section 2.11 feel free to read it on your own

COMP322 - Introduction to C++ Lecture 01 - Introduction

What we will learn about this week: Declaring and referencing arrays. arrays as function arguments. Arrays

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

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

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

UNIT- 3 Introduction to C++

Exam 3 Chapters 7 & 9

What Is a Function? Illustration of Program Flow

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

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

Introduction to Programming

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

Chapter 1 Introduction to Computers and C++ Programming

Lab 2.1: Fixing a C++ program

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Functions and Recursion

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

In this chapter you will learn:

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Transcription:

Lab 1 Experiment 1 Complete and execute the following program #include <iostream> using std::cout; using std::cin; using std::endl; int main() { int number1; int number2; int number3; int smallest; int largest; cout << "Input three different integers: "; // prompt /* Write a statement to read in values for number1, number2 and number3 using a single cin statement */ largest = num1; // assume first number is largest /* Write a statement to determine if number2 is greater than largest. If so assign number2 to largest */ /* Write a statement to determine if number3 is greater than largest. If so assign number3 to largest */ smallest = num1; // assume first number is smallest /* Write a statement to determine if number2 is less than smallest. If so assign number2 to smallest */ /* Write a statement to determine if number3 is less than smallest. If so assign number3 to smallest */ /* Write an output statement that prints the sum, average, product, largest and smallest */ return 0; } // end main

Experiment 2 Complete and execute the following program #include <iostream> using std::cout; using std::cin; using std::endl; int main() { /* Write variable declarations here */ cout << "Enter two integers: "; /* Write a cin statement to read data into variables here */ if ( /* Write a condition that tests if number1 is a multiple of number2 */ ) cout << number1 << " is a multiple of " << number2 << endl; if ( /* Write a condition that tests if num1 is not a multiple of num2 */ ) cout << number1 << " is not a multiple of " << number2 << endl; return 0; } // end main

Lab 2 Experiment 1 Write a program to compute the area and circumference of a rectangle 3 inche wide by 5 inches long. What changes must be made to the program so it works for a rectangle 6.8 inches wide by 2.3 inches long? Experiment 2 Write a program to print your name, and date of birth?

Lab 3 Experiment 1: Write the following data into files named test.txt : 12 13 9000 1000 Then write a program to read these data into the following variables and print them to the user: A B C D The output should look like : A= 12 B=13 C=9000 D=1000 Experiment 2: Write a program to compute the factorial of number 6? Experiment 3: Modify the previous program to compute the factorial for any number n?

Lab 4 Experiment 1: Write a function to find the difference between two times (hh:mm:ss) in minutes? Experiment 2: Write a recursive function to compute the factorial of number n?

Lab 5 Experiment 1: Write a program to sum up all the numbers in array? Experiment 2: Write a program to multiply two arrays?

Lab 6 Experiment 1: Design an airline reservation data structure that contains the following data: Flight number Originating airport code (3 characters) Destination airport code (3 characters) Departure time Arrival time Write a program that lists all the planes that leave from two airports specified by the user.

Lab 7 Write a class to implement a simple queue. A queue is very similar to a stack except the data is removed in first-in-first-out (FIFO) order. Member functions: void queue::put(int item); // Insert an item in the queue int queue::get(void); // Get the next item from the queue Sample usage: queue a_queue; a_queue.put(1); // Queue contains: 1 a_queue.put(2); // Queue contains: 1 2 a_queue.put(3); // Queue contains: 1 2 3 cout << a_queue.get() << '\n'; // Prints 1, queue contains 2 3 cout << a_queue.get() << '\n'; // Prints 2, queue contains 3

Lab 8 Define a class that will hold the set of integers from 0 to 31. An element can be set with the set member function and cleared with the clear member function. It is not an error to set an element that's already set or clear an element that's already clear. The function test is used to tell whether an element is set. Member functions: void small_set::set(int item); // Set an element in the set void small_set::clear(int item); // Clear an element in the set int small_set::test(void); // See whether an element is set Sample usage: small_set a_set; a_set.set(3); // Set contains [3] a_set.set(5); // Set contains [3,5] a_set.set(5); // Legal (set contains [3,5]) cout << a_set.test(3) << '\n'; // Prints "1" cout << a_set.test(0) << '\n'; // Prints "0" a_set.clear(5); // Set contains [3]

Lab 9 You need to supply some I/O routines for handling lines in a file. The basic definition of the linenumber class is: class line_number { public: void goto_line(int line); int get_current_line(void); } The member functions are defined as: void goto_line(int line); Positions the input file at specified line. int get_current_line(void); Returns the current line number (as set by goto_line). long int get_char_pos(void);

Lab 10 Q1: Write a program that uses pointers to set each element of an array to zero? Q2: Write a function that takes a single string as its argument and returns a pointer to the first nonwhite character in the string?

Tutorial Problems 1- On your computer, Write the hello program and execute it. 2- Take several programming examples from any source, enter them into the computer, and run them. 3- Write a program to print a block E using asterisks (*), where the E is 7 characters high and 5 characters wide 4- Write a program to print "HELLO" in big block letters where each letter is 7 characters high and 5 characters wide. 5- Write a program that converts Celsius to Fahrenheit. 6- Write a program to calculate the volume of a sphere, 7- Write a program to print out the perimeter of a rectangle given its height and width. perimeter = 2 (width+height) 8- Write a program that converts kilometers per hour to miles per hour. miles = (kilometers -0.6213712) 9- Write a program that takes hours and minutes as input and outputs the total number of minutes ( 1 hour 30 minutes = 90 minutes). 10- Write a program that takes an integer as the number of minutes and outputs the total hours and minutes (90 minutes = 1 hour 30 minutes).

11- Write a program to find the square of the distance between two points. Find the distance only if you want to do the independent research needed to perform a square root in C++. 12- A professor generates letter grades using the following table: Given a numeric grade, print the letter. 13- Modify the previous program to print out a + or - after the letter grade based on the last digit of the score. The modifiers are listed in the following table For example, 81=B-, 94=A, and 68=D+. Note: n F is only an F. There is no F+ or F-.

14-. A leap year is any year divisible by 4 unless it is divisible by 100, but not 400. Write a program to tell whether a year is a leap year. 15- Write a program that takes a series of numbers and counts the number of positive and negative values. 16- Write a program to average n numbers. 17- Write a program to print out the multiplication table. 18- Write a program that reads a character and prints out whether or not it is a vowel or a consonant. 19- Write a program that converts numbers to words. Example: 895 results in ''eight nine five." 20- The number 85 is said "eighty-five" not "eight five." Modify the previous program to handle the numbers 0-100 so all numbers come out as we really say them. Example: 13 "thirteen," 100 "one hundred." 21- Write a function that returns the maximum value of an array of numbers.

Simple Project Part I: 0/1 Knapsack Problem Transportation companies such as Egypt Air face daily problems in logistics. Consider the following simple logistics problem, which you will solve: An airline cargo company has 1 aeroplane which it flies from the Egypt to the US on a daily basis to transport some cargo. In advance of a flight, it receives bids for deliveries from (many) customers. Customers state the weight of the cargo item they would like delivered, and the amount they are prepared to pay. The airline is constrained by the total amount of weight the plane is allowed to carry. The company must choose a subset of the packages (bids) to carry in order to make the maximum possible profit, given the weight limit that they must respect. In mathematical form the problem is: Given a set of N items each with weight w i and value v i, for i=1 to N, choose a subset of items (e.g. to carry in a knapsack) so that the total value carried is maximized, and the total weight carried is less than or equal to a given carrying capacity, C. This kind of problem is known as a 0/1 Knapsack Problem. A Knapsack Problem is any problem that involves packing things into limited space or a limited weight capacity. The problem above is "0/1" because we either do carry an item: "1"; or we don't: "0". Other problems allow that we can take more than 1 or less than 1 (a fraction) of an item. An Enumeration Method for solving 0/1 Knapsack A straightforward method for solving any 0/1 Knapsack Problem is to try out all possible ways of packing/leaving out the items. For each way, it is easy to calculate the total weight carried and the total value carried. We can then choose the most valuable packing that is within the weight limit. For example, consider the following knapsack problem instance: 3 1 5 4 2 12 10 3 8 5 11

Where: The first line gives the number of items. The last line gives the capacity of the knapsack. The remaining lines give the index, value and weight of each item. (We will always use that way of representing knapsack problem instances) The full enumeration of possible packings would be as follows 0 Items Packed Value Weight Feasible? 000 0 0 001 8 5 010 12 10 011 20 15 *Not feasible* 100 5 4 101 13 9 *Optimal* 110 17 14 *Not feasible* 111 25 19 *Not feasible* The items packed column represents the packings as a binary string, where "1" in position i means pack item i, and 0 means do not pack it. Every combination of 0s and 1s has been tried. The one which is best is 101 (take items 1 and 3), which has weight 9 (so less than C=11) and value 13. Some vocabulary: Any binary string of length N is referred to as a packing or a solution. (This only means it is a correctly formatted instruction of what items to pack). If the solution also has weight less than the capacity C of the knapsack, then it is a feasible solution. If it is the best possible feasible solution (in terms of value) then it is an optimal solution. If it is only a high value solution, but not optimal, then it is an approximate solution. We will investigate some efficient ways of finding optimal solutions and approximate solutions. Deliverable : Implementation of the 0/1 Knapsack solution

Part II: Based on your experience in 0/1 knapsack problem, restrict your code to generate the 0/1 knapsack solution according to binary reflected gray code. Binary reflected gray code means generating bit string so that every one of them differs from its immediate predecessor by only a single bit. Example: 000 001 011 010 110 111 101 100 If you look at it this way, it might be helpful to generate an algorithm: 2-bit Gray code 00 01 11 10 3-bit Gray code 000 001 011 010 110 111 101 100 4-bit Gray code 0000 0001 0011 0010

0110 0111 0101 0100 1100 1101 1111 1110 1010 1011 1001 1000 Deliverable : Implementation of the solution