You are on page 1of 3

Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Total Marks: 04

Obtained Marks:

Object Oriented
Programming Techniques
Assignment # 04
Last date of Submission:17December2019

Submitted To: Muhammad Usman


___________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Student Name: Mahrukh Zafar ______________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Reg Number: 1880193


____________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Object Oriented Programming BS(SE)-2 SZABIST-ISB


Techniques
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Instructions:Copied or shown assignments will be marked zero. Late submissions are not
entertained in any case.

Questions:
Assign values to an int array by using random function between 0 to 250.
(250/assigned value) apply this calculation for each element of the array and
save the result in a file (abc.txt). Also use exception handling.
Solution
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int marks[3];
int total_marks = 250;
cout << "enter marks" << endl;
for (int i = 1; i <= 3; i++)
{
cin >> marks[i];
}
for (int i = 1; i <= 3; i++)
{
try
{
if (marks[i] == 0)
{
cout << "result=" << marks[i] << endl;
}
else
{
throw marks[i];
}
}
catch (int a)
{
cout << "marks = " << a << endl;
}
}
for (int i = 1; i <= 3; i++)
{
cout << "marks = " << marks[i] / total_marks << endl;

Object Oriented Programming BS(SE)-2 SZABIST-ISB


Techniques
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

}
ofstream fout;
fout.open("sample.txt");
for (int i = 1; i <= 3; i++)
{
fout << marks[i];
}
fout.close();
ifstream fin;
fin.open("sample.txt");
cout << "Marks \t\t" << endl;
for (int i = 1; i<=3; i++)
{
cout << marks[i] << "\t\t" << endl;
}
fin.close();
system("pause");
return 0;
}

Output:

Object Oriented Programming BS(SE)-2 SZABIST-ISB


Techniques

You might also like