【发布时间】:2016-03-04 04:28:48
【问题描述】:
我试图让我的程序显示原始数组,然后对其进行排序并用指针显示排序后的数组,然后最终以原始状态显示数组。 到目前为止,我已经得到了要显示的原始数组和排序数组,但是第三个显示也显示为排序数组。但我需要它处于原始状态。 我认为我没有正确使用指针并且我实际上正在更改数组,如果有人可以向我解释错误在哪里,将不胜感激!
#include <stdio.h>
#include <stdlib.h>
void printArrayO(int DataArr[], int size)
{
// for loop used to print array contents 10 per line
for (int i = 0; i < size; i++)
{
if ((i % 10) == 0)
{
printf("\n");
}// end of if statement
// printing with a field size of 5
printf("%*d", 5, DataArr[i]);
}// end of for loop
printf("\n");
}// end of void print function
void bubbleSortArray(int ArrPoint[], int size)
{
double temp; // temporary variable
// for loops used to sort the numbers
for (int i = 0; i < size; i++) // check if this is right for first for loop
{
for (int j = 0; j < (size - 1) - i; j++)
{
if (ArrPoint[j] > ArrPoint[j + 1])
{
temp = ArrPoint[j];
ArrPoint[j] = ArrPoint[j + 1];
ArrPoint[j + 1] = temp;
}// end of if statement
}// end of nested for loop
}// end of for loop
}// end of void bubble sort function */
// print array function
void printArray(int ArrPoint[], int size)
{
// for loop used to print array contents 10 per line
for (int i = 0; i < size; i++)
{
// possible make a count variable and then use it to make a new line if %10 ==0
if ((i % 10) == 0)
{
printf("\n");
}// end of if statement
// printing with a field size of 5
printf("%*d", 5, ArrPoint[i]);
}// end of for loop
printf("\n");
}// end of void print function
int main()
{
// given numbers for the main array
int DataArr[150] = {71, 1899, 272, 1694, 1697, 296, 722, 12, 2726, 1899,
1374, 1541, 1923, 1904, 1083, 1462, 2981, 1929, 304, 2550,
1059, 1860, 1963, 516, 647, 1607, 590, 157, 2351, 753,
2455, 349, 79, 1634, 368, 1992, 2401, 357, 1478, 1601,
239, 365, 2453, 2283, 2432, 1223, 2739, 2487, 2714, 1391,
1972, 2805, 1504, 413, 1647, 2750, 44, 64, 934, 1008,
1429, 1427, 315, 2499, 1620, 1816, 2441, 2557, 2188, 531,
1514, 2825, 449, 265, 2064, 1022, 34, 1864, 1861, 1516,
1465, 2327, 398, 2769, 563, 194, 429, 942, 1795, 223,
2406, 780, 780, 61, 133, 195, 495, 1774, 1934, 2171,
433, 1417, 292, 324, 2929, 1597, 1470, 764, 593, 891,
679, 47, 1778, 2532, 1862, 2636, 549, 2923, 2270, 1101,
1607, 2395, 726, 1111, 892, 1988, 555, 379, 224, 298,
1660, 2203, 2385, 2159, 2574, 705, 2513, 1755, 313, 173,
148, 2449, 259, 1006, 1221, 2259, 2020, 1484, 2717, 2400};
// array of int pointers
int *Arrpoint = &DataArr;
int *Arrpoint2 = &DataArr;
// *pointer[i] to use;
printf("Array in the original order");
printf("\n");
printArrayO(DataArr[150], 150);
printf("\n");
printf("Array after being sorted \n");
bubbleSortArray(Arrpoint[150], 150);
printArray(Arrpoint[150], 150);
printf("\n");
printf("Array in original order again \n");
printArrayO(DataArr[150], 150);
return 0;
}
【问题讨论】:
-
您的代码到处都有导致未定义行为的错误。例如,
printArrayO(DataArr[150], 150);是缓冲区溢出(有效索引从 0 到 149)。无论如何,这是完全错误的,因为DataArr[i]是int,但该函数需要int *。如果您注意编译器警告并修复所有这些错误,那么其中许多错误不应该存在。建议您这样做并重新发布您的问题。 -
Arrpoint不是 int 指针数组。Arrpoint2也不是。而Arrpoint[150]是越界读取,它不是整个数组。 -
一个好的起点是启用编译器警告,然后在寻求帮助之前全部消除它们。
-
函数
printArray0和printArray是相同的。那应该警告你,DataArray和ArrPoint都包含整数。