Sri Krishna College of Engineering and Technology
Name :Dinesh Karthikeyan G M Email :23ee026@skcet.ac.in
Roll no:23ee026 Phone :9942227000
Branch :SKCET Department :EEE-A
Batch :2023_27 Degree :BE-EEE
2023_27_I_PS using CPP_IRC
PSCPP_Exception_Stream_CE
Attempt : 1
Total Mark : 50
Marks Obtained : 50
Section 1 : Coding
1. Problem Statement
Index out of bounds exception:
Create an array of size n and add elements to it. Get a position and a value
and modify the same.
If the position exceeds the size of the array, throw and catch an exception,
and print "Index out of bounds".
Answer
//You are using GCC
#include<iostream>
using namespace std;
int main()
{
int n1;
cin>>n1;
int a[n1];
for(int i=0;i<n1;i++){
cin>>a[i];
}
int n2;
int n3;
cin>>n2>>n3;
try{
if(n2<n1){
for(int i=0;i<n1;i++){
if(a[i]==a[n2]){
a[i]=n3;
}
}
}
else{
throw 1;
}
}
catch(int i){
cout<<"Index out of bounds"<<endl;
}
for(int i=0;i<n1;i++){
cout<<a[i]<<" ";
}
}
Status : Correct Marks : 10/10
2. Problem Statement
Check whether the person is eligible for a driving license or not. If the age
is less than 18, throw an exception and print "Invalid Age" and quit. If the
score is less than 40, throw an exception and print "You should get at least
40 marks" and quit. Else print "Passed".
Answer
// You are using GCC
#include <iostream>
using namespace std;
int main() {
int age, score;
cin >> age >> score;
try {
if (age < 18) {
cout << age << " " << score << std::endl;
throw "Invalid age";
} else if (score < 40) {
cout << age << " " << score << std::endl;
throw "You should get at least 40 marks";
} else {
cout << age << " " << score << std::endl;
cout << "Passed" << std::endl;
}
} catch (const char* exception) {
cout << exception << std::endl;
}
return 0;
}
Status : Correct Marks : 10/10
3. Problem Statement:
Data Type Exception
Get an integer input,
If the input is 1, Throw an Integer exception.
If the input is 2, Throw a Character exception.
If the input is 3, Throw a double exception.
Answer
// You are using GCC
#include <iostream>
#include <exception>
using namespace std;
int main() {
try {
int input;
cin >> input;
if (input == 1) {
throw exception();
} else if (input == 2) {
throw 'c';
} else if (input == 3) {
throw 1.23;
} else {
cout << "No exception" <<endl;
}
} catch (const std::exception& e) {
cout << "Integer exception caught." <<endl;
} catch (const char& e) {
cout << "Character exception caught." <<endl;
} catch (const double& e) {
cout << "Double exception caught." <<endl;
}
return 0;
}
Status : Correct Marks : 10/10
4. Count no of vowels- Files
Write a program to read the content from the file(sample.txt) and count
number of vowels.
Rules:
The input file should be named as "sample.txt".The input file should be
named as "sample.txt".
Answer
#include<iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream fout;
fout.open("sample.txt");
//You are using GCC
char str[300]="Time is a great teacher but unfortunately it kills all its pupils ";
fout<<str;
fout.close();
ifstream fin;
fin.open("sample.txt");
int count=0;
char ch;
while(!fin.eof())
{
fin.get(ch);
if(ch=='a'|| ch=='e'|| ch=='i'|| ch=='o'|| ch=='u'|| ch=='A'|| ch=='E'|| ch=='I'|| ch=='O'||
ch=='U')
{
count++;
}
}
cout<<"Number of vowels in file are"<<count;
fin.close();
return 0;
}
Status : Correct Marks : 10/10
5. File Ascending Order
Write a program to create a file with integer values and open the file in read
mode to read the content of the file then sort the content of the file in
ascending order.
Rule:
The input file name should be named as "sample.txt".
Answer
#include <iostream>
#include <fstream>
using std::ofstream;
using namespace std;
int main()
{
// You are using GCC
int n;
cin>>n;
int a[n];
int b[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
ifstream file;
file.open("sample.txt");
int x,i=0;
while(file>>x)
b[i++]=x;
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
if(a[i]>a[j]){
int temp = a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(int i=0;i<n;i++){
cout<<a[i]<<endl;
}
file.close();
return 0;
}
Status : Correct Marks : 10/10