You are on page 1of 12

CHANDIGARH UNIVERSITY

Gharuan, Mohali
Institute/Department: University Institute of Engineering
Division: Academic Unit - 4
Subject Name: Object Oriented Programming Using C++
Subject Code: CST-152

Assignment No.: 3
Max. Marks: 12

Date of Allotment: 18- Mar -20


Last date of Submission: 02-Apr-20

Course Outcomes:
CO Title Level
Number
It will provide the environment that allows students to Understand
CO1
understand object-oriented programming Concepts.
Students will demonstrate basic experimental skills for Remember
differentiating between object-oriented and procedural
CO2
programming paradigms and the advantages of object-oriented
programs.
Ability to demonstrate their coding skill on complex Understand
CO3 programming concepts and use it for generating solutions for
engineering and mathematical problems.
Students will develop skills to understand the application of Understand
classes, objects, constructors, destructors, inheritance, operator
CO4
overloading and polymorphism, pointers, virtual functions,
templates, exception handling, file operations and handling.

Questions:

Sr. Group ASSIGNMENT CO


No. Numbe
r
1 Group Q1. What screen output does the following program produce, and CO4
A why?
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
char character;
int integer;
ofstreamout_stream;
ifstreamin_stream;
/* Create a file containing two integers */
out_stream.open("Integers");
out_stream<< 123 << ' ' << 456;
out_stream.close();
/* Attempt to read a character, then an integer,
then a character again, then an integer again,
from the file "Integers" just created. */
in_stream.open("Integers");
in_stream>> character >> integer;
cout<< "character: '" << character << "'\n";
cout<< "integer: " << integer << "\n";
in_stream>> character >> integer;
cout<< "character: '" << character << "'\n";
cout<< "integer: " << integer << "\n";
in_stream.close();

return 0;
}

CO3
Q2. Write a program which outputs its own C++ source file to the
screen.
Q3. Write a program that (i) starts with the output test statement: CO1
cout<< "Testing: " << 16/2 << " = " << 4*2 <<".\n\n"; CO2
and then (ii) outputs its own C++ source file to a file called
"WithoutComments.cpp" and to the screen, but omitting any of
the comments enclosed in "/* ... */" markers (and omitting the
markers themselves). The new program in the file
"WithoutComments.cpp" should compile and run in exactly the
same way as the program from which it was generated.
Q4. Write a program to produce the output as shown below: CO1
Results:
x value y value expressions results
10 |5 | x+=y | x=15
10 |5 | x-=y-2 | x=7
10 |5 | x*=y*5 | x=250
10 |5 | x/=x/y | x=5
10 |5 | x%=y | x=0

Q5. Write C++ program to display a table that represents a Pascal CO4
triangle of any size. In Pascal triangle, the first and the second
rows are set to 1. Each element of the triangle (from the third
row downward) is the sum of the element directly above it and
the element to the left of the element directly above it. See the
example Pascal triangle(size=5) below:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
2 Grou Q1. Write a program which counts and displays the number of CO3
pB characters (including blanks) in its own source code file.
Q2. Assuming that a text file named FIRST.TXT contains some text CO4
written into it, write a function named vowelwords(), that reads
the file FIRST.TXT and creates a new file named
SECOND.TXT, to contain only those words from the file
FIRST.TXT which start with a lowercase vowel (i.e., with 'a', 'e',
'i', 'o', 'u').
For example, if the file FIRST.TXT contains Carry umbrella and
overcoat when it rains Then the file SECOND.TXT shall contain
umbrella and overcoat it
Q3. What is the output of this program? CO2
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
int length;
char * buffer;
ifstream is;
is.open ("sample.txt", ios :: binary );
is.seekg (0, ios :: end);
length = is.tellg();
is.seekg (0, ios :: beg);
buffer = new char [length];
is.read (buffer, length);
is.close();
cout.write (buffer, length);
delete[] buffer;
return 0;
}

Q4. Write a C++ program to add two fractions and display the result CO4
fraction. Your program will prompt the user to input fraction 1
and fraction 2. The numerator and denominator of each fraction
are input separately by space. See the example output below.
You will need to use a C++ structure to define a fraction. The
structure has two members: numerator and denominator.
Enter fraction 1(numerator denominator): 1 2
Enter fraction 2(numerator denominator): 2 5
Result: 9/10
Q5. What is a virtual pointer or vfptr and virtual function table CO1
vftable? explain with diagram and example?
3 Grou Q1. Without using an array write a program that prints itself out CO2
pC backwards on the screen.

