2015-02-07 58 views
1

对于我的学校项目,我应该用大约-10和10之间的随机数填充20的数组。然后,我必须根据它们是否为负值来组织这些数字,0或正数。我被告知要完成打印出原始数组的程序,以及新的数组。C++:出现在输出中的奇怪数字

出于某种原因,有组织的数组在某些空格中打印出随机(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" 

预先感谢您所有谁答复。对此,我真的非常感激。

+0

看起来像意外值的元素从未初始化。还应考虑完全在积极领域工作,并且仅在向用户呈现时抵消10。 – bvj 2015-02-07 03:14:52

+0

谢谢你的偏移提示。我唯一担心的是意外值在'final [10]'数组下初始化。我是否在for循环中初始化它们时出错?它似乎是正确的...... – iProgramIt 2015-02-07 03:22:42

+0

只有最终的[i]元素被设置在数字[i] <= 0的地方。这在你的输出中也很明显。如果您发表评论//数字大于0,则表示您有复制粘贴错误 – bvj 2015-02-07 03:29:21

回答

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

谢谢你的帮助!不幸的是,控制台中的输出仍然显示奇怪的数字,即使有您的更改。是否有一个原因?再次感谢。 – iProgramIt 2015-02-07 03:33:53

+0

查看我的评论下面“你的代码的另一个问题是if语句中的部分//数字大于0的拼写错误。我会让你为你自己找出一个:)” – tofi9 2015-02-07 03:36:09

+0

是的,有一个==代替>的数字大于0循环:),现在应该运行得很好。 – Roberto 2015-02-07 03:39:19

0

对于numbers阵列,final阵列的索引器应该从索引器(i)中独立增加。换句话说:

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

的另一个问题与您的代码是一款//Numbers greater than 0内的if声明一个错字。我会让你的身影,一个为自己:)

+0

感谢您的快速响应!只是一个问题,什么时候将变量'second'用于你将它放在你的代码版本中的位置? – iProgramIt 2015-02-07 03:31:05

+0

如果要跟踪分为第一组(小于0)和第二组(多于0)的数量,则只需要变量“first”和“second”。我会初始化它们到零的初始值。如果这不是要求,那么你不需要这两个变量 – tofi9 2015-02-07 03:33:42

0

你的输出越来越陌生号码,因为您的代码不完全填充阵列(它打印出那些已经对这些存储位置的内容)。

,不会产生代码(你几乎没有):

  • 你应该通过整个数组的大小(0〜20)的迭代循环for(他们三个),请记住,在这些你试图检查值的循环,所以你需要经历所有的值。
  • 根据您的计数器(first,second),而不是基于i,分配给您的最终阵列。再次,i只是用来遍历数组。这会导致您的阵列在这里出现漏洞。当您输入if报表并且仅在其各自的职位(i)中时,您只将值分配给final
  • 您的if对于数字大于零的语句不正确,它会检查是否等于。
  • 初始化firstsecond 0,而不是1

应该这样做。作为一个方面说明,你并不需要两个单独的计数器,firstsecond。你可以只使用一个,在你的第一个循环中增加它,然后在第二个循环中继续增加它,等等。

+0

谢谢你的帮助!为什么我会迭代第三个for循环?我不知道这将如何改变控制台的输出...... – iProgramIt 2015-02-07 03:37:23

+0

你仍然需要在你的'final'数组中添加大于零的值。不过,不要分配给'final [i]'。继续使用你的计数器,如'final [second ++]'或'final [first ++]',如果你第二次被抛弃的话。 – teealgo 2015-02-07 03:40:33