Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 编程新手,在扩展数组以显示零时遇到问题_C++_Arrays - Fatal编程技术网

C++ 编程新手,在扩展数组以显示零时遇到问题

C++ 编程新手,在扩展数组以显示零时遇到问题,c++,arrays,C++,Arrays,我在一个数组扩展项目中遇到了一些问题,我正试图找到让我的数组扩展为全零的问题所在。以下是要求: 阵列扩展器。程序应该有一个整数数组。它将有一个包含两个参数的函数:整数数组和数组大小。此函数将创建一个新数组,该数组的大小是arguments数组的两倍。函数应将参数数组的内容复制到新数组中,并使用0初始化第二个数组中未使用的元素。函数必须返回指向新数组的指针。然后程序将显示新数组的内容 程序必须具有以下功能 •int*expandArray(int[],int) •void showArray(in

我在一个数组扩展项目中遇到了一些问题,我正试图找到让我的数组扩展为全零的问题所在。以下是要求:

阵列扩展器。程序应该有一个整数数组。它将有一个包含两个参数的函数:整数数组和数组大小。此函数将创建一个新数组,该数组的大小是arguments数组的两倍。函数应将参数数组的内容复制到新数组中,并使用0初始化第二个数组中未使用的元素。函数必须返回指向新数组的指针。然后程序将显示新数组的内容

程序必须具有以下功能 •int*expandArray(int[],int) •void showArray(int[],int)

我正在让程序无问题地构建第一个数组,但是,第二个数组虽然显示第一个数字数组,但显示第二个数字数组。我已经看了好几个小时了,我不知道如何纠正它才能正常工作。以下是我目前掌握的代码:

//Include section
#include <iostream>  
#include <cstring>  

//Namespace Section
using namespace std;

//Function Prototype Section
int *expandArray(int[], int&);
void showArray(const int[], int);

//Main section: this is the entry point of the program, which controls the flow of execution
int main()
{
    int arraySize = 7;                  //sets the size of the array at 7
    int *array = new int[arraySize];

    for (int c = 0; c < arraySize; ++c)
        array[c] = c + 1;

    //the following outputs the initial array of 7 to the user's screen; beginning at 1 and incrementing to 7
    cout << "*********************************************************************" << endl;
    cout << "                The following is the initial array                   " << endl;
    cout << "*********************************************************************" << endl;
    cout << endl;

    showArray(array, arraySize);

    cout << endl;
    cout << endl;

    //the following outputs the initial array, plus expands the array by double, initializing the unused elements with zero
    cout << "*********************************************************************" << endl;
    cout << "                The following is the expanded array                  " << endl;
    cout << "*********************************************************************" << endl;
    cout << endl;

    showArray(array, arraySize * 2);

    cout << endl;
    cout << endl;

    delete[] array;

    system("PAUSE");  

    return 0;
}

int *expandArray(int array[], int &arraySize)
{
    int *expandedArray;

    expandedArray = new int[arraySize * 2];

    for (int index = arraySize; index < (arraySize * 2); index++)

        expandedArray[index] = 0;

        arraySize *= 2;

    delete[] array;

    return expandedArray;
}

void showArray(const int arr[], int arraySize)
{
    for (int index = 0; index < arraySize; index++)

        cout << arr[index] << " " << endl;
}
//包含节
#包括
#包括
//名称空间部分
使用名称空间std;
//功能原型部分
int*expandArray(int[],int&);
void showArray(常量int[],int);
//主要部分:这是程序的入口点,控制执行流
int main()
{
int arraySize=7;//将数组大小设置为7
int*array=newint[arraySize];
对于(int c=0;c在我看来,您忘记了在函数声明中将初始数组的内容复制到
expandedArray
中。您只将索引介于
arraySize
arraySize*2
之间的所有元素设置为0,但从未实际复制参数的值

我将包括以下内容:

for(int i=0; i<arraySize; i++) expandedArray[i] = array[i];

for(int i=0;i实际上,您的代码中有两个错误:

当您运行代码时,它会正确打印类型为
double
的数组的前7个元素,但不会打印其他7个元素。这是因为它们未初始化,因此返回的是垃圾值

因此,在函数中,必须将其他7个元素初始化为0。前7个元素和第一个矩阵也是如此

我已经纠正了问题。请仔细查看:

        //Include section
        #include <iostream>
        #include <cstring>

        //Namespace Section
        using namespace std;

        //Function Prototype Section
        int *expandArray(int[], int&);
        void showArray(const int[], int);
        int *expandedArray;
        //Main section: this is the entry point of the program, which controls the flow of execution
        int main()
        {
            int arraySize = 7;                  //sets the size of the array at 7
            int *array = new int[arraySize];

            for (int c = 0; c < arraySize; ++c)
                array[c] = c + 1;

            //the following outputs the initial array of 7 to the user's screen; beginning at 1 and incrementing to 7
            cout << "*********************************************************************" << endl;
            cout << "                The following is the initial array                   " << endl;
            cout << "*********************************************************************" << endl;
            cout << endl;

            showArray(array, arraySize);

            cout << endl;
            cout << endl;

            //the following outputs the initial array, plus expands the array by double, initializing the unused elements with zero
            cout << "*********************************************************************" << endl;
            cout << "                The following is the expanded array                  " << endl;
            cout << "*********************************************************************" << endl;
            cout << endl;

            expandArray(array, arraySize);
            showArray(expandedArray, arraySize);


            cout << endl;
            cout << endl;

            delete[] array;



            return 0;
        }

        int *expandArray(int array[], int &arraySize)
        {


            expandedArray = new int[arraySize * 2];

            for (int c = 0; c < arraySize; ++c)
                expandedArray[c] = c + 1;
            for (int index = arraySize; index < (arraySize * 2); index++)

                expandedArray[index] = 0;

                arraySize *= 2;

            delete[] array;

            return expandedArray;
        }

        void showArray(const int arr[], int arraySize)
        {
            for (int index = 0; index < arraySize; index++)

                cout << arr[index] << " " << endl;
        }
        //here is your solution bro..!!
//包含节
#包括
#包括
//名称空间部分
使用名称空间std;
//功能原型部分
int*expandArray(int[],int&);
void showArray(常量int[],int);
int*扩展阵列;
//主要部分:这是程序的入口点,控制执行流
int main()
{
int arraySize=7;//将数组大小设置为7
int*array=newint[arraySize];
对于(int c=0;cint*arrayExpander(int-arr[],int-size)
{
int*expendedArray=新int[size*2];
//将原始阵列中的元素移动到扩展阵列中
//将其余元素初始化为零
对于(int i=0;icout问题在于您没有将原始数组的内容复制到新分配的数组空间中

一个简单的解决方案是使用大括号初始化为0的
new[]
,然后将原始内容复制到新数组中。大括号初始化会将所有空间初始化为0,因此无需编写两个循环即可将新分配的空间设置为0

int *expandArray(int array[], int &arraySize)
{
    int *expandedArray;

    // allocate and initialize all entries to 0
    expandedArray = new int[arraySize * 2]{0}; 

    // copy old elements to new space
    for (int index = 0; index < arraySize; index++)
        expandedArray[index] = array[index];

    // delete old space
    delete [] array;

    // double array size
    arraySize *= 2;
    return expandedArray;
}
int*expandArray(int-array[],int&arraySize)
{
int*扩展阵列;
//将所有条目分配并初始化为0
expandedArray=newint[arraySize*2]{0};
//将旧图元复制到新空间
for(int index=0;index
问题出在哪里?呃,九十年代称为.0.o无论如何,您忘记了“函数应该将参数数组的内容复制到新数组”assignment.output的一部分应该在初始数组上展开,并用零将其加倍。例如,如果我的初始数组是:1 2 3 4 5,它应该输出1 2 3 4 5 0 0 0 0 0。但是,如果您运行该程序,它将使用初始数组进行构建,但会使用随机数字分组进行展开Baum mit Augen…您能解释一下代码我忘了这一点?当我构建它时,它似乎是输出初始参数数组,然后进行扩展,但它只是不输出为零。@Clarky7782找到将第一个数组的内容复制到第二个数组的代码部分。谢谢你提供的信息,j_beck。我对如何实现这一点有点困惑他在代码内。请您进一步解释一下好吗?欢迎来到StackOverflow,感谢您抽出时间帮助回答问题!通常在回答问题时,您要解释为什么给定的方法不能
int *arrayExpander(int arr[], int size) 
{
    int *expendedArray = new int[size * 2];

    // move elements forom original array into the expandedArray
    // initilize the rest of the elements to ZERO
    for (int i = 0; i < size * 2; i++) 
    {
        if (i < size)
        {
            // filling firt half
            expendedArray[i] = arr[i];
        }
        else
        {
            // second half of the array
            expendedArray[i] = 0;
        }
    }
    return expendedArray;
}

int main()
{
    int size = 5;
    int arr[] = { 1,2,3,4,5 };

    // Array pointer
    int *arrPtr = arrayExpander(arr, size);

    // Display
    for (int i = 0; i < size * 2; i++)
    {
        cout << arrPtr[i] << " " << flush;
    }
    return 0;
}
int *expandArray(int array[], int &arraySize)
{
    int *expandedArray;

    // allocate and initialize all entries to 0
    expandedArray = new int[arraySize * 2]{0}; 

    // copy old elements to new space
    for (int index = 0; index < arraySize; index++)
        expandedArray[index] = array[index];

    // delete old space
    delete [] array;

    // double array size
    arraySize *= 2;
    return expandedArray;
}