Q2. If we have object from ofstream class, then default mode of CO4
opening the file is _____
a. ios::in
b. ios::out
c. ios::in|ios::trunc
d. ios::out|ios::trunk
Q3. Write a function in C++ to count and display the number of CO4
lines not starting with alphabet 'A' present in a text file
"STORY.TXT".
Example:
If the file "STORY.TXT" contains the following lines,
The rose is red.
A girl is playing there.
There is a playground.
An aeroplane is in the sky.
Numbers are not allowed in the password.
The function should display the output as 3
Q4 Any character is entered by the user; write a program to CO2
determine whether the character entered is a capital letter, a
small case letter, a digit or a special symbol. The following table
shows the range of ASCII values for various characters.
Characters ASCII Values

A–Z 65 – 90

a–z 97 – 122

0–9 48 – 57

special symbols 0 - 47, 58 - 64, 91 - 96, 123 – 127

Q5. Give the output of the following program CO3


By seeing which operator thus this program stops getting the
input?
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
char ch;
streambuf * p;
ofstreamos ("test.txt");
pbuf = os.rdbuf();
do {
ch = cin.get();
p ->sputc(ch);
} while (ch != '.');
os.close();
return 0;
}
4. Grou Q1. What is the Output of following program? CO1
pD #include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
int main ()
{
ifstreamifile;
ifile.open ("text.txt");
char last;
ifile.ignore (256, ' ');
last = ifile.get();
cout<< "Your initial is " << last << '\n';
ifile.close();
return 0;
1. }
Q2. User defined function in C++ named copyupper(), that reads CO2
the file FIRST.TXT and creates a new file named
SECOND.TXT contains all words from the file FIRST.TXT in
uppercase
Q3. Given positive two integers m and n such that m<n, the greatest CO4
common divisor of m and n is the same as the greatest common
divisor of m and (n-m). Use this fact to write a recursive
definition of the function "greatest_common_divisor(...)",
which takes two positive integer arguments and returns their
greatest common divisor. Test your function in a suitable main
program.
Q4. What will act as a intermediate between i/o operations and CO4
physical file?
a)Memory
b)Ram
c)Stream buffer
d) None of the mentioned
Q5. The Fibonacci sequence a(1), a(2), a(3), ..., a(n), ... is defined CO3
by
• a(1) =1
• a(2) = 1
• a(n) = a(n-1) + a(n-2), for all n > 2
This generates the sequence
1, 1, 2, 3, 5, 8, 13, 21, ...
Write a C++ function "fibonacci(...)" that computes the
Fibonacci number corresponding to its positive integer argument,
so that, for example, fibonacci(7) == 13.

5 Grou Q1. What will be the changes made to the content of the file after CO3
pE running the code?why?
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
//create a text file named file before running.
ofstreamofile;
ofile.open ("file.txt");
ofile<< "geeksforgeeks", 13;
ofile.seekp (8);
ofile<< " geeks", 6;
ofile.close();
return 0;
}
Q2. Write a program in C++ to read the content from a text file CO3
OUT.TXT, count and display the number of alphabets present
in it.
Q3. Which one is always faster in writing on C++? CO1
a) Writing to a file
b) Writing to memory
c) Reading from the network
d) None of the mentioned
Q4. To convert temperatures written in Fahrenheit to Celsius CO2
(Centigrade), you subtract 32, multiply by 5 and then divide by
9. To convert Celsius to Absolute Value (Kelvin), you add
273.15. Write a program that displays a temperature conversion
chart on the screen as follows:
Fahrenheit Celsius Absolute Value
0 -17.78 255.37
20 -6.67 266.48
40 4.44 277.59
... ...... ......
... ...... ......
300 148.89 422.
Q5. Write a C++ program that will output the multiplication table CO3
as show below:

1*1=1 2*1=2 3*1=3 …… 9*1=1


