Pulling info out of a user input array.

I think I have most of this fairly close. I am trying to pull out the average, which I am getting, as one of the three things I am trying to do. The median and getting the printed list are not working out as well as I had hoped and I am not sure what to do to fix it. I think basically I have an issue with trying to use the counter to return the info but, am not sure exactly how to change it. I am sure it is simple but, I need some help.

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
 #include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

//*****************************************Constants*********************************************;

const int size = 100;

//**************************************Function Prototypes**************************************;

int mean(int Grades[], int counter);
int median(int Grades[], int counter);

//******************************************Program Main*****************************************;
int main()
{

	int Grades[100];
	int counter = 0;
	char input;

//                          Getting the number of grades and grades to be entred                *;
	do{
		cout << "Please enter the grade.\n";
		cin >> Grades[counter];
		if (Grades[counter] <0 || Grades[counter] >100){
			cout << "Please make sure the value entered is not less than 0 or greater than 100.\n";
			cin >> Grades[counter];
		}
		cout << "Do you wish to enter another grade? Y or N?\n";
		cin >> input;
		counter++;
	}while (counter < 100 && toupper(input) == 'Y');

	if (counter == 100){
		cout << "You have reached the maximum number of grades you can input.\n";
	}

	cout << "The average of the grades you have entered is " << mean(Grades, counter) << "." << endl;
	cout << "The median grade of the grades you entered are " << median(Grades, counter) << "." << endl;

	for (int i = 0; i < counter; ++i){
		for (int j = 0; j < counter-1; ++j)
			cout << counter << " ";
		if (i % 5 == 0){
			cout << endl;
		}
	}


	cin.ignore();
	return 0;
}
//*************************************End of Main***********************************************;
//***********************************Begin Functions*********************************************;

//*************Function to return mean value for the number of Grades to input*******************;

int mean(int Grades[], int counter){
	int sum = 0;
	int average;
	for (int i = 0; i < counter; i++)
	{
		sum += Grades[i];
	}
	 return average = sum / counter;
}
//************************Function to find the median grade/grades*******************************;

int median(int Grades[], int counter){

	for (int i = 0; i < counter; ++i)
		for (int j = 0; j < counter-1; ++j)
		if (Grades[i] > Grades[i+1]){
			swap(Grades[i], Grades[i+1]);
			}
		return (counter);
		}


Last edited on
mean() is correct. At worst, you're getting incorrect division semantics from your usage of int. Dividing an int by another int will get you a truncated value. E.g. 1/2 is 0, not 0.5.
Sorry, I meant median is not working. Thanks for the help with the mean. I can fix that quick enough. Sorry to have wasted your time on that.
Here ya go

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
#include <iostream>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;
int median(vector<int> grades);
int average(vector<int> grades);

int main()
{
    vector<int> grades;
    string input;
    bool get_grades = true;
    cout << "Type \"done\" at any time to finish\n";
    while (get_grades == true)
    {

        cout << "Enter a grade: ";
        cin >> input;
        if (input == "done")
            get_grades = false;
        else
        {
            int int_input;
            stringstream(input) >> int_input;
            grades.push_back(int_input);
        }
    }
    cout << "The average of the grades you have entered is " << average(grades) << "\n";
    cout << "The median of the grades you entered is " << median(grades) << "\n";
}

int median(vector<int> grades)
{
    int median = 0;
    if (grades.size() % 2 == 0)
    {
        sort(grades.begin(), grades.end());
        median = (grades.at((grades.size()-1)/2) + grades.at(grades.size()/2))/2;
    }
    else median = grades.at(grades.size()/2);
    return median;
}

int average(vector<int> grades)
{
    int average = 0;
    for (vector<int>::iterator i = grades.begin(); i != grades.end(); ++i)
    {
        average += *i;
    }
    return average/grades.size();
}


http://www.cplusplus.com/reference/vector/vector/
Oh, I wish I could use that but, I am currently taking my first C++ class. At the moment I am taking it online. I was supposed to have this in by last night but, asked for an extension because I couldn't get anything out of it. We were given specific parameters wanted in the program to learn about building, calling and basically trying to declutter the main by using a few functions. We have not gotten to using string arrays yet, that is this week. Unfortunately there are few tutorials that they provide for multi-dimensional arrays. This will be the last class for this degree program I am taking online for a while. I have found that basically the courses require much more direct interaction for quicker question answering. This week is the last week of the course so, I will likely have one more program to ask for help with. I will post the updated code I have.

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;

//*****************************************Constants*********************************************;

const int size = 100;

//**************************************Function Prototypes**************************************;

int mean(int Grades[], int counter);
int median(int Grades[], int counter);

//******************************************Program Main*****************************************;
int main()
{

	int Grades[100];
	int counter = 0;
	char input;

	//                          Getting the number of grades and grades to be entred                *;
	do{
		cout << "Please enter the grade.\n";
		cin >> Grades[counter];
		if (Grades[counter] <0 || Grades[counter] >100){
			cout << "Please make sure the value entered is not less than 0 or greater than 100.\n";
			cin >> Grades[counter];
		}
		cout << "Do you wish to enter another grade? Y or N?\n";
		cin >> input;
		counter++;
	} while (counter < 100 && toupper(input) == 'Y');

	if (counter == 100){
		cout << "You have reached the maximum number of grades you can input.\n";
	}

	cout << "The average of the grades you have entered is " << mean(Grades, counter) << "." << endl;
	cout << "The median grade of the grades you entered are " << median(Grades, counter) << "." << endl;

	int rows = counter;
	int cols = counter;
	int Grades[20][5] = { {}, {} };
	for (int i = 0; rows < counter; ++i){
		for (int j = 0; cols < counter; ++j){
			cout << Grades[rows][cols] << " ";

		}
		cout << endl;
	}


	cin.ignore();
	return 0;
}
//*************************************End of Main***********************************************;
//***********************************Begin Functions*********************************************;

//*************Function to return mean value for the number of Grades to input*******************;

int mean(int Grades[], int counter){
	int sum = 0;
	int average;
	for (int i = 0; i < counter; i++)
	{
		sum += Grades[i];
	}
	return average = sum / counter;
}
//************************Function to find the median grade/grades*******************************;

int median(int Grades[], int counter){

	for (int i = 0; i < counter; ++i)
	for (int j = 0; j < counter - 1; ++j)
	if (Grades[i] > Grades[i + 1]){
		swap(Grades[i], Grades[i + 1]);
	}
	return (counter);
}

Thanks.
Still need a little help on this one.
Topic archived. No new replies allowed.