3

I'm working on a project for school. This is the case:

You should be able to input weights for n number of students. Calculate average weight of students and output how many students are weighting less than 65 kg's.

By now I have this sample of C++ source:

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int number_of_students;

    cout << "How many students would you like to add?: ";
    cin >> number_of_students;
    cout << endl;
    cout << endl;

    cout << "--------------------------------------------" << endl;
    cout << "---------- ENTER STUDENT'S WEIGHT ----------" << endl;
    cout << "--------------------------------------------" << endl;
    cout << endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}

Which is basically nothing because I'm stucked currently. I do not know who can I add for example 6 new variables of weight when the user inputs for example 6 students.

EDIT:

I can do the average weight calculation and find how many students weigh less than 65 kg's. Only I'm stucked at defining number of variables of how many students will be added. Calculate average weight of students and output how many students are weighting less than 65 kg's.

4 Answers4

6

You need to store the weights in some kind of container with variable size. It's very much recommended to use containers from the standard library, the most typical choice is std::vector.

#include<vector>
#include<algorithm>  //contains std::accumulate, for calculating the averaging-sum

int main(int argc, char *argv[])
{
    int number_of_students;

    cout << "How many students would you like to add?: ";
    cin >> number_of_students;
    cout << endl;
    cout << endl;

    cout << "--------------------------------------------" << endl;
    cout << "---------- ENTER STUDENT'S WEIGHT ----------" << endl;
    cout << "--------------------------------------------" << endl;
    cout << endl;

    std::vector<float> weights(number_of_students);

    for(int i=0; i<number_of_students; ++i) {
      cin >> weights[i];
    }

    cout << "Average is: " << std::accumulate(weights.begin(), weights.end(), 0.f)
                                      / number_of_students
      << std::endl;

    return EXIT_SUCCESS;
}
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
6

You can use one variable in cycle. Example:

for (int i = 0; i < number_of_students; i++) {
    int weight;
    cin >> weight;
    if (weight < 65)
        result++;
}
imslavko
  • 6,596
  • 2
  • 34
  • 43
  • 2
    This is actually more efficient (+1) than storing all the weights and only then calculating the average, but for more complicated problems it's often more error-prone and the overhead of the more functional approach usually doesn't matter. – leftaroundabout Nov 05 '12 at 00:04
  • 1
    I just think if it is your school work and you did not learn arrays yet, you are supposed to use local variable in loop – imslavko Nov 05 '12 at 00:15
1

Use the new operator to create an array of integers.

cin >> number_of_students;
int* x = new int[number_of_students];

You now have a array of size=number_of_students. Use it to store weights.


Edit Dealing with it this way, isn't really the best (dealing with memory leaks and the like). Do pay heed to the comments and the other answers, esp. the one that uses no intermediate storage and the std::vector based solution.

Community
  • 1
  • 1
Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
  • I come from a JavaScript and PHP background... this is like an Array? –  Nov 04 '12 at 23:56
  • 1
    just remember that where you use new[...] you will need a delete[] somewhere else to ensure there a no memory leaks. – doron Nov 05 '12 at 00:45
  • 1
    Yeah, and really... just don't do it this way, @Zlatan. Standard containers are so much more safe, reliable, easy... using manually allocated arrays has very seldom any real advantages these days, it just gives you one more thing to worry about. – leftaroundabout Nov 05 '12 at 00:55
1

maybe not getting the question, but you can craete an array of a certain length like

int* a = new int[number_of_students];

for (int i = 0; i < number_of_students; i++) {
    cin >> a[i];
}

Hope it helps and good luck...