









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This is code assigned in lab of course. It was assigned by Jay Singh at Aligarh Muslim University for Data Structure course. It includes: Hello. World, Loop, Scope, Declare, Block, Compiler, Error, Declaration, Inner, Outer, Function
Typology: Exercises
1 / 15
This page cannot be seen from the preview
Don't miss anything!










A constant is an expressions with a fixed value. Kinds of constants:
1 2 3 4 5 6
1 const int pathwidth = 100; 2 // pathwidth = 2; this will cause a compiler error! 3 const char tabulator = ’\ t ’; 4 cout << " tabulator = " << tabulator << ’\ n ’;
lvalue is short for “left hand side value” (of an assignment). Examples of non-lvalue expressions:
Examples of lvalue expressions:
rvalue is short for “right hand side value” because rvalues can appear on the right hand side of an assignment. Anything with a well-defined value can be an rvalue, including an assigment: (x = 5) can be used as an rvalue whose value is 5, e.g. y = (x=5);.
a++ and ++a are shorthand for a = a + 1 with a big warning sign:
// this code outputs 0 to 9 for ( int i = 0; i < 10;) { cout << i ++ << " \ n " ; }
1 () [] - >. :: Grouping, scope, array/member access 2! ˜ * & sizeof (type cast) ++ – (most) unary operations, sizeof and typecasts 3 * / % Multiplication, division, modulo 4 + - Addition and subtraction 5 << >> Bitwise left and right shift 6 < <= > >= Comparisons: less than, etc. 7 == != Comparisons: equal and not equal 8 & Bitwise AND 9 ˆ Bitwise exclusive OR 10 | Bitwise inclusive (normal) OR 11 && Logical AND 12 || Logical OR 13 ?: Conditional expression (ternary operator) 14 = += -= *= /= %=, etc. Assignment operators 15 , Comma operator
An operator that takes three arguments and defines a conditional statement.
1 if ( a > b ) 2 result = x ; 3 else 4 result = y ;
is equivalent to
1 result = a > b? x : y ;
A type of selection control statement. Its purpose is to allow the value of a variable or expression to control the flow of program execution via multiple possible branches. Omitting break keywords to causes the program execution to “fall through” from one block to the next, a trick used extensively. In the example below, if n = 2, the fifth case statement (line
1 switch ( n ) {
2 case 0: 3 cout << " You typed zero .\ n " ; 4 break ; 5 case 1: 6 case 4: 7 case 9: 8 cout << " n is a perfect square .\ n " ; 9 break ; 10 case 2: 11 cout << " n is an even number .\ n " ; 12 case 3: 13 case 5: 14 case 7: 15 cout << " n is a prime number .\ n " ; 16 break ; 17 case 6: 18 case 8: 19 cout << " n is an even number .\ n " ; 20 break ; 21 default : 22 cout << " Only single - digit positive numbers are allowed .\ n " ; 23 break ; 24 }
Used for breaking out of a loop or switch statement. 1 // outputs first 10 positive integers 2 int i = 1; 3 while ( true ) 4 { 5 if ( i > 10) 6 break ; 7 cout << i << " \ n " ; 8 ++ i ; 9 }
Used for skipping the rest of a loop body and continuing to the next iteration. 1 // print out even numbers in 2 for ( int i = 0; i <= 10; ++ i ) 3 {
range 1 to 10
2 “Hello, World!”
This section is about writing the canonical “Hello, World!” program and its derivatives. Now is a good time to get used to your development environment. Submit each program in a separate source file named �section�.�subsection�.cpp.
Write a program that outputs “Hello, World!” by printing a const char * with value “Hello, World!”.
Write a program that outputs “Hello, World!” n times (where n is a nonnegative integer that the user will input) with:
3 More Programs
Now that you have had some practice working in your development environment, here are some more challenging tasks. Again, submit your work (source code and answers to ques tions) in an appropriately named text file.
For these questions, you are encouraged to use a computer.
Hints: Did your program compile? If so, what does it print? If not, what error message do you get?
Given a list of N integers, find its mean (as a double), maximum value, minimum value, and range. Your program will first ask for N , the number of integers in the list, which the user will input. Then the user will input N more numbers. Here is a sample input sequence:
3 <-- N 2 1 3
Three numbers are given: 2, 1, 3. The output should be as follows:
Mean: 2 Max: 3 Min: 1 Range: 2
Write a program to read a number N from the user and then find the first N primes. A prime number is a number that only has two divisors, one and itself.
3.4.1 Ternary operator
Write a program that loops indefinitely. In each iteration of the loop, read in an integer N (declared as an int) that is inputted by a user, output N 5 if N is nonnegative and divisible by 5, and -1 otherwise. Use the ternary operator (?:) to accomplish this. (Hint: the modulus operator may be useful.)
3.4.2 continue
Modify the code from 3.4.1 so that if the condition fails, nothing is printed. Use an if and a continue command (instead of the ternary operator) to accomplish this.
3.4.3 break
Modify the code from 3.4.2 to let the user break out of the loop by entering -1 or any negative number. Before the program exits, output the string “Goodbye!”.
Do these problems without the use of a computer!
4 Factorials Gone Wrong
This section focuses on debugging programs. We will start off with a simple factorial program and do a step by step troubleshooting routine.
Here is the code for a factorial program. Copy it into your IDE and verify that it compiles. 1 # include < iostream > 2 3 using namespace std ; 4 5 int main () 6 { 7 short number ; 8 cout << " Enter a number : " ; 9 cin >> number ; 10 11 cout << " The factorial of " << number << " is " ; 12 int accumulator = 1; 13 for (; number > 0; accumulator *= number - -) ; 14 cout << accumulator << " .\ n " ; 15 16 return 0; 17 }
What do you get when you enter the following values: 0, 1, 2, 9, 10?
Run the program and enter −1. What happens? How can you change the code such that it won’t exhibit this behavior?
Try entering some larger numbers. What is the minimum number for which your modified program from 4.2 stops working properly? (You shouldn’t have to go past about 25. You may find Google’s built-in calculator useful in checking factorials.) Can you explain what has happened?
Modify the given code such that negative inputs do not break the program and the smallest number that broke the program before now works. Do not hardcode answers.
Since we know that only a small number of inputs produce valid outputs, we can alterna tively hardcode the factorials of these inputs. Rewrite the program from the previous part (“Rewriting Factorial”) using a switch statement to demonstrate for inputs up to 10 how you would do this. (Of course, the code for inputs above 10 would basically be the same, but you do not need to go through the work of finding all those large factorials.)
Are there any other inputs we have to consider? Why or why not?