该程序通过2D阵列抓取用户的输入。获得输入后,程序会将2D数组转换为1D数组(通过转换功能)。转换后,转换后的数组将被发送到另一个 SORT 函数,并对该1D数组中的所有内容进行排序。接下来,当排序完成其任务时,程序将调用 ReturnBackTo2D 函数将已转换和排序的数组恢复为 2D数组,最后函数 ReturnBackTo2D < / strong>应该调用输出函数来打印整个2D数组。该程序用于在每个函数( int rows,int cols )上添加两个额外参数之前正常工作,当它被设置为静态行数和列数时,但是现在编译器已停止成功编译,在 ReturnBack2D 功能中为我提供了一个错误,突出显示输出函数调用“没有用于调用输出的匹配函数。我正在运行的编译器是Apple的Xcode 6.1.1(最新版本)。
代码如下:
#include <iostream>
using namespace std;
void Output(int a[][5], int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
}
void ReturnBackTo2D(int old_array[], int rows, int cols)
{
int new_array[rows][cols];
int curr_index = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
new_array[i][j] = old_array[curr_index];
curr_index++;
}
}
Output(*new_array, rows, cols);
}
void SORT(int a[], int size, int rows, int cols)
{
int pos_min,temp;
for (int i=0; i < size-1; i++)
{
pos_min = i;
for (int j=i+1; j < size; j++)
{
if (a[j] < a[pos_min])
pos_min=j;
}
if (pos_min != i)
{
temp = a[i];
a[i] = a[pos_min];
a[pos_min] = temp;
}
}
ReturnBackTo2D(a, rows, cols);
}
void Convert(int a[][5], int size, int rows, int cols)
{
int converted[rows*cols]; int curr_index = 0;
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
{
converted[curr_index] = a[i][j];
curr_index++;
}
for (int i = 0 ; i < 25; i++)
cout<<converted[i]<<" ";
SORT(converted, rows*cols, rows, cols);
}
int main()
{
int a[5][5];
int rows, cols;
cout<<"Enter rows: ";
cin>>rows;
cout<<"Enter columns: ";
cin>>cols;
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
cin>>a[i][j];
Convert(a, rows*cols, rows, cols);
cout<<"\n\n";
return 0;
}