简体   繁体   English

数组在执行代码时不打印任何内容

[英]Array doesn't print anything on executing code

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.我正在创建一个 function ,它采用 integer 数字,在数字之后找到 5 的下一个倍数,然后如果倍数和数字之间的差小于 3,那么它会打印出倍数,否则就是数字本身,最后打印出所有数字的数组。

 #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.现在我尝试运行这个 function,编译器在代码中显示程序没有错误,但是代码没有打印任何内容。

Someone Please help.有人请帮忙。

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:找出最接近的 5 乘法和一个数字之间的差值可以简单地通过以下方式完成:

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.说明: C++ 是向下舍入除法,因此(n + 4) / 5n / 5向上舍入,因此(n+4)/5*5是最接近 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.另一件事,您声明了一个数组但从不调整它的大小,因此它的大小为 0。您需要通过在构造函数中指定大小或使用std::vector::resize方法来调整它的大小。

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;
}

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.所以array[i]总是错误的。

Two possible solutions两种可能的解决方案

1) Make the vector the correct size to begin with 1)使向量开始时的正确大小

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. 2) 使用push_back将项目添加到向量中,每次调用push_back时,向量的大小都会增加一。

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.并且请不要调用您的向量array ,这只是在撒尿。

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.我觉得你的 function 没有问题,但是调用这个 function 有点棘手,让我给你一个快速的主要尝试可能会对你有所帮助。

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.每当您有时间必须阅读如何编写高效代码时,您的代码效率就会很低。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM