C ++:出现在输出中的奇怪数字

时间:2015-02-07 03:06:45

标签: c++ arrays sorting random

对于我的学校项目,我应该用(大约)-10到10之间的随机数填充20个数组。然后,我必须根据它们是负数,0还是正数来组织这些数字。我被告知完成程序打印出原始数组以及新数组。

由于某种原因,有组织的数组在某些空格中打印出随机(TYPE:long)数字。我不确定为什么会这样。以下是我的代码:

#include <iostream>
#include <stdlib.h>
#include <time.h>

int main(int argc, const char * argv[]) {
// insert code here...
srand(time(NULL));
int numbers[20], final[20], first = 1, second = 1;

std::cout << "Enter 20 numbers, and without sorting, this program will take them and organize them based on positive, negative, and 0.\n\n";
for(int i = 0; i < 20; i++){
    std::cout << "Enter number " << (i+1) << ": ";
    //std::cin >> numbers[i];
    numbers[i] = (rand()%20 -10);
    std::cout << numbers[i] << std::endl;
}

//Numbers lower than 0
for(int i = 0; i < 20; i++){
    if(numbers[i] < 0){
        final[i] = numbers[i];
        first++;
    }
}
//Numbers equal to 0
for(int i = first; i < 20; i++){
    if(numbers[i] == 0){
        final[i] = numbers[i];
        second++;
    }
}
//Numbers greater than 0
for(int i = second; i < 20; i++){
    if(numbers[i] == 0){
        final[i] = numbers[i];
    }
}

std::cout << "This is your original array: ";
for(int i = 0; i < 20; i++){
    std::cout << numbers[i];
    if(i != 19)
        std::cout << ",";
    std::cout << " ";
    if(i == 19)
        std::cout << std::endl << std::endl << std::endl;
}

std::cout << "This your new, organized, array: ";
for(int i = 0; i < 20; i++){
    std::cout << final[i];
    if(i != 19)
        std::cout << ",";
    std::cout << " ";
    if(i == 19)
        std::cout << std::endl << std::endl << std::endl;
}

system("pause");
return 0;
}

我的输出是:

Enter 20 numbers, and without sorting, this program will take them and organize them based on positive, negative, and 0.

Enter number 1: -6
Enter number 2: 0
Enter number 3: -4
Enter number 4: -5
Enter number 5: 0
Enter number 6: -4
Enter number 7: -5
Enter number 8: -5
Enter number 9: -8
Enter number 10: 5
Enter number 11: 0
Enter number 12: -3
Enter number 13: 5
Enter number 14: -5
Enter number 15: 7
Enter number 16: 2
Enter number 17: 9
Enter number 18: 9
Enter number 19: 3
Enter number 20: 2
This is your original array: -6, 0, -4, -5, 0, -4, -5, -5, -8, 5, 0, -3, 5, -5, 7, 2, 9, 9, 3, 2 


This your new, organized, array: -6, 1879110449, -4, -5, 0, -4, -5, -5, -8, 1, 0, -3, 1606416384, -5, 1606423158, 32767, 1606416416, 32767, 1606416416, 32767"

提前感谢所有回复的人。我真的很感激。

3 个答案:

答案 0 :(得分:0)

final数组的索引器应该从i数组的索引器(numbers)独立增加。换句话说:

int idxFinal = 0; // determines where on the 'array' index to insert next

for(int i = first; i < 20; i++){
    if(numbers[i] < 0){
        final[idxFinal] = numbers[i];
        first++;
        idxFinal++;
    }
}

...

//Numbers equal to 0
for(int i = first; i < 20; i++){
    if(numbers[i] == 0){
        final[idxFinal++] = numbers[i];
        second++;
    }
}

您的代码的另一个问题是if部分//Numbers greater than 0语句中的拼写错误。我会让你自己想出一个:)

答案 1 :(得分:0)

您没有将数字放在正确的final数组位置,请检查现在first如何用于标记下一个元素应放在最终数组中的位置:

我还删除了未使用的second变量

#include <iostream>
#include <stdlib.h>
#include <time.h>

int main(int argc, const char * argv[]) {
// insert code here...
srand(time(NULL));
// changed first = 1 to first = 0 and eliminated second
int numbers[20], final[20], first = 0;

std::cout << "Enter 20 numbers, and without sorting, this program will take them and organize them based on positive, negative, and 0.\n\n";
for(int i = 0; i < 20; i++){
    std::cout << "Enter number " << (i+1) << ": ";
    //std::cin >> numbers[i];
    numbers[i] = (rand()%20 -10);
    std::cout << numbers[i] << std::endl;
}

//Numbers lower than 0
for(int i = 0; i < 20; i++){
    if(numbers[i] < 0){
        // now it is put in final[first] instead of final[i] 
        final[first] = numbers[i];
        first++;
    }
}
//Numbers equal to 0
//changed i to start from 0 again
for(int i = 0; i < 20; i++){
    if(numbers[i] == 0){
        final[first] = numbers[i];
        first++;
    }
}
//Numbers greater than 0
for(int i = 0; i < 20; i++){
    if(numbers[i] > 0){ // Yeah, here was the typo... replaced `==` with `>`
        final[first] = numbers[i];
        // added this increment
        first++;
    }
}

std::cout << "This is your original array: ";
for(int i = 0; i < 20; i++){
    std::cout << numbers[i];
    if(i != 19)
        std::cout << ",";
    std::cout << " ";
    if(i == 19)
        std::cout << std::endl << std::endl << std::endl;
}

std::cout << "This your new, organized, array: ";
for(int i = 0; i < 20; i++){
    std::cout << final[i];
    if(i != 19)
        std::cout << ",";
    std::cout << " ";
    if(i == 19)
        std::cout << std::endl << std::endl << std::endl;
}

system("pause");
return 0;
}

答案 2 :(得分:0)

你在输出上得到奇怪的数字,因为你的代码没有完全填满数组(它打印出已经存在于那些内存位置的内容)。

不提供代码(你几乎就在那里):

  • 你应该在整个数组大小(0到20)中迭代你的for循环(所有三个循环),记住在这些循环中你试图检查值,所以你需要经历所有他们。
  • 根据您的计数器(firstsecond)分配到最终数组,而不是基于i。同样,i仅用于遍历数组。这会导致阵列在这里有孔。您只需在输入final语句时将值分配给if,并且仅在各自的位置(i)中分配。
  • 对于大于零的数字,您的if语句不正确,它会检查等号。
  • 使用0而不是1初始化firstsecond

应该这样做。作为旁注,您实际上不需要两个单独的计数器firstsecond。您可以只使用一个,在第一个循环中递增它,然后在第二个循环中继续递增它,依此类推。