1+2=2 2*2=4 3*2=6 …… 9*2=18
……. ……. ……. …… …….
1*9=9 2*8=18 3*9=27 …… 9*9=81
6 Grou Q1. What is the Output of following program? CO1
pF #include<iostream>
#include<fstream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
ifstreamifile;
ifile.open ("text.txt");
cout<< "Reading data from a file :-" <<endl ;
int c = ifile.peek();
if ( c == EOF ) return 1;
if ( isdigit(c) )
{
int n;
ifile>> n;
cout<< "Data in the file: " << n << '\n';
}
else
{
string str;
ifile>> str;
cout<< "Data in the file: " << str << '\n';
}
ifile.close();
return 0;
1. }
Q2 Write a program to add record in a file, search record by name CO3
telephone number and display all records from a file
Q3. Write a function called zeroSmaller() that is passed two int CO4
arguments by reference and then sets the smaller of the two
numbers to 0. Write a main()program to exercise this function.
Q4. Raising a number to a power p is the same as multiplying n by CO2
itself p times. Write a function called power that takes two
arguments, a double value for n and an int value for p, and return
the result as double value. Use default argument of 2 for p, so
that if this argument is omitted the number will be squared.
Write the main function that gets value from the user to test
power function.
Q5. WAP to perform Read & Write File Operation to Convert CO2
lowercase to uppercase
7 Grou Q1. What is the Output of following program? CO4
pG #include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
int main ()
{
ifstreamifile;
ifile.open ("text.txt");
char last;
ifile.ignore (256, ' ');
last = ifile.get();
cout<< "Your initial is " << last << '\n';
ifile.close();
return 0;
1. }
Q2. Write a program to store the information of about 5 employees CO4
in a file and read the contents and print them on screen.
Q3. WAP in c++ that reads the file FIRST.TXT and creates a new CO2
file named SECOND.TXT, to contain only those words from the
file FIRST.TXT which start with a lowercase vowel.

Q4. Raising a number onto a power p is the same as multiplying n by CO3


itself p times. Write a function called power() that takes a double
value for n and an int value for p, and returns the result 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 values from the user to test this
function.
Q5. Write a program that (i) starts with the output test statement: CO4
cout<< "Testing: " << 16/2 << " = " << 4*2 <<".\n\n";
and then (ii) outputs its own C++ source file to a file called
"WithoutComments.cpp" and to the screen, but omitting any of
the comments enclosed in "/* ... */" markers (and omitting the
markers themselves). The new program in the file
"WithoutComments.cpp" should compile and run in exactly the
same way as the program from which it was generated.
8 Grou Q1. Write a program to store the information of about 5 employees CO3
pH in a file and read the contents and print them on screen.
Q2. Write a C++ program to compute the real roots of the equation: CO4
ax2+bx+c=0.
The program will prompt the user to input the values of a, b,
and c. It then computes the real roots of the equation based on
the following rules:
-if a and b are zero=> no solution
-if a is zero=>one root (-c/b)
-if b2-4ac is negative=>no roots
-Otherwise=> two roots
The roots can be computed using the following formula:
x1=-b+(b2-4ac)1/2/2a
x=-b-(b2-4ac)1/2/2a
Q3. Assuming that a text file named FIRST.TXT contains some text CO3
written into it, write a function named copyupper(), that reads
the file FIRST.TXT and creates a new file named
SECOND.TXT contains all words from the file FIRST.TXT in
uppercase.
Q4. rite C++ program to display a matrix as shown below. The CO4
diagonal of the matrix fills with 0. The lower side fills will -1s
and the upper side fills with 1s.

0 1 1 1 1
-1 0 1 1 1
-1 -1 0 1 1
-1 -1 -1 0 1
-1 -1 -1 -1 0
Q5. Program to write and read an object in, from binary file using CO4
write() and read() in C++.
9 Grou Q1. Write a program which counts and displays the number of CO1
pI characters (including blanks) in its own source code file.
Q2. Assuming that a text file named FIRST.TXT contains some text CO3
written into it, write a function named vowelwords(), that reads
the file FIRST.TXT and creates a new file named
SECOND.TXT, to contain only those words from the file
FIRST.TXT which start with a lowercase vowel (i.e., with 'a', 'e',
'i', 'o', 'u'). For example, if the file FIRST.TXT contains Carry
umbrella and overcoat when it rains Then the file
SECOND.TXT shall contain umbrella and overcoat it.
Q3. write C++ program to display a table of numbers as shown CO4
below:
1 2 3 4 5
6 7 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
Q4. Write a program that will ask the user to input three integer CO3
values from the keyboard. Then it will print the smallest and
largest of those numbers.
Q5.WAP Read Characters from One file and Write them in Toggle CO4
Case in Other using C++ file stream.
10 Grou Q1. Write a program that (i) starts with the output test statement: CO3
pJ cout<< "Testing: " << 16/2 << " = " << 4*2 <<".\n\n";
and then (ii) outputs its own C++ source file to a file called
"WithoutComments.cpp" and to the screen, but omitting any of
the comments enclosed in "/* ... */" markers (and omitting the
markers themselves). The new program in the file
"WithoutComments.cpp" should compile and run in exactly the
same way as the program from which it was generated
Q2. What is the Output of following program? CO3
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
int main ()
{
ifstreamifile;
ifile.open ("text.txt");
char last;
ifile.ignore (256, ' ');
last = ifile.get();
cout<< "Your initial is " << last << '\n';
ifile.close();
return 0;
}
Q3. WAP Open a file and append data to the end of the file. CO4

