对于我的学校项目,我应该用(大约)-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"
提前感谢所有回复的人。我真的很感激。
最佳答案
你没有把数字放在正确的 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;
}
关于C++ : Bizarre Numbers Appearing in Output,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28378109/