1

I was creating a function that takes an integer number, finds the next multiple of 5 after the number and then if the difference between the multiple and the number is less than 3, then it prints out the multiple else the number itself, finally prints out an array of all the numbers.

 #include <bits/stdc++.h>
 #include <iostream>

 using namespace std;


vector<int> gradingStudents(vector<int> grades) {
int size=grades.size();
int c=0;
int d;
vector<int> array;
for(int i=0;i<size;i++){

    while(grades[i]>(c*5)){
    c++;
    }
    d=c*5;

    if((d-grades[i])<3){
        array[i]=d;
    }else{
        array[i]=grades[i];
    }
    d=0;
    c=0;
}    
return array ;

Now I tried running this function, and the compiler gives shows no error in the program in the code, however the code doesn't print anything.

Someone Please help.

8
  • Vectors start out empty. Any indexing in an empty array will be out of bounds. Commented Apr 10, 2020 at 12:17
  • vector<int> array(size); . Regarding, "however the code doesn't print anything" that's not surprising, since there absolutely no IO in the posted code whatsoever, even if you make the fix I suggested. Commented Apr 10, 2020 at 12:18
  • Now I tried running this function -- How did you run this function when there is no main to start the program? Commented Apr 10, 2020 at 12:23
  • Unrelated: You seem to only use c to multiply it with 5 so why not do c += 5; instead and skip the multiplications? Commented Apr 10, 2020 at 12:23
  • @PaulMcKenzie I actually only pasted the function here, not the whole code. Commented Apr 10, 2020 at 12:28

3 Answers 3

2

First, I have to say that this code is extremely inefficient. Finding the difference between the closest muliplication of 5 and a number can be simply done by:

int difference = (n - (n + 4) / 5 * 5) - n;

Explanation: C++ is rounding down the division, so (n + 4) / 5 is n / 5 rounded up, and hence (n+4)/5*5 is the closest multiplication of 5.

Another thing, you declare an array but never resize it, so its size is 0. You need to resize it either by specifying the size in the constructor or using the std::vector::resize method.

code:

std::vector<int> gradingStudents(std::vector<int> grades) {
    std::size_t size = grades.size();
    std::vector<int> array(size);
    for (int i = 0; i < size; i++) {

        int closestMul = (grades[i] + 4) / 5 * 5;

        if (closestMul - grades[i] < 3) {
            array[i] = closestMul;
        }
        else {
            array[i] = grades[i];
        }
    }
    return array;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Proably your code is crashing, which is why it doesn't print anything. And one reason it might be crashing is your vector use is wrong.

It's very common to see beginners write code like this

vector<int> array;
for (int i=0;i<size;i++) {
    array[i] = ...;

But your vector has zero size. So array[i] is an error, always.

Two possible solutions

1) Make the vector the correct size to begin with

vector<int> array(size);
for (int i=0;i<size;i++) {
    array[i] = ...;

2) Use push_back to add items to the vector, every time you call push_back the vector increases in size by one.

vector<int> array(size);
for (int i=0;i<size;i++) {
    array.push_back(...);

And please don't call your vector array, that's just taking the piss.

Comments

1

i feel nothing is wrong with your function but calling of this function is a bit tricky let me give you a quick main to try may be that will help you.

int main() {

    vector <int> test ;
    test.push_back(1);
    test.push_back(2);
    gradingStudents(test);
    return 0;
}

Try initially the size of the vector is empty i hope you are sending something from the main . Your code is very inefficient whenever you find time must read how to write an efficient code.

1 Comment

Thanks for the suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.