Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
91 views

CPP

This tutorial document provides examples and exercises for C++ programming tutorials. It includes: 1. Examples of writing programs to perform conversions between units like gallons to cubic feet, generate tables of data, and determine if a character is uppercase or lowercase. 2. More advanced examples including a currency conversion program, adding fractions, and calculating electricity bills based on consumption tiers. 3. Further examples like generating tables of multiples, temperature conversions, and basic four-function calculators for decimals and fractions. 4. Examples of writing functions for calculating circle areas, exponentiation, comparing numbers, and adding distances. 5. More object-oriented examples including classes for time, students, arrays, and rectangles

Uploaded by

fsdv
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

CPP

This tutorial document provides examples and exercises for C++ programming tutorials. It includes: 1. Examples of writing programs to perform conversions between units like gallons to cubic feet, generate tables of data, and determine if a character is uppercase or lowercase. 2. More advanced examples including a currency conversion program, adding fractions, and calculating electricity bills based on consumption tiers. 3. Further examples like generating tables of multiples, temperature conversions, and basic four-function calculators for decimals and fractions. 4. Examples of writing functions for calculating circle areas, exponentiation, comparing numbers, and adding distances. 5. More object-oriented examples including classes for time, students, arrays, and rectangles

Uploaded by

fsdv
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Tutorial 1 (11-09-2020): Introduction to lab

Tutorial 2(18.09.2020)
1. Assuming there are 7.481 gallons in a cubic foot, write a program that asks the user to enter a
number of gallons, and then displays the equivalent in cubic feet.
2. Write a program that generates the following table:
1990 135
1991 7290
1992 11300
1993 16200
Use a single cout statement for all output.
3. A library function, islower(), takes a single character (a letter) as an argument and returns a
nonzero integer if the letter is lowercase, or zero if it is uppercase. This function requires the
header file CTYPE.H. Write a program that allows the user to enter a letter, and then displays
either zero or nonzero, depending on whether a lowercase or uppercase letter was entered.
(See the SQRT program for clues.)
4. On a certain day the British pound was equivalent to $1.487 U.S., the French franc was
$0.172, the German deutschemark was $0.584, and the Japanese yen was $0.00955. Write a
program that allows the user to enter an amount in dollars, and then displays this value
converted to these four other monetary units.
5. If you have two fractions, a/b and c/d, their sum can be obtained from the formula
Write a program that encourages the user to enter two fractions, and then displays their
sum in fractional form. (You don’t need to reduce it to lowest terms.)
The interaction with the user might look like this:
Enter first fraction: 1/2
Enter second fraction: 2/5
Sum = 9/10
6. An electricity board charges the following rates to domestic users to discourage large
consumption of energy. For the first 100 units Rs. 1.50 per unit For the next 200 units Rs.
3.00 per unit Beyond 300 units Rs. 5.00 per unitAll users are charged a minimum of Rs.100. If
the total cost exceeds Rs.250, then an additional surcharge of 15% is added. Write a program
to read the names of users and number of units consumed and print out the charges with
names.

Tutorial 3(25.09.2020)

1. Assume that you want to generate a table of multiples of any given number. Write a
program that allows the user to enter the number and then generates the table, formatting
it into 10 columns and 20 lines. Interaction with the program should look like this (only the
first three lines are shown):
Enter a number: 7
7 14 21 28 35 42 49 56 63 70
77 84 91 98 105 112 119 126 133 140
147 154 161 168 175 182 189 196 203 210
2. Write a temperature-conversion program that gives the user the option of converting
Fahrenheit to Celsius or Celsius to Fahrenheit. Then carry out the conversion. Use floating-
point numbers. Interaction with the program might look like this: Type 1 to convert Fahrenheit
to Celsius, 2 to convert Celsius to Fahrenheit: 1 Enter temperature in Fahrenheit: 70 In
Celsius that’s 21.111111
3. Create the equivalent of a four-function calculator. The program should ask the user to enter
a number, an operator, and another number. (Use floating point.) It should then carry out the
specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers.
Use a switch statement to select the operation. Finally, display the result. When it finishes the
calculation, the program should ask whether the user wants to do another calculation.
4. Create a four-function calculator for fractions. Here are the formulas for the four arithmetic
operations applied to fractions:
• Addition: a/b + c/d = (a*d + b*c) / (b*d)
• Subtraction: a/b - c/d = (a*d - b*c) / (b*d)
• Multiplication: a/b * c/d = (a*c) / (b*d)
• Division: a/b / c/d = (a*d) / (b*c)
•The user should type the first fraction, an operator, and a second fraction. The program should
then display the result and ask whether the user wants to continue.

Tutorial 4(09.10.2020)

1. Write a function called circarea() that finds area of circle.It should take an argument of type
float and return an argumentof the same type .Write a main function that gets a readius
value from the user ,calls circarea() and display the result.
2. Raising a number n to a power ‘p’ is the same as multiplying n by itself p times.Write a
function called power() that takes a double value for n and an int value for p and the result is
returned as a double value.Use a default argument of 2 for p so that if this argument is
omitted ,the number ‘n’ will be squared ,Write a main() function that gets the values from
the user to test this function.
3. Write a function zeroSmaller() that is passed two integers by reference and then sets the
smaller of the two numbers to zero.Write a main program to exercise this function.
4. Write a function that takes two distance values as arguments and returns the larger one.
Include a main() program that accepts two distance values from the user ,compares them
and displays the larger.
5. Addition of two points using a structure
6. Program to input different parts of telephone code and display output using structure
7. Print Pattern
1
1 2 3
1 2 3 4 5
8. Print Pattern
*
* * *
* * * * *
9. Program to create a structure to add two distances that are given in feet and inches

Tutorial 5(23-10-2020)

1. TO PRINT the sequence


1
1*2
1*2*3
1*2*3*4
1*2*3*4*5
2. To print the series
*
12
***
1234

*****

3. Create a class called time that has seperate int member data for
hours,minutes,seconds.Include constructors to initialise the data to 0 and to fixed value.
Also include nesscesary member functions to display the time in 11:59:59 format and
function to add object of time passed as argument.
4. Create a class STUDENT declare data members as USNnumber(int type),semester(int
type),six subject marks.
Declare a member function to read(),percent() and display.Calculate the percentage of a
student using percent(),
and based on percentage assign a class to a student with the following conditions
if percentage>=75% - FCD
between 60 to 74% - FC
between 50 to 59% - SC
below 50%- FAIL
Make this record for 10 students
5. averaging of array elements
6. Demonstrattion of Multi-dimensional array
7. Program to show use of static data members
8. Function Overloading for area of triangle,rectangle,circle
9. Write a class “rectangle” containing two data items “length” and breadth “ and four
functions setdata(), getdata(),displaydata() and area () to set the length and breadth,
to get the user inputs, to display and to find the area of the rectangle respectively.Also write
a main program which declares the objects and uses the member functions of the class.
10. Create a four – function calculator for fractions. The user should type the first fraction, an
operator, and a second fraction. The program should then display the result and ask
whether the user wants to continue. The functions should be called using fadd( ), fsub( ),
fmul( ), and fdiv( ). Each of these functions should take two arguments of type struct
fraction, and return an argument of the same type.

Tutorial 6(06-11-2020)

1. Write a function called reverseit() that reverses a C-string(an array of charecter).Use a for loop
that swaps the first and last last charecter,then the second and last but one character and so on
.The string should be passed to reverseit() as an argument.Write a program to get a string from
the user ,call reverseit(), and print out the result .Use an input method that allows embedded
blanks.

2. Create a class called employee that contains a name (an object of class string) and an employee
number (type long).Include a member function called getdata() to get data from the user for
insertion into the object and another function called putdata() to display the data.Assume that
name has no embedded blanks.Write a main() program to excercise this class.It should create an
array of type employee and then invite the user to input data for up to 100 employees.Finally, it
should print out data for all the employees.
3. Write a program that calculates the average of upto 100 English distance create an array of
objects of the class Distance.
4. Start with a program that allows the user to input a number of integers and then stores them in
an int array.Write a function called maxint() that goes through the array ,element by
element,looking for the largest one .The function should take the address of the array and the
number of elements in it as arguments.And return the index number of the largest element.The
program should call this function and then display the largest element and its index number

5. Concatenation of Two strings without Inbuilt functions

6. Write a program to compare two STRING data type ,If the two values are equal print that they
are equal otherwise print they are not equal.
7. Implementing a stack with safe push and safe pop operations

Tutorial 7(13-11-2020)

1. Create a record of upto 100 students using multilevel Inheritance


2. Create a record of 100 students using multiple inheritance
3. Create a record of upto 100 employee using Multiple Inheritance with atleast three classes
4. Write a code to Demonstrate ambiguity resolution
5. For the code shown in Qn 4 find
a. What happens if we use p.M::display() in main function
b. What happens if we use p.N::display() in main function
c. What would happen display function in class M having return type int and modify in
main function int c = p.display() and cout<< c
6. Write a program to demonstrate virtual Base class
7. In program 6 what would happen if:
a. If we use no virtual keyword
b. If we use virtual keyword for one base class
c. If we use virtual keyword for both the base classes
8. Write a program to create a class STUDENT with data members USN, Name, Age. Using
Inheritance create class UGSTUDENT having fields semester, Fees and Stipend. Enter data for
5 students.
9. Create a class Shape that has 2 variables length and width. Inherit classes Rectangle and
Triangle from Shape with public access. Write a C++ program to output the area of Rectangle
and Triangle.

Tutorial 8(20-11-2020)

1. Write a program to demonstrate initialization of data members using constructors in single


inheritance without arguments.
2. Write a program to demonstrate initialization of data members using constructors in single
inheritance with arguments.
3. Write a program to demonstrate initialization of data members using constructors in single
inheritance with arguments and by using private access specifier.
4. Write a program to display address of a variable.
5. Demonstrate the use of & and * operator in pointers.
6. Show if we can use floating pointer variable to store address of an integer variable and how
to use void pointer to store address of any other datatype of variables.
7. Write a program in C++ to find the maximum number between two numbers using a pointer
8. Write a program in C++ to add two numbers using pointers

Tutorial 9(27-11-2020)

1. Swapping of two numbers using Reference Variable.


2. Swapping of two numbers using pointers
3. Bubble Sort algorithm using arrays.
4. Bubble sort algorithm using pointers
5. String length using pointers
6. String copy using pointers
7. String concat using pointers
8. To demonstrate the use of new operator to allocate memory.
9. Program to demonstrate memory allocation for an array using a constant to set the
dimensions of the array.
10. Program to demonstrate memory allocation for an array using a variable to set dimensions
of an array.
11. Program to show use of delete operator

Tutorial 10(04-12-2020)

1. Program to show Array of Pointers


2. Pointer to object, assign it with the address of object.
3. Another Approach to new : Distance& dist = *(new Distance);
4. Class String and show the use of constructors and destructors
5. Show output for the following code

#include <iostream>

using namespace std;

////////////////////////////////////////////////////////////////

class Base //base class

public:

void show() //normal function

{ cout<<"Base\n"; }

};

////////////////////////////////////////////////////////////////

class Derv1 : public Base //derived class 1

public:

void show()

{ cout << "Derv1\n"; }

};
////////////////////////////////////////////////////////////////

class Derv2 : public Base //derived class 2

public:

void show()

{ cout << "Derv2\n";

};

int main()

Derv1 dv1; //object of derived class 1

Derv2 dv2; //object of derived class 2

Base* ptr; //pointer to base class

ptr = &dv1; //put address of dv1 in pointer

ptr->show(); //execute show()

ptr = &dv2; //put address of dv2 in pointer

ptr->show(); //execute show()

return 0;

You might also like