损坏的冒泡排序程序错误。更新代码
Broken bubblesort program errors. UPDATED CODE
所以我一直纠结于如何正确调试这个程序并拥有它运行。任何人都可以摆脱一些见解。假设先对数组或名称进行排序,然后对年龄数组进行排序。
prog.c: In function 'main':
prog.c:24:22: warning: passing argument 1 of 'bubblesortname' from incompatible pointer type
bubblesortname(fullname,age,SIZE);
^
prog.c:9:8: note: expected 'char **' but argument is of type 'char (*)[25]'
void bubblesortname(char *fullname[], int *age, int size);
任何能得到这个的人都可以免费得到 cookie 运行 :)
**
#define SIZE 5
#include <stdio.h>
#include <string.h>
#include <stdio.h>
void input(char fullname[][25], int age[]);
void output(char fullname[][25], int age[]);
void bubblesortname(char fullname[][25], int *age, int size);
void bubblesortage(char fullname[], int *age, int size);
int main(int argc, char *argv[])
{
char fullname[SIZE][25];
int age[SIZE];
// promt user for names and ages
input(fullname, age);
//output unsorted names and ages
output(fullname, age);
bubblesortname(fullname,age,SIZE);
output(fullname, age);
//sorts age
bubblesortage(fullname,age,SIZE);
//
output(fullname, age);
return 0;
}
void input(char fullname[][25], int age[])
{
int i;
for (i = 0; i < SIZE; i++)
{
fflush(stdin);
printf("Enter a full name\n");
//scanf("%[\^n]\n", fullname[i]);
fgets (fullname[i],40, stdin);
printf("Enter the age\n");
scanf("%d", &age[i]);
}
}
void output(char fullname[][25], int age[])
{
int i;
for (i = 0; i < SIZE; i++)
printf("%s, %d\n", fullname[i], age[i]);
}//end function
void bubblesortname(char fullname[][], int *age, int size)
{
int temp_age;
char* temp_name;
int j,i;
for (i = 0; i < SIZE - 1; ++i)
{
for (j = 0; i < SIZE - 1; ++j)
{
if (strcmp(fullname[j], fullname[j + 1]) > 0)
{
temp_age = age[i];
age[j] = age[j + 1];
age[j + 1] = temp_age;
temp_name = fullname[j];
fullname[j] = fullname[j + 1];
fullname[j + 1] = temp_name;
}//end if
}//end inner for
}//end for
}//end function
bubblesortage(char fullname[][], int *age, int size)
{
int j,i;
int temp_age;
char* temp_name;
char temp[25];
for (i = 0; i < size - 1; ++i)
{
for (j = 0; j < size - 1; ++j)
{
if (age[j] > age[j + 1])
{
strcpy(temp, fullname[<index1>]);
strcpy(fullname[index1], fullname[index2]);
strcpy(fullname[index2], temp);
temp_age = age[j];
age[j] = age[j + 1];
age[j + 1] = temp_age;
temp_name = fullname[j];
fullname[j] = fullname[j + 1];
fullname[j + 1] = temp_name;
}// end inner for
}// end outer for
}// end function
你的程序有几个问题:
首先,您的函数原型与您稍后提供的函数不匹配:
void input(char fullname[25], int age[]);
不匹配
void input(char fullname[][25], int age[])
void output(char fullname[25], int age[]);
不匹配
void output(char fullname[][25], int age[])
void bubblesortname(char *fullname[][25], int *age, int size);
不匹配
void bubblesortname(char *fullname[], int *age, int size)
最后
void bubblesortage(char *fullname[], int *age, int size);
和
bubblesortage(char *fullname[], int *ages, int size)
两者都不匹配(注意缺失的 return 类型,默认为 int
)。
您需要修复所有这些才能匹配。
接下来,您需要决定您想要排序的内容,或者 - 更具体地说 - 如果您认识到必须交换两个字符串,您想要移动哪些数据。
您最初设置数据结构的方式(一个大型二维数组)意味着您想要对全长的单个字符串进行排序,而不是对指向字符串的指针进行排序(这会更有效,因为您会仅交换两个指针而不是 2 x 26 字节)。
一个二维数组只是一个内存区域,由行 x 列 x sizeof(element) 组成,因此没有指向交换的指针,只有数据。
唉,你决定了,你决定了完整的字符串。
为此,您需要一个 26 字节(25 个字符 + 尾随 '[=19=]'
)的临时字段 bubblesortname()
函数:
char temp[25];
一旦发现需要交换两个字符串,请执行以下操作:
strcpy(temp, fullname[<index1>]);
strcpy(fullname[index1], fullname[index2]);
strcpy(fullname[index2], temp);
(别忘了交换您的年龄值,以保持一切同步)。
你自己解决这个问题,你就可以保留你的 cookies ;)
给我该死的饼干:)
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#define SIZE 5
void input (char fullname[][25], int age[]);
void output (char fullname[][25], int age[]);
void bubblesortname (char fullname[][25], int *age, int size);
void bubblesortage (char fullname[][25], int *age, int size);
void fflush_stdin();
int main (void)
{
char fullname[SIZE][25];
int age[SIZE];
// promt user for names and ages
input (fullname, age);
//output unsorted names and ages
printf ("\n input provided:\n\n");
output (fullname, age);
// sorts by name
bubblesortname (fullname, age, SIZE);
printf ("\n sorted by name:\n\n");
output (fullname, age);
//sorts age
bubblesortage (fullname, age, SIZE);
printf ("\n sorted by age:\n\n");
output (fullname, age);
return 0;
}
void input (char fullname[][25], int age[])
{
int i = 0;
size_t nchr = 0;
for (i = 0; i < SIZE; i++) {
printf ("\nEnter a full name: ");
if (fgets (fullname[i], 24, stdin) != NULL)
{
nchr = strlen (fullname[i]);
while (nchr > 0 && (fullname[i][nchr -1] == '\n' || fullname[i][nchr -1] == '\r'))
fullname[i][--nchr] = 0;
}
printf ("Enter the age : ");
scanf ("%d", &age[i]);
fflush_stdin();
}
}
void output (char fullname[][25], int age[])
{
int i;
for (i = 0; i < SIZE; i++)
printf (" %-30s, %d\n", fullname[i], age[i]);
} //end function
void bubblesortname (char fullname[][25], int *age, int size)
{
int j = 0, i = 0;
int temp_age = 0;
char temp_name[25] = {0};
for (i = 0; i < size - 1; ++i) {
// for (j = 0; i < size - 1; ++j) {
for (j = 0; j < size - 1 - i; ++j) {
if (strcmp (fullname[j], fullname[j + 1]) > 0) {
temp_age = age[j + 1];
age[j + 1] = age[j];
age[j] = temp_age;
strcpy (temp_name, fullname[j + 1]);
strcpy (fullname[j + 1], fullname[j]);
strcpy (fullname[j], temp_name);
} //end if
} //end inner for
} //end for
} //end function
void bubblesortage (char fullname[][25], int *age, int size)
{
int j = 0, i = 0;
int temp_age = 0;
char temp_name[25] = {0};
for (i = 0; i < size - 1; ++i) {
// for (j = 0; j < size - 1; ++j) {
for (j = 0; j < size - 1 - i; ++j) {
if (age[j] > age[j + 1]) {
temp_age = age[j + 1];
age[j + 1] = age[j];
age[j] = temp_age;
strcpy (temp_name, fullname[j + 1]);
strcpy (fullname[j + 1], fullname[j]);
strcpy (fullname[j], temp_name);
} // end inner for
} // end outer for
} // end function
}
void fflush_stdin()
{ int c; while ((c = getchar()) != '\n' && c != EOF); }
输出
$ ./bin/freecookies
Enter a full name: George Carver
Enter the age : 143
Enter a full name: Albert Einstein
Enter the age : 115
Enter a full name: Ma Ferguson
Enter the age : 131
Enter a full name: George Charles Butte
Enter the age : 116
Enter a full name: Alexander Hamilton
Enter the age : 277
input provided:
George Carver , 143
Albert Einstein , 115
Ma Ferguson , 131
George Charles Butte , 116
Alexander Hamilton , 277
sorted by name:
Albert Einstein , 115
Alexander Hamilton , 277
George Carver , 143
George Charles Butte , 116
Ma Ferguson , 131
sorted by age:
Albert Einstein , 115
George Charles Butte , 116
Ma Ferguson , 131
George Carver , 143
Alexander Hamilton , 277
严重的是,您遇到的问题是 (1) 您在冒泡排序上的索引完全错误,(2) 您不能将字符串相互分配,您必须逐个复制字符串,以及 (3) fflush(stdin)
永远不会正确导致 未定义的行为 。
所以我一直纠结于如何正确调试这个程序并拥有它运行。任何人都可以摆脱一些见解。假设先对数组或名称进行排序,然后对年龄数组进行排序。
prog.c: In function 'main':
prog.c:24:22: warning: passing argument 1 of 'bubblesortname' from incompatible pointer type
bubblesortname(fullname,age,SIZE);
^
prog.c:9:8: note: expected 'char **' but argument is of type 'char (*)[25]'
void bubblesortname(char *fullname[], int *age, int size);
任何能得到这个的人都可以免费得到 cookie 运行 :) **
#define SIZE 5
#include <stdio.h>
#include <string.h>
#include <stdio.h>
void input(char fullname[][25], int age[]);
void output(char fullname[][25], int age[]);
void bubblesortname(char fullname[][25], int *age, int size);
void bubblesortage(char fullname[], int *age, int size);
int main(int argc, char *argv[])
{
char fullname[SIZE][25];
int age[SIZE];
// promt user for names and ages
input(fullname, age);
//output unsorted names and ages
output(fullname, age);
bubblesortname(fullname,age,SIZE);
output(fullname, age);
//sorts age
bubblesortage(fullname,age,SIZE);
//
output(fullname, age);
return 0;
}
void input(char fullname[][25], int age[])
{
int i;
for (i = 0; i < SIZE; i++)
{
fflush(stdin);
printf("Enter a full name\n");
//scanf("%[\^n]\n", fullname[i]);
fgets (fullname[i],40, stdin);
printf("Enter the age\n");
scanf("%d", &age[i]);
}
}
void output(char fullname[][25], int age[])
{
int i;
for (i = 0; i < SIZE; i++)
printf("%s, %d\n", fullname[i], age[i]);
}//end function
void bubblesortname(char fullname[][], int *age, int size)
{
int temp_age;
char* temp_name;
int j,i;
for (i = 0; i < SIZE - 1; ++i)
{
for (j = 0; i < SIZE - 1; ++j)
{
if (strcmp(fullname[j], fullname[j + 1]) > 0)
{
temp_age = age[i];
age[j] = age[j + 1];
age[j + 1] = temp_age;
temp_name = fullname[j];
fullname[j] = fullname[j + 1];
fullname[j + 1] = temp_name;
}//end if
}//end inner for
}//end for
}//end function
bubblesortage(char fullname[][], int *age, int size)
{
int j,i;
int temp_age;
char* temp_name;
char temp[25];
for (i = 0; i < size - 1; ++i)
{
for (j = 0; j < size - 1; ++j)
{
if (age[j] > age[j + 1])
{
strcpy(temp, fullname[<index1>]);
strcpy(fullname[index1], fullname[index2]);
strcpy(fullname[index2], temp);
temp_age = age[j];
age[j] = age[j + 1];
age[j + 1] = temp_age;
temp_name = fullname[j];
fullname[j] = fullname[j + 1];
fullname[j + 1] = temp_name;
}// end inner for
}// end outer for
}// end function
你的程序有几个问题:
首先,您的函数原型与您稍后提供的函数不匹配:
void input(char fullname[25], int age[]);
不匹配
void input(char fullname[][25], int age[])
void output(char fullname[25], int age[]);
不匹配
void output(char fullname[][25], int age[])
void bubblesortname(char *fullname[][25], int *age, int size);
不匹配
void bubblesortname(char *fullname[], int *age, int size)
最后
void bubblesortage(char *fullname[], int *age, int size);
和
bubblesortage(char *fullname[], int *ages, int size)
两者都不匹配(注意缺失的 return 类型,默认为 int
)。
您需要修复所有这些才能匹配。
接下来,您需要决定您想要排序的内容,或者 - 更具体地说 - 如果您认识到必须交换两个字符串,您想要移动哪些数据。
您最初设置数据结构的方式(一个大型二维数组)意味着您想要对全长的单个字符串进行排序,而不是对指向字符串的指针进行排序(这会更有效,因为您会仅交换两个指针而不是 2 x 26 字节)。
一个二维数组只是一个内存区域,由行 x 列 x sizeof(element) 组成,因此没有指向交换的指针,只有数据。
唉,你决定了,你决定了完整的字符串。
为此,您需要一个 26 字节(25 个字符 + 尾随 '[=19=]'
)的临时字段 bubblesortname()
函数:
char temp[25];
一旦发现需要交换两个字符串,请执行以下操作:
strcpy(temp, fullname[<index1>]);
strcpy(fullname[index1], fullname[index2]);
strcpy(fullname[index2], temp);
(别忘了交换您的年龄值,以保持一切同步)。
你自己解决这个问题,你就可以保留你的 cookies ;)
给我该死的饼干:)
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#define SIZE 5
void input (char fullname[][25], int age[]);
void output (char fullname[][25], int age[]);
void bubblesortname (char fullname[][25], int *age, int size);
void bubblesortage (char fullname[][25], int *age, int size);
void fflush_stdin();
int main (void)
{
char fullname[SIZE][25];
int age[SIZE];
// promt user for names and ages
input (fullname, age);
//output unsorted names and ages
printf ("\n input provided:\n\n");
output (fullname, age);
// sorts by name
bubblesortname (fullname, age, SIZE);
printf ("\n sorted by name:\n\n");
output (fullname, age);
//sorts age
bubblesortage (fullname, age, SIZE);
printf ("\n sorted by age:\n\n");
output (fullname, age);
return 0;
}
void input (char fullname[][25], int age[])
{
int i = 0;
size_t nchr = 0;
for (i = 0; i < SIZE; i++) {
printf ("\nEnter a full name: ");
if (fgets (fullname[i], 24, stdin) != NULL)
{
nchr = strlen (fullname[i]);
while (nchr > 0 && (fullname[i][nchr -1] == '\n' || fullname[i][nchr -1] == '\r'))
fullname[i][--nchr] = 0;
}
printf ("Enter the age : ");
scanf ("%d", &age[i]);
fflush_stdin();
}
}
void output (char fullname[][25], int age[])
{
int i;
for (i = 0; i < SIZE; i++)
printf (" %-30s, %d\n", fullname[i], age[i]);
} //end function
void bubblesortname (char fullname[][25], int *age, int size)
{
int j = 0, i = 0;
int temp_age = 0;
char temp_name[25] = {0};
for (i = 0; i < size - 1; ++i) {
// for (j = 0; i < size - 1; ++j) {
for (j = 0; j < size - 1 - i; ++j) {
if (strcmp (fullname[j], fullname[j + 1]) > 0) {
temp_age = age[j + 1];
age[j + 1] = age[j];
age[j] = temp_age;
strcpy (temp_name, fullname[j + 1]);
strcpy (fullname[j + 1], fullname[j]);
strcpy (fullname[j], temp_name);
} //end if
} //end inner for
} //end for
} //end function
void bubblesortage (char fullname[][25], int *age, int size)
{
int j = 0, i = 0;
int temp_age = 0;
char temp_name[25] = {0};
for (i = 0; i < size - 1; ++i) {
// for (j = 0; j < size - 1; ++j) {
for (j = 0; j < size - 1 - i; ++j) {
if (age[j] > age[j + 1]) {
temp_age = age[j + 1];
age[j + 1] = age[j];
age[j] = temp_age;
strcpy (temp_name, fullname[j + 1]);
strcpy (fullname[j + 1], fullname[j]);
strcpy (fullname[j], temp_name);
} // end inner for
} // end outer for
} // end function
}
void fflush_stdin()
{ int c; while ((c = getchar()) != '\n' && c != EOF); }
输出
$ ./bin/freecookies
Enter a full name: George Carver
Enter the age : 143
Enter a full name: Albert Einstein
Enter the age : 115
Enter a full name: Ma Ferguson
Enter the age : 131
Enter a full name: George Charles Butte
Enter the age : 116
Enter a full name: Alexander Hamilton
Enter the age : 277
input provided:
George Carver , 143
Albert Einstein , 115
Ma Ferguson , 131
George Charles Butte , 116
Alexander Hamilton , 277
sorted by name:
Albert Einstein , 115
Alexander Hamilton , 277
George Carver , 143
George Charles Butte , 116
Ma Ferguson , 131
sorted by age:
Albert Einstein , 115
George Charles Butte , 116
Ma Ferguson , 131
George Carver , 143
Alexander Hamilton , 277
严重的是,您遇到的问题是 (1) 您在冒泡排序上的索引完全错误,(2) 您不能将字符串相互分配,您必须逐个复制字符串,以及 (3) fflush(stdin)
永远不会正确导致 未定义的行为 。