程序包含多维数组时出错

时间:2015-01-26 15:25:07

标签: c++ arrays multidimensional-array

编写此程序时出错。搜索了一段时间,但我找不到任何具有相同问题的人,使用多维数组。

该计划的目的非常平凡。制作一个数组,用随机整数填充它,显示,显示偶数,以奇怪的方式排序。再次显示。

//main.cpp
#include "header.h"
#include <iostream>

using namespace std;

int main()
{
    int arrayInt[fd][sd];
    initializeArray(arrayInt[fd][sd]);
}

//functions.cpp
#include <iostream>
#include "header.h"

using namespace std;

void displayArray(int arrayInt[][sd])
{
    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; j < 6; i++)
        {
            cout << arrayInt[i][j];
        }
        cout << "" << endl;
    }
    system("pause");
}

void initializeArray(int arrayInt[][sd])
{
    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; i < 6; i++)
        {
            arrayInt[i][j] = rand() % 101;
        }
    }
    displayArray(arrayInt[fd][sd]);
    evenArray(arrayInt[fd][sd]);
    sortArray(arrayInt[fd][sd]);
    displayArray(arrayInt[fd][sd]);
}

void sortArray(int arrayInt[][sd])
{
    int temp = 0;
    for (int i = 0; i < 9; i++)
    {
        if (i % 2 == 0)
        for (int j = 0; i < 6; i++)
        {
            while (arrayInt[i][j] > arrayInt[i][j + 1])
            {
                temp = arrayInt[i][j];
                arrayInt[i][j] = arrayInt[i][j + 1];
                arrayInt[i][j + 1] = temp;
            }
            while (arrayInt[i][j] < arrayInt[i][j - 1])
            {
                temp = arrayInt[i][j];
                arrayInt[i][j] = arrayInt[i][j - 1];
                arrayInt[i][j - 1] = temp;
            }
        }
        if (i % 2 == 1)
        for (int j = 0; i < 6; i++)
        {
            while (arrayInt[i][j] < arrayInt[i][j + 1])
            {
                temp = arrayInt[i][j];
                arrayInt[i][j] = arrayInt[i][j + 1];
                arrayInt[i][j + 1] = temp;
            }
            while (arrayInt[i][j] > arrayInt[i][j - 1])
            {
                temp = arrayInt[i][j];
                arrayInt[i][j] = arrayInt[i][j - 1];
                arrayInt[i][j - 1] = temp;
            }
        }
    }
    system("pause");
}

void evenArray(int arrayInt[][sd])
{
    int temp = 0;

    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; i < 6; i++)
        {
            if (arrayInt[i][j] % 2 == 0)
            {
                temp +=1;
                cout << arrayInt[i][j];
            }
        }
        cout << "" << endl;
    }
    system("pause");
}

//header.h
#include <iostream>
#include <string>

using namespace std;

const int fd = 8;
const int sd = 5;

void displayArray(int arrayInt [][sd]);
void initializeArray(int arrayInt [][sd]);
void sortArray(int arrayInt [][sd]);
void evenArray(int arrayInt [][sd]);

错误:

error C2664: 'void sortArray(int [][5])' : cannot convert argument 1 from 'int' to 'int [][5]'functions.cpp

error C2664: 'void initializeArray(int [][5])' : cannot convert argument 1 from 'int' to 'int [][5]' main.cpp   

error C2664: 'void evenArray(int [][5])' : cannot convert argument 1 from 
'int' to 'int [][5]' functions.cpp
error C2664: 'void displayArray(int [][5])' : cannot convert argument 1 from 'int' to 'int [][5]'functions.cpp  

error C2664: 'void displayArray(int [][5])' : cannot convert argument 1 from 'int' to 'int [][5]  'functions.cpp

2 个答案:

答案 0 :(得分:0)

array传递给函数时,必须只传递数组名称。 arrayInt[fd][sd]指的是一个元素。数组索引也从0开始。

访问最后一个元素

arrayInt[fd-1][sd-1]

所以arrayInt[fd][sd]会导致溢出。

所以改变initializeArray(arrayInt[fd][sd]);

initializeArray(arrayInt);

并更改其他此类调用,以便仅传递数组名称

答案 1 :(得分:0)

该行

initializeArray(arrayInt[fd][sd]);

没有意义,因为arrayInt[fd][sd]的类型为int;调用该函数如下。

initializeArray(arrayInt);