Q4. The Fibonacci sequence a(1), a(2), a(3), ..., a(n), ... is defined CO3
by
• a(1) =1
• a(2) = 1
• a(n) = a(n-1) + a(n-2), for all n > 2
This generates the sequence
1, 1, 2, 3, 5, 8, 13, 21, ...
Write a C++ function "fibonacci(...)" that computes the Fibonacci
number corresponding to its positive integer argument, so that,
for example, fibonacci(7) == 13.
Q5. Write a C++ program that will display the calculator menu. CO4
The program will prompt the user to choose the operation choice
(from 1 to 5). Then it asks the user to input two integer vales for
the calculation. See the sample below.
MENU
1. Add
2. Subtract
3. Multiply
4. Divide
Enter your choice: 1
Enter your two numbers: 12 15
Result: 27
Continue? Y
The program also asks the user to decide whether he/she
wants to continue the operation. If he/she input ‘y’, the
program will prompt the user to choose the operation gain.
Otherwise, the program will terminate.
11 Grou Q1. What is the Output of following program? CO1
pK #include<iostream>
#include<fstream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
ifstreamifile;
ifile.open ("text.txt");
cout<< "Reading data from a file :-" <<endl ;
int c = ifile.peek();
if ( c == EOF ) return 1;
if ( isdigit(c) )
{
int n;
ifile>> n;
cout<< "Data in the file: " << n << '\n';
}
else
{
string str;
ifile>> str;
cout<< "Data in the file: " << str << '\n';
}
ifile.close();
return 0;
}
Q2.WAP to depict the working of seekg() and seekp(). CO4

Q3. Given positive two integers m and n such that m<n, the greatest CO3
common divisor of m and n is the same as the greatest common
divisor of m and (n-m). Use this fact to write a recursive
definition of the function "greatest_common_divisor(...)",
which takes two positive integer arguments and returns their
greatest common divisor. Test your function in a suitable main
program.
Q4. Write a function to count the number of blank present in a text CO3
file named "OUT.TXT".
Q5. Write a C++ program that will output the multiplication table as CO1
show below:

1*1=1 2*1=2 3*1=3 …… 9*1=1


1+2=2 2*2=4 3*2=6 …… 9*2=18
……. ……. ……. …… …….
1*9=9 2*8=18 3*9=27 …… 9*9=81
12 Grou Q1. Write a function in C++ to count and display the number of lines CO3
pL not starting with alphabet 'A' present in a text file
"STORY.TXT".
Example:
If the file "STORY.TXT" contains the following lines,
The rose is red.
A girl is playing there.
There is a playground.
An aeroplane is in the sky.
Numbers are not allowed in the password.
The function should display the output as 3.
Q2. Write a program which will raise any number x to a positive CO4
power n using a "for loop".
Q3. To convert temperatures written in Fahrenheit to Celsius CO3
(Centigrade), you subtract 32, multiply by 5 and then divide by
9. To convert Celsius to Absolute Value (Kelvin), you add
273.15. Write a program that displays a temperature conversion
chart on the screen as follows:
Fahrenheit Celsius Absolute Value
0 -17.78 255.37
20 -6.67 266.48
40 4.44 277.59
... ...... ......
... ...... ......
300 148.89 422.04
Q4. Write a C++ program to keep records and perform statistical CO4
analysis for a class of 20 students. The information of each
student contains ID, Name, Sex, quizzes Scores (2 quizzes per
semester), mid-term score, final score, and total score.
The program will prompt the user to choose the operation of
records from a menu as shown below:

================================================
==
MENU
================================================
==
1. Add student records
2. Delete student records
3. Update student records
4. View all student records
5. Calculate an average of a selected student’s
scores
6. Calculate total scores of a selected student
7. Display the highest and lowest scores
8. Sort students’ records by ID
9. Sort students' records by total score
Enter your choice:1
Q5. Write a program which outputs its own C++ source file to the CO4
screen.

You might also like