-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaverageMarks.cpp
More file actions
50 lines (49 loc) · 822 Bytes
/
averageMarks.cpp
File metadata and controls
50 lines (49 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<ctime>
using namespace std;
void createFile(fstream & file)
{
srand((unsigned)time(NULL));
int n=rand()%100;
cout<<"total subjects: "<<n<<endl;
for(int i=0;i<n;i++)
file<<rand()%100<<" ";
}
main()
{
fstream file;
file.open("Marks",ios::out|ios::in);
if(!file)
perror("");
/*if(!file)
{
throw(5);
};
catch(int)
{
cout<<"unable to open file";
}*/
createFile(file);
int max=-1;
float avg=0;
int marks;
int count=0;
ios_base::iostate s=file.rdstate();
while(!(s&ios_base::eofbit))
{
file>>marks;
//cout<<"shivam";
//file>>marks;
avg+=marks;
count++;
if(max<marks)
max=marks;
s=file.rdstate();
}
avg=avg/count;
cout<<fixed<<"Avg: "<<avg;
cout<<endl<<"Max marks is: "<<max;
cin.get();
}