findavg function in array

I've written this program before, but now it consists of separate functions. I'm trying to get my average function to work, this is as close as I've gotten.

any advice?

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
66
67
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
const int rows = 6;
const int cols = 6;

void fillarray(int[rows][cols]);
void showarray(int[rows][cols]);
int findavg(int);

int main()
{
	int avg;
	const int size = 36;
	int sheet[rows][cols];
 fillarray(sheet);
 showarray(sheet);
 cout << "the average is: " << endl;
 findavg(avg);
	return 0;
}
void fillarray(int thearray[rows][cols])
{
	 ifstream fin;
 fin.open("scores.txt");

	int scores;
	for (int x = 0; x < rows; x++)
	{
		for (int y = 0; y < cols; y++)
		{

		fin >> thearray[x][y];
	}
}
	fin.close();
}
void showarray(int thearray[rows][cols])
{

	for (int x = 0; x < rows; x++)
	{
		for (int y = 0; y < cols; y++)
		{
			cout << thearray[x][y] << " ";
		}
		cout << endl;
	}

}
int findAvg(int thearray[rows][cols], int size, int average)
{
	int total;

	for (int x = 0; x < rows; x++)
	{
		for (int y = 0; y < cols; y++)
		{
			total += thearray[rows][cols];
		}
	}
	average = total / ((int)size);
	return (average); 
}
Last edited on
The first problem:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int findavg(int);
...
int findAvg(int thearray[rows][cols], int size, int average)
{
	int total;

	for (int x = 0; x < rows; x++)
	{
		for (int y = 0; y < cols; y++)
		{
			total += thearray[rows][cols];
		}
	}
	average = total / ((int)size);   
	return (average); 
}


If all i want form the output of "findavg" function is the average, what perimeters would i use for the "findavg" function?

I realize that matching perimeters is important, that was a small mistake.
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
66
67
68

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
const int rows = 6;
const int cols = 6;

void fillarray(int[rows][cols]);
void showarray(int[rows][cols]);
int findavg(int[rows][cols], int, int);

int main()
{
	int avg;
	const int size = 36;
	int sheet[rows][cols];
 fillarray(sheet);
 showarray(sheet);
 cout << "the average is: " << endl;
 findavg(sheet, avg, size);
	return 0;
}
void fillarray(int thearray[rows][cols])
{
	 ifstream fin;
 fin.open("scores.txt");

	for (int x = 0; x < rows; x++)
	{
		for (int y = 0; y < cols; y++)
		{

		fin >> thearray[x][y];
	}
}
	fin.close();
}
void showarray(int thearray[rows][cols])
{

	for (int x = 0; x < rows; x++)
	{
		for (int y = 0; y < cols; y++)
		{
			cout << thearray[x][y] << " ";
		}
		cout << endl;
	}

}
int findAvg(int thearray[rows][cols], int size, int average)
{
	int total = 0;
	for (int x = 0; x < rows; x++)
	{
		for (int y = 0; y < cols; y++)
		{
			
			total += thearray[rows][cols];
		}
	}
	average = total / ((int)size);
	return (average); 
}





something else I tried


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int findAvg(int thearray[rows][cols], int size, int average) // You're taking average as  
{                                                                                                  // an argument here.
	int total = 0;
	for (int x = 0; x < rows; x++)
	{
		for (int y = 0; y < cols; y++)
		{
			
			total += thearray[rows][cols];
		}
	}
	average = total / ((int)size);    // Isn't size already an int?
  	return (average);    // And returning it here. This also looks like you are calling a 
                                       // function called return with average as an argument.
}


In your original code:
1
2
 cout << "the average is: " << endl;
 findavg(avg);  

this would not output the average even if the function worked. If you show what you had originally, I could make some suggestions as to how to break that part into a function. To be honest what you have confuses me. You included vector, but never use one also.
This is the simple way to do it, working perfectly

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
#include<vector>
#include<string>
#include<fstream>
using namespace std;

	typedef vector<int> one_d;
	vector<one_d> two_d;
int main()
{
	const int size = 36;
	int scores; int sum = 0;

	int sheet [6][6];
	ifstream fin;
	fin.open("scores.txt");
	int row = 6; int col = 6;
	for(int i = 0; i < row; i++)
	{
		two_d.push_back(one_d());
	}
	cout << "the scores are: " << endl;
	for(int i = 0; i < row; i++)
	{
		for(int j = 0; j < col; j++)
		{
			fin >> sheet[i][j];
			sum += sheet[i][j];
			two_d[i].push_back(sheet[i][j]);
		
			cout << two_d[i][j] << " ";
			
		}
		cout << endl;
	}
	cout << "the average is: " << endl;
	
	cout << sum / size << endl;

	return 0;
}




I'm sorry for the confusion. I've been using this template for a lot of experimentation which is why vector is still included. I just want to make a function that will calculate the average and is able to be called in my main function.
This is the simple way to do it

I would not call having an array and a vector of the same thing simple and I do not understand why they are 2 dimensional. All that aside, you need to decide if you want the function to print the average, which is what it looks like you want in main() or if you want it to return the average, which is how you tried to write the function. Since you are not doing anything with the average other than displaying it I would make it a void function and print the result from the function.
That way was simple to me because it's the first way I learned how to do it.
Thank you, that answer's enough for me to continue confidently.
Glad to help, but something to think about in in the future -
If each row represented a person or something like:
Bob score1 score2 ...
Jane score1 score2 ...
then you would use a 2d array to find the average of each person. Since you are only finding the average of every score it would be much simpler to use a 1d array (I use array interchangeably with vector).
Topic archived. No new replies allowed.