Professional Documents
Culture Documents
Total Marks: 04
Obtained Marks:
Object Oriented
Programming Techniques
Assignment # 04
Last date of Submission:17December2019
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;
}
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: