为什么这段代码不起作用?我必须填充数组。 [英] Why doesn't this code work? I have to fill the array.

查看:76
本文介绍了为什么这段代码不起作用?我必须填充数组。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
using namespace std;

const int CAPACITY=20;

void displayArray(int array[], int numElements);

void fill_array (int array[], int CAPACITY, int &numElements);

int main ()
{
    int NumArray[CAPACITY];	// an int array with a given CAPACITY
    int numElements=0;  // the array is initially empty, i.e., contains 0 elements
    
    fill_array(numArray[], numElements);
    
    return 0;

}

void fill_array (int NumArray[], int &numElements)
{
    cout << "Enter a list up to 20 integers or -1 to end the list" << endl;
    for (numElements=0; numElements<CAPACITY;numElements++)
    {
        cin >> NumArray[numElements];
    }
    
}

void displayArray(int array[], int numElements)
{
    for (int i = 0; i < numElements; i++)
        cout << array[i] << " ";
        cout << endl;
}





我的尝试:



示例输出:

输入最多20个整数的列表或-1以结束列表

3 2 4 8 12 5 6 9 0 11 10 13 -1

3 2 4 8 12 5 6 9 0 11 10 13

输入一个值和要插入的位置:18 5

3 2 4 8 12 18 5 6 9 0 11 10 13

输入要从数组中删除的值:18

3 2 4 8 12 5 6 9 0 11 10 13

输入要追加的值:15

3 2 4 8 12 5 6 9 0 11 10 13 15





我必须在代码中添加所有这些部分:

从最简单的函数开始,例如,搜索数组内容的函数。 br />
建议的函数编写顺序是:

函数搜索数组中的值

函数用正整数填充数组

从数组中删除元素的功能:删除后,数组应该没有空洞。

要插入的函数数组元素

编写一个函数,测试它以确保它真的有效,然后转到下一个函数。

写评论1)记录你的算法和设计,2)使你的代码可读,3)调试代码。



What I have tried:

Example Output:
Enter a list of up to 20 integers or -1 to end the list
3 2 4 8 12 5 6 9 0 11 10 13 -1
3 2 4 8 12 5 6 9 0 11 10 13
Enter a value and a position to insert: 18 5
3 2 4 8 12 18 5 6 9 0 11 10 13
Enter a value to delete from the array: 18
3 2 4 8 12 5 6 9 0 11 10 13
Enter a value to append: 15
3 2 4 8 12 5 6 9 0 11 10 13 15


I have to add all this part in the code:
Start with the simiplest function, e.g., the one to search the content of an array.
The suggested order to write the function is:
Function to search for a value in the array
Function to fill an array with positive integers
Function to delete an element from the array: after deletion, the array should have no holes.
Function to insert an element into the array
Write one function, test it to make sure it really works, and then move on to next function.
Write comments to 1) document your algorithms and design, 2) make your code readable, 3) debug code.

推荐答案

Quote:

为什么这段代码不起作用?我必须填充数组。

Why doesn't this code work? I have to fill the array.



是什么让你认为这段代码不起作用?



你的代码做什么没有按照你期望的方式行事,或者你不明白为什么!



有一个几乎通用的解决方案:逐步在调试器上运行代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有不知道你的代码应该做什么,它没有发现错误,只是通过向你展示发生了什么来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]

1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]



调试器仅显示您的代码正在执行的操作,您的任务是与应该执行的操作进行比较。


What makes you think that this code don't work ?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


尝试:

Try:
#include <iostream>
using namespace std;

const int CAPACITY=20;

void displayArray(int array[], int numElements);

void fill_array (int array[], int &numElements);

int main ()
{
    int NumArray[CAPACITY]; // an int array with a given CAPACITY
    int numElements=0;  // the array is initially empty, i.e., contains 0 elements

    fill_array(NumArray, numElements);
    displayArray(NumArray, numElements);

    return 0;

}

void fill_array (int NumArray[], int &numElements)
{
    cout << "Enter a list up to 20 integers or -1 to end the list" << endl;
    for (numElements=0; numElements<CAPACITY;numElements++)
    {
        cin >> NumArray[numElements];
        if ( NumArray[numElements] == -1 ) break;
    }

}

void displayArray(int array[], int numElements)
{
    for (int i = 0; i < numElements; i++)
        cout << array[i] << " ";
        cout << endl;
}





请注意:

  • 您的命名方案不一致。
  • 你没有理由使用out参数(你可以使用函数返回值)
  • 使用全局变量不好

  • 这篇关于为什么这段代码不起作用?我必须填充数组。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