Sie sind auf Seite 1von 5

C++ Worksheet

For the questions where you need to show output, you may run the code in your C++
compiler and see what output it gives. Please be aware that you will need to put in
#include etc and also give some basic values to variables if needed -

1. Given the following code -

if ((x>y) && (y >0))


x=x+y;
y++;
if (x >10)
y=x*y;
else
y=x+x*x+y;
cout<<" the values of x and y are respectively"<<x<<"and"<<y<<"\n";

What will this block of code print if variables x and y take respectively the following
values -
1. 5 and 3
2. 7 and 4
3. 5 and -1
4. 11 and 12;

2. Will the following source code compile and run? If not, explain why, otherwise describe
the execution output of the program.

if (0)
cout<<" Good Bye World ,";
else
cout<<" Where am I ?);
cout<<" if the world ended on December 21, 2012.\ n";

3. Write a program to ask the user to enter a mark adn then print the corresponding Grade
for that mark. i.e
90 - 100 A+
80 - 89 A
70 - 79 B
60 - 69 C
50 - 59 D
below 50 F
Make sure you write an "effective" program. i.e the program should perform as FEW
calculations and checking as possible.

4. What will be printed by the following block of code? and explain why.

i=0;
while(1)
{
i=i+1;
cout<<i<<"\n";
if (i>3)
break; }

5. What will be printed by the following block of code? and explain why.

i=5;
while(i)
{
i--;
cout<<i<<"\n";
}

6. Write a program that would read the colour code on a resistor and would print the
resistance value. Here is some information to get you started -

A resistor, such as the one shown in Figure 1, is a circuit element designed to have a
special electrical resistance between its two ends.
Resistance values are expressed in ohms. Resistors are frequently marked with colored
bands that encode their resistance values, as shown in Figure 2.

The first two bands encode the first two significant digits of the resistance value, the
third is a power-of-ten multiplier or number-of-zeroes, and the fourth is the tolerance
accuracy, or acceptable error, of the value.
For example, if the first band is green, the second is black, the third is orange, and the
fourth is silver, the resistor has a value of 50 x 103 +/- 50 x 103 ohms or 50 +/- 5 kilo
ohms

Figure 1

Figure 2
It is your wish how you implement this program. For example, you can ask the user to
enter the colour in words, such as "Black", "Green" etc but using strings might be
difficult. Instead you can ask them to enter specific numbers based on what colours they
want.
The challenge in this program is how you show the output. For example, if the user
chooses a resistor where the colour band is Orange, green, blue, silver. Then the
calculated resistance will be Orange Green x 10blue +/- silver i.e 35 x 106 +/- 10% of (35 x
106). You need to display this in terms of Kilo, Mega, Giga etc. Thus 35 x 106 +/- 10% of
(35 x 106) should be shown as 35 Mega ohms +/- 3.5 Mega ohms

Kilo = 103 Mega = 106 Giga = 109. So if you get any value from 1,000 to
9,99,999 you should write it as 1 Kilo ohm to 999.999 Kilo ohm. If the value if from
10,00,000 to 99,99,99,999 you should write it as 1 Mega ohm to 999.999 Mega ohm etc.

Here is a hint - accept bands 1 and 2 as ONE single number. Make the user enter the
values that way. Then multiply it by 10^band 3 and then depending on the value of band,
you can DIVIDE the final answer by the band to get the answer. For instance, if I wanted
to convert 1250 meters into Kilometers, I would divide by 1000 and write the answer as
1.25 Kilometers

7. What will be the output of this program?


int abc (int a)
{
int x =10;
cout<<" t in abc before increasing : x= "<<x<<"\n";
x=x+a;
cout<<"\ t in abc after increasing : x=" <<x<<"\n";
return x;
}
void main()
{
int x=100;
int a=20;
a=abc(x);
x=abc(a);
cout<<" in main : a = "<<a<<" x= "<< x;
}

8. Write a program to print the fibonacci sequence 1,1,2,3,5,8,11.... i.e if we need the ith
Fibonacci number Xi, then we can use the (i-1)th Xi-1and (i-2)th Xi-2 number and calculate
it as Xi = Xi-1 + Xi-2 . For example the 6th number in the sequence is 8. So we can use the
5th and 4th numbers 3 and 5 to get the 6th number 8 (3 + 5 = 8).

You need to write a function called int getFibonacci (int user_choice)


that will return the fibonacci number that the user chooses. user_choice is the variable
from the main() that will have the user's choice. IF the user_choice = 7, then the function
should return the 7th fibonacci number to the main() and print it.

9. Explain if the following program will have compiling error or run-time error. Compiler
errors are shown by the compiler. Run time errors are those that will make the program
run differently but will not show up when you compile.

int a [3]={1 , 2, 3};


int b [3]={4 , 5, 6};
a=b;
for (i=0; i <3; i++)
cout<< a[i]<<"\n";

10. A matrix (i.e., a rectangular array of numbers) is called binary if its entries are either 0 or
1. A matrix is called sparse if most of its entries are 0.
One idea is to initialise the 2D array as 0 and then put 1 wherever required. The objective
of this programming exercise is for you to develop such a representation for binary sparse
matrices. More specifically, you are to write a program that asks the user to input a binary
sparse matrix from the keyboard (by entering the locations of its 1's), the program then
prints out the number of 1's in each column. An example output of the program is given
below.

Enter the number of rows of the matrix :


3
Enter the number of columns of the matrix :
4
Enter the number of 1's in row 1:
2
Enter the column locations of the 1's in row 1:
1
3
Enter the number of 1's in row 2:
3
Enter the column locations of the 1's in row 2:
1
2
3
Enter the number of 1's in row 3:
2
Enter the column locations of the 1's in row 3:
3
4
OK , I got the matrix .
The number of 1's in column 1 is 2.
The number of 1's in column 2 is 1.
The number of 1's in column 3 is 3.
The number of 1's in column 4 is 1.

In the above example, the matrix that the user enters is (this is only for
your reference). Try to avoid using 2D arrays.

11. A positive integer triple (a; b; c) with 0 < a < b < c and a2 + b2 = c2 is called a
Pythagorean triple. Write a program which reads an integer N and prints
1. the total number of Pythagorean triples (a; b; c) with c < N,
2. every such Pythagorean triple

12. A positive integer greater than 1 is called a prime if it is only divisible by itself and by 1.
Write a program that prompts the user to enter an integer between 2 and 1000 and prints
out whether it is a prime. You can use a fixed formula, or you can start a loop to see if the
number is divisible by all numbers LESSER than itself. If it is NOT divisible, then it is
prime.

13. Using loops, create a function as such:


void drawLine(char c, int n)
The function will take a character c and draw it n times on a single line. For example
if you call drawLine (‘*’, 5); it will draw *****.

14. Write a program that creates a single dimentsional array of 10 floating type
numbers. Accept the values from the user and then print them in reverse. But you
need to first store the values into another array in reverse and then print that new
array

15. Write a program that creates a single dimentsional array of 10 numbers. Now fill
them with the first 5 positive numbers (1 to 10). Create another array and fill it with
the factorial of each number from the first array. Make sure the new array has the
PROPER DATA TYPE to hold the values of the factorial. Since "int" alone cannot
handle the factorial of numbers beyond 7.

Das könnte Ihnen auch gefallen