multidemensional dynamic array

My instructions are to create a program that will ask for a file of test scores and average each students score then average each test score. I have got every thing down to the averages and it will average but it holds on to the numbers after each loop.

here is the number file

3 6
3.9 4.5 2.1 1.0 2.4 4.3
3.1 4.2 5.1 6.2 1.0 2.7
1.2 2.3 3.1 4.2 5.2 6.4

the first two are the student and test numbers.

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
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

typedef double* DblArrayPtr;

int main(){

	char input_file[16];
        ifstream fin;
	cout<<"Enter a file containing students test scores\n";
	cin>>input_file;

	fin.open(input_file);
	if(fin.fail()){
	cout<<"Input file opening failed!\n";
	exit(1);
	}
	
	int d1=0, d2=0;
	for(int i=0;i<1;i++)
		fin>>d1;
	for(int i=0;i<1;i++)
		fin>>d2;
	DblArrayPtr *m=new DblArrayPtr[d1];
	for(int i=0;i<d1;i++)
		m[i]= new double [d2];

	for(int i=0;i<d1;i++)
	   for(int j=0;j<d2;j++)
		fin>>m[i][j];


       		double student=0, test;

		for(int i=0; i<d1; i++){
			for(int j=0; j<d2; j++)
			student=student+m[i][j];
			cout<<student/d2;
			cout<<endl;
		}

		for(int j=0; j<d2; j++){
			for(int i=0; i<d1; i++)
			test=test+m[i][j];
			cout<<test/d1;
			cout<<endl;
		}
		



   return 0;
}
You have news with no deletes. That makes me sad.

This is C++. You want:

1
2
3
typedef std::vector<double> DoubleVec;

typedef std::vector<DoubleVec> Double2dVec;


I haven't added the deletes yet. I was just trying to get the program to work. I do not understand what you are telling me to do above. Do I put that above the main just like the other initializer? Thanks for the help.
Sorry -- I am suggesting you use vectors rather than dynamically allocated arrays to hold your data.

I am assuming you learned about "vector" before you were introduced to "new". Then again the tutorial on this site doesn't really go into C++ containers at all, which is astounding to me.

Apologies for any confusion.
No problem I just appreciate the help. I havent learned about vectors in C++. Part of the assignment is to use a multidimensional dynamic array with pointers. The whole works just when I run the program this is what I get:

Student average
3.03333
6.75
10.4833
2.73333
Test average
6.4
9.83333
13.6333
16.5
20.9667

The program works except when it comes to the last part it will add up the numbers and average but it wont start over with each vector it just keeps on adding them up but it stops at the right places. Sorry hard to explain by typing.
I am still stuck with this thing, making me go out of my mind.
I figured it out student and test needed to be initialized in side of the for loop. I also added my deletes for good measure.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
for(int i=0; i<d1; i++){
			double student=0;
			for(int j=0; j<d2; j++)
			student=student+m[i][j];
			cout<<"\nThe test average for student " <<i+1<<" is "<<student/d2;
			cout<<endl;
		}
			

		for(int j=0; j<d2; j++){
			double test=0;
			for(int i=0; i<d1; i++)
			test=test+m[i][j];
			cout<<"\nThe average of test " <<j+1<<" is "<<test/d1;
			cout<<endl;
		}
		cout<<"\n";

	for(int i=0; i<d1; i++)
		delete[] m[i];
		delete[] m;
		
Topic archived. No new replies allowed.