Help please!!
May 15, 2015 at 9:39am UTC
Write your question here.
I have an assignment about 2D array :( and its too complicated i just need help in the first function (input) and i will complete the rest..
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<iomanip>
#include<string>
usnig namespace std;
const int max=20;
const int n=3;
int input(int [][n], string[], float &);
int main()
{
string names[max];
int marks[max][N];
int classSize;
classSize=input(marks,names,avg);
input(marks, names, avg);
cout<<"The average mark of all students is: " <<avg<<endl;
return 0;
}
int input(int marks[][n], string names[], float & avg)
{
ifstream fin;
int classSize;
int work[max], midterm[max], final[max], sum[max];
fin.open("marks.txt" );
fin>>classSize;
for (int i=0; i<max; i++)
{
getline(fin, names[i]);
for (int j=0; j<N; j++)
{
fin>>marks[i][j];
}
}
for (int i=0; i<max; i++)
{
fin>>work[i]>>midterm[i]>>final[i];
sum[i]=work[i]+midterm[i]+final[i];
}
avg=sum/n;
return classSize;
}
May 15, 2015 at 10:35am UTC
Your program is full of errors! And what you want to calculate is unclear...
inside
int input(int [][n], string[], float &)
function...
fin.open("marks.txt" );
this statement opens a file named "marks.txt" which is previously written.
That file contains a pattern of data and all other subsequent statements operate on that data.
Here is a reworked version of your wrong program, I don't know if this is what you want-
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<cstdlib>
using namespace std;
const int MAX=20;
const int n=3;
float avg;
int input(int [][n], string[], float &);
int main()
{
string names[MAX];
int marks[MAX][n];
int classSize;
classSize=input(marks,names,avg);
input(marks, names, avg);
cout<<"The average mark of all students is: " <<avg<<endl;
return 0;
}
int input(int marks[][n], string names[], float & avg)
{
ifstream fin;
int classSize;
int work[MAX], midterm[MAX], final[MAX], sum[MAX];
fin.open("marks.txt" );
if (!fin) // exit the program if file is unavailable
{
cout << "File does not exist.\n" ;
exit(1);
}
fin>>classSize;
for (int i=0; i<MAX; i++)
{
getline(fin, names[i]);
for (int j=0; j<n; j++)
{
fin>>marks[i][j];
}
}
for (int i=0; i<MAX; i++)
{
fin>>work[i]>>midterm[i]>>final[i];
sum[i]=work[i]+midterm[i]+final[i];
}
int totalSum = 0;
for (int i = 0; i < MAX; i++) totalSum += sum[i];
avg = (float ) (totalSum/n);
return classSize;
}
Last edited on May 15, 2015 at 10:51am UTC
May 15, 2015 at 3:13pm UTC
Thanks alot, but still facing the same problem, the average is a huge number, i did same as what you did but nothing worked..
Topic archived. No new replies allowed.