-3

New to c++. I want to take the input values in scoreCurrent and place them into different arrays depending on whether they're above the average or below the average. I tried looking for solutions from different websites and youtube but none worked so far. How do I do this?

I apologize if my whole code is a mess and thank you in advance.

#include <iostream>

using namespace std;

int main()
{
     //  student count
    int students, upperLimit;

    cout<<"Enter no. of students: ";
    cin>>students;

     //  upper limit
    do
    {
        cout<<"Enter the upper limit: ";
        cin>>upperLimit;
        if(upperLimit<5)
        {
            cout<<"Invalid upper limit."<<endl;
            continue;
        }
            break;
    }while(true);

     //  student scores
    int scoreCurrent, scoreTotal;
    float average=0;
    int belowAve(students), aboveAve(students);

    for(int index=1; index<=students; index++)
    {
        cout<<"Enter score for student no. "<<index<<": ";
        cin>>scoreCurrent; //  take this and place it into an array

         //  condition invalid
        if(scoreCurrent>upperLimit || scoreCurrent<0)
        {
            int current=index-1;
            cout<<"Invalid score."<<endl;
            index=current;
            scoreCurrent=0;
        }

        scoreTotal+=scoreCurrent;
        average=(float) scoreTotal/(float) students;


        if(scoreCurrent>average)
        {
            // scoreCurrent values are placed in belowAve array;
        }

        if(scoreCurrent>average)
        {
            //  scoreCurrent values are placed in aboveAve array;
        }
    }

     //  display
    cout<<"Average: "<<average<<endl;
    cout<<"Scores above or equal average: "<<belowAve<<endl;
    cout<<"\nScores below average: "<<aboveAve<<endl;

    return 0;
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
YepCode
  • 3
  • 1
  • 5
    There are no arrays in your code. _"How to place input value in arrays"_ First step: Create arrays. –  Mar 18 '21 at 14:39
  • 2
    The first things will be creating arrays.[`std::vector`](https://en.cppreference.com/w/cpp/container/vector) may be better. – MikeCAT Mar 18 '21 at 14:40
  • `belowAve` and `aboveAve` are ints, not arrays, and even if they were you don't have any code that attempts to place anything in them. I would suggest trying to make them `std::vector`s and then it's as simple as calling `push_back` on the appropriate container. Although the code for displaying a container is a bit more complicated than what you have. – Nathan Pierson Mar 18 '21 at 14:41
  • 1
    Are you sure you want to use an array?. A common solution for this type of thing in c++ is to declare a `std::vector` for storage and do a `pushback()` to append it to the back of it – infixed Mar 18 '21 at 14:58
  • Consider using C++ casts as opposed to C casts (e.g. `static_cast()` against `(float)`). – mediocrevegetable1 Mar 18 '21 at 15:00
  • There is another big point: Currently you are using a running average, e.g. 3 students with scores 5, 6, 7. After the first loop iteration the average is 5 / 3. After the second loop iteration the average is (5+6) / 3. After the third iteration the average is (5+6+7) / 3. You fix this you have to first read and store the all scores and calculate the sum of scores. Then calculate the average. Then split all students into two arrays / vectors. –  Mar 18 '21 at 15:35
  • _New to C++_ → you need a book. – Enlico Mar 18 '21 at 15:44

1 Answers1

0

My guess is that scorecurrent is an array (and index the offset in scorecurrent)? If you think that this will work

std::cin >> scoreCurrent;

then the problems are:

  1. where in scorecurrent will the input be placed (you don't use index)?
  2. (if you want to give scores for all indices) when does the input stop?

Apart from the argument that a vector is better (which it is, because you don't need to specify its size and can just push_back()) I think this solution is a very elegant way to solve your problem.

It uses a lambda function to iterate over all elements of a (fixed-sized) array. There is also another, more traditional solution where the >> operator of std::cin is overloaded for arguments of array types.

alle_meije
  • 2,424
  • 1
  • 19
  • 40