Wednesday 12 September 2012

sample questions of c++



Course: Programming Fundamentals                                                                                    Lab 06

Problem 1:
Implement the functions with the prototypes below:

long square (int number);
long cube (int number);

The function square returns the square of the number passed as argument and the function cube returns the cube of the number passed as argument to the function.  The functions are placed in the header file named lab6.h

Write a program that prints the squares and cubes of the numbers from 0 to 10 in the format shown below. The program includes the header file lab6.h and calls the functions square and cube to calculate these values.

Sample Output
Number      Square      Cube
0           0           0
1           1           1
2           4           8
3           9           27
4           16          64
5           25          125
6           36          216
7           49          343
8           64          512
9           81          729
10          100         1000


Problem 2:
Implement a function with the prototype below:

double calRectangle(int option, double length, double width);

The function returns the area of the rectangle if option is 1 and returns perimeter of the rectangle for option value 2. The function is placed in the header file named lab6.h

Write a program that takes the length and width of a rectangle as input from console and prints the area and perimeter of the rectangle. The program includes the header file lab6.h and calls the function calRectangle to calculate the area and perimeter of the rectangle.

Hint:        Area of rectangle = length * width
Perimeter of rectangle = 2(length + width)

Sample Output
Enter length of the rectangle: 10
Enter width of the rectangle : 5

Area of the rectangle      = 50
Perimeter of the rectangle = 30



Problem 3:
Implement the functions with the prototypes below:

int isOdd(int number);
int isPrime(int number);
int isMultiple(int number1, int number2);

The function isOdd returns 1 if the number passed as input argument is odd otherwise it returns 0.
The function isPrime returns 1 if the number passed as input argument is prime otherwise it returns 0.
The function isMultiple returns 1 if the number passed as 1st input argument is a multiple of number passed as 2nd input argument otherwise it returns 0. The functions are placed in the header file named lab6.h

Write a program that tests the functionality of the functions above as shown in the format below. The program includes the header file lab6.h and calls the functions above to determine if a number is even/odd OR if a number is prime or not OR if a number is multiple of the other.

Sample Output
Enter 1 to test if a number is even or odd, 2 to test if the number is prime and 3 to test if one number is multiple of other: 1

Enter number: 19
19 is an odd number.

Do you want to run this program again (y/n)? y

Enter 1 to test if a number is even or odd, 2 to test if the number is prime and 3 to test if one number is multiple of other: 2

Enter number: 29
29 is a prime number.

Do you want to run this program again (y/n)? y

Enter 1 to test if a number is even or odd, 2 to test if the number is prime and 3 to test if one number is multiple of other: 3

Enter first number: 15
Enter second number: 3
15 is a multiple of 3.

Do you want to run this program again (y/n)? n



Problem 4:
Implement the function with the prototypes below:

double newSal (double salary, double increment);

The function newSal returns the new salary of a person if the initial salary passed in 1st argument increases by the %age value passed in 2nd argument. The function is placed in the header file named lab6.h

Write a program that takes the current salary of a person as input from console and prints the salary table below after 1st, 2nd, 3rd, 4th and 5th year assuming that the salary increases at rate 5%, 6%, 7%, 8%, 9% and 10% every year. The program includes the header file lab6.h and calls the function newSal to calculate these values.

Sample Output
Enter salary: 10000

       1 Year        2Years        3Years        4Years        5Years
5%    10500.00    11025       11576.25    12155.06    12762.82
6%    10600       11236       11910.16    12624.77    13382.26
7%                                                    
8%                                                    
9%                                                    
10%                                                   
Problem 5:
Implement the function with the prototypes below:

void translate (long);

The function translate takes in a number as input argument and prints it in words. You can also develop other functions that can be called from this function to reduce repetition of code. The functions are placed in the header file named lab6.h

Write a program that takes a number as input from console and translates it into words. The program includes the header file lab6.h and calls the function translate to translate the number into words.

Sample Output
Enter the a number: 2147483647

You entered two billion one hundred forty seven million four hundred eighty three thousand six hundred forty seven



Problem 6:
Implement the function with the prototypes below:

long power (int x, int y);

The function power takes in two integers as arguments and returns xy. The function uses the recursion algorithm for implementation and is placed in the header file named lab6.h

Write a program that reads x and y as integers from console. The program includes the header file lab6.h and calculates x ^ y by calling the function above and prints in the format shown below:

Sample Output
Enter x: 2
Enter y: 3

2 ^ 3 = 8


Problem 7:
Implement the function with the prototypes below:

long timeInSec (int hour, int min, int sec);

The function takes three integer arguments (hours, minutes and seconds) and returns the number of seconds since the last time the clock struck 12.

Write a program that uses this function to calculate the amount of time in seconds between two times both of which are within a 12 hour cycle of clock. The program includes the header file lab6.h


Problem 8:
Implement the function with the prototypes below:

int isPerfect (int number);

The function returns 1 if the number passed as input argument is a perfect number otherwise it returns 0. An integer is said to a perfect number if the sum of its factors including 1 (but not the number itself) is equal to the number. e.g., 6 is a perfect number since 6 = 1 + 2 + 3. The function is placed in the header file named lab6.h

Write a program that uses this function to print all the perfect numbers between 1 and 1000.  The program includes the header file lab6.h





Problem 9:
Implement the function with the prototype below:

int gcd (int number1, int number2);

The function returns the greatest common divisor of two numbers passed as input arguments The greatest  common divisor (GCD) is the largest integer that divides the two integers. The function is placed in the header file named lab6.h

Write a program that reads in two numbers from console and uses this function to calculate and print the greatest common divisor of the two numbers. The program includes the header file lab6.h


Problem 10:
Implement the function with the prototype below:

char grade (int number);
void gradeDescription (char grade);
double gpa(char grade);

The function grade takes in the marks in a course as input argument and returns the grade of a student as per rules in the grading table below.

The function gradeDescription takes in the grade in a course as input argument and prints a description of the grade as mentioned in the grading table below.

The function gpa takes in the grade in a course as input argument and returns the grade point average (GPA) of a student in that course as per rules in the grading table below.

Write a program that reads in the marks in a course. The program includes the header file lab6.h and calculates and prints the grade, grade point average and grade description by calling the respective functions in the format shown below:

Sample Output
Enter marks in a course (0-100): 76

Marks Grade GPA   Description
76    B     3     Good


Grading Table:
Grade Criteria
Grade
Grade Point
Grade Description
Marks greater than 0   AND less than 50
F
0
Fail.
Marks 50 or Greater than 50 AND less than 60
D
1
Poor
Marks 60 or Greater than 60 AND less than 70
C
2
Satisfactory
Marks 70 or Greater than 70 AND less than 80
B
3
Good
Marks 85 or Greater than 80 AND less than or equal to 100
A
4
Excellent




No comments:

Post a Comment