Home > Mobile >  iterating through an array to transfer its elements to a vector with certain conditions (c )
iterating through an array to transfer its elements to a vector with certain conditions (c )

Time:07-14

I am a Grade 10 student taking a Computer Science course over the summer and I am having trouble with my homework question.

The question asks to write code that will allow a user to enter 6 grades and sort the grades into two different vectors; one that stores passing grades and another that stores failing grades (>=60 means you pass). In the end, it wants you to print all the passing and failing grades in their respective place (passing grades on one line, failing grades on the other).

So far, my code accepts the user's input and successfully stores it into an integer array with 6 elements. However, after accepting the input, this error shows up:

terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
signal: aborted (core dumped)

Please look at the snippet below:

int userGrades[6]; 
vector <int> passingGrades; 
vector <int> failingGrades; 
  
for (int i = 0; i < 6; i  ) {
    cout << "Enter the grades of Student " << i 1 << ": "; 
    cin >> userGrades[i]; 
}

for (int x = 0; x < 6; x  ) {

    for (int y = 0; y < 6; y  ) {

        if (userGrades[x] >= 60) {
            passingGrades.at(x) = (userGrades[x]);
            break;
        }
        else {
            failingGrades.at(x) =(userGrades[x]);
            break;
        }
    }
}

int pgSize = passingGrades.size(); 
int fgSize = failingGrades.size(); 

cout << "The passing grades are: "; 
for (int a = 0; a < pgSize; a  ) {
    cout << passingGrades[a] << ", "; 
}

cout << "The failing grades are: "; 
for (int b = 0; b < fgSize; b  ) {
    cout << failingGrades[b] << ", "; 
}

CodePudding user response:

The vectors passingGrades and failingGrades have no elements, so any access to their "elements" are invalid.

You can use std::vector::push_back() to add elements to a std::vector.

Also note that the loop using y looks meaningless because the code inside the loop doesn't use y and executes break; in the first iteration.

In conclusion, the part:

for (int x = 0; x < 6; x  ) {

    for (int y = 0; y < 6; y  ) {

        if (userGrades[x] >= 60) {
            passingGrades.at(x) = (userGrades[x]);
            break; 
        }
        else { 
            failingGrades.at(x) =(userGrades[x]); 
            break; 
        } 
    }
}

should be:

for (int x = 0; x < 6; x  ) {

    if (userGrades[x] >= 60) {
        passingGrades.push_back(userGrades[x]);
    }
    else { 
        failingGrades.push_back(userGrades[x]); 
    } 
}

CodePudding user response:

These nested for loops do not make sense:

for (int x = 0; x < 6; x  ) {

    for (int y = 0; y < 6; y  ) {

        if (userGrades[x] >= 60) {
            passingGrades.at(x) = (userGrades[x]);
            break; 
        }
        else { 
            failingGrades.at(x) =(userGrades[x]); 
            break; 
        }
    }
}

For example, the index y is not used. And secondly, both vectors are initially empty, so you may not use the member function at(). Otherwise, an exception will be thrown.

It will be enough to use a range-based for loop, for example:

for ( const auto &x : userGrades )
{
    if ( x >= 60 ) 
    {
        passingGrades.emplace_back( x );
    }
    else
    {
        failingGrades.emplace_back( x );
    }
}

Here is a demonstration program.

#include <iostream>
#include <vector>

int main()
{
    const size_t N = 6;
    unsigned int userGrades[N] =
    {
        60, 50, 70, 40, 80, 30 
    } ;
    std::vector<int> passingGrades; 
    std::vector<int> failingGrades;     

    for ( const auto &x : userGrades )
    {
        if ( x >= 60 ) 
        {
            passingGrades.emplace_back( x );
        }
        else
        {
            failingGrades.emplace_back( x );
        }
    }

    if ( not passingGrades.empty() )
    {
        std::cout << "The passing grades are: "; 
        for ( const auto &x : passingGrades )
        {
            std::cout << x << ", ";
        }
        std::cout << '\n'; 
    }
    else
    {
        std::cout << "There are no passing grades\n";
    }

    if ( not failingGrades.empty() )
    {
        std::cout << "The failing grades are: "; 
        for ( const auto &x : failingGrades )
        {
            std::cout << x << ", ";
        }
        std::cout << '\n'; 
    }
    else
    {
        std::cout << "There aew no failing grades\n";
    }
}

The program output is

The passing grades are: 60, 70, 80, 
The failing grades are: 50, 40, 30, 

Or, instead of a for loop, you could use the standard std::partition_copy() algorithm. For example:

#include <iterator>
#include <algorithm>

//...

std::partition_copy( std::begin( userGrades ), std::end( userGrades ),
                     std::back_inserter( passingGrades ),
                     std::back_inserter( failingGrades ),
                     []( const auto &x )
                     {
                         return x >= 60;
                     } );

Here is a demonstration program.

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

int main()
{
    const size_t N = 6;
    unsigned int userGrades[N] =
    {
        60, 50, 70, 40, 80, 30 
    } ;
    std::vector<int> passingGrades; 
    std::vector<int> failingGrades;     

    std::partition_copy( std::begin( userGrades ), std::end( userGrades ),
                         std::back_inserter( passingGrades ),
                         std::back_inserter( failingGrades ),
                         []( const auto &x )
                         {
                             return x >= 60;
                         } );    

    if ( not passingGrades.empty() )
    {
        std::cout << "The passing grades are: "; 
        for ( const auto &x : passingGrades )
        {
            std::cout << x << ", ";
        }
        std::cout << '\n'; 
    }
    else
    {
        std::cout << "There are no passing grades\n";
    }

    if ( not failingGrades.empty() )
    {
        std::cout << "The failing grades are: "; 
        for ( const auto &x : failingGrades )
        {
            std::cout << x << ", ";
        }
        std::cout << '\n'; 
    }
    else
    {
        std::cout << "There aew no failing grades\n";
    }
}

The program output is

The passing grades are: 60, 70, 80, 
The failing grades are: 50, 40, 30, 

CodePudding user response:

Simpler Program

#include <iostream>

using namespace std;
#define PASS 60
int main(){
    int pass[6], fail[6],grades[6];
    cout<<"Please key in the grades:\n";
    int i;
    for(i=0;i<=6;i  ){
        cout<<"Please key in the grade "<<i<<" :";
        cin>>grades[i];
        cout<<"\n";
    }


    //Displaying pass and fail vectors
    cout<<"Passing grades: \n";
    int y;
    for(y=0;y<=6;y  )
    {

        if(grades[y]>=PASS)cout<<grades[y]<<"\n";
    }

    cout<<"Failed grades: \n";
    int x;
    for(x=0;x<=6;x  )
    {
        if(grades[x]<60)cout<<grades[x]<<"\n";
    }

return 0;
}
  • Related