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