[英]Sorting an Array of Strings in C using functions
#include <stdio.h>
#include <string.h>
void bubble_sort_grades(char [], int);
int main(void)
{
int menuswitch=1;
int amountofstudents;
int i;
int z;
char studentinfo[100];
char fname[50][100];
char lname[50][100];
char grade[50][100];
printf("Enter Amount of Students: ");
scanf("%d ", &amountofstudents);
for (i=0;i<amountofstudents;i++)
{
fgets(studentinfo, sizeof(studentinfo), stdin);
strcpy(fname[i], strtok(studentinfo, " "));
strcpy(lname[i], strtok(NULL, " "));
strcpy(grade[i], strtok(NULL, " "));
}
while (menuswitch==1)
{
int answer;
printf("Enter 1 for Alphabetical Sort (First Name) \n");
printf("Enter 2 for Alphabetical Sort (Last Name) \n");
printf("Enter 3 for Score Sort \n");
printf("Enter 0 to exit \n");
printf("Enter choice now: ");
scanf("%d", &answer);
if (answer==1)
{
bubble_sort_grades(grade,amountofstudents);
printf("%s\n", grade[0]);
printf("%s\n", grade[1]);
}
if (answer==2)
{
printf("In 2 \n");
}
if (answer==3)
{
printf("In 3 \n");
}
if (answer==0)
{
printf("Ending Program \n");
menuswitch=0;
}
}
}
void bubble_sort_grades(char grades2[], int amount)
{
int c, d , t;
for (c=0; c<(amount); c++)
{
for (d=0; d<amount-1; d++)
{
if (grades2[c]>grades2[d+1])
{
t=grades2[d+1];
grades2[d+1]=grades2[d];
grades2[d]=t;
}
}
}
}
Sorry for asking another questions but I need help on bubble sorting. 很抱歉再次提出问题,但我需要气泡排序方面的帮助。 I created a function to bubble sort the grades of students from the input. 我创建了一个功能,可以根据输入结果对学生的成绩进行泡沫排序。 Yet, when I do this I get only the first grade sorted instead of the array. 但是,当我这样做时,我只会得到第一年级而不是数组。
Input:
John Smith 86
Victor Jones 76
Output: 68 76 输出:68 76
You have two main problems here. 您在这里有两个主要问题。
Problem 1: Incorrect array indexing 问题1:不正确的数组索引
As @TWhite already pointed out, the parameter for your bubble-sort function has the wrong type. 正如@TWhite已经指出的那样,您的冒泡排序函数的参数类型错误。 You've declared your arrays to be of type char[50][100]
, which means it allocates 50*100 characters as a single large block in memory. 您已将数组声明为char[50][100]
,这意味着它将分配50 * 100个字符作为内存中的单个大块。 If your memory for grade
is allocated at baseAddr
, then grade[0]
is at baseAddr+0
, grade[1]
is at baseAddr+100
, grade[2]
is at baseAddr+200
, etc. If you don't tell bubble_sort_grades
the last dimension of your 2D-array, then it has no way calculate these indices. 如果您的grade
内存分配在baseAddr
,那么grade[0]
在baseAddr+0
, grade[1]
在baseAddr+100
, grade[2]
在baseAddr+200
,等等。如果您不告诉bubble_sort_grades
二维数组的最后一个维度,则无法计算这些索引。 Changing the signature of bubble_sort_grades
to void bubble_sort_grades(char[][100], int)
would fix that problem. 将bubble_sort_grades
的签名更改为void bubble_sort_grades(char[][100], int)
将解决该问题。
Problem 2: You're storing c-strings but treating them like ints 问题2:您正在存储C字符串,但将它们视为整数
The grade
array is an array of c-strings ( char*
). grade
数组是c字符串( char*
)的数组。 It stores characters, not ints. 它存储字符,而不是整数。 That means that this line is totally wrong: if (grades2[c]>grades2[d+1])
( Side note: Notice that you're using c
instead of d
as the first index, which is also an error ). 这意味着这行是完全错误的: if (grades2[c]>grades2[d+1])
( 注意:请注意,您使用c
而不是d
作为第一个索引,这也是一个错误 )。 If you want to compare strings, you should instead use strcmp
, since comparing two char*
values will with the >
operator just do a pointer comparison. 如果要比较字符串,则应改用strcmp
,因为比较两个char*
值将使用>
运算符进行指针比较。 However, using strcmp
requires that all the grades be 2 digits (eg 05
instead of 5
), otherwise the string "9"
will be greater than "80"
. 但是,使用strcmp
要求所有成绩均为2位数字(例如05
代替5
),否则字符串"9"
将大于"80"
。 Since the grades are c-strings, that also means t=grades2[d+1]
is totally incorrect since you're storing a char*
into an int
. 由于等级是c字符串,因此这意味着t=grades2[d+1]
完全不正确,因为您将char*
存储到int
。 You'd need to create a temporary buffer char t[100]
, then use strcpy
instead of copying things around by assignment. 您需要创建一个临时缓冲区char t[100]
,然后使用strcpy
而不是通过赋值来复制内容。
I like @chux's suggestion of using a struct
. 我喜欢@chux关于使用struct
的建议。 Using structs has the added benefit of automatically (correctly) handling copying the whole struct when you use the =
operator. 使用结构具有附加的好处,当您使用=
运算符时,可以自动(正确)处理复制整个结构。 I was going to suggest something similar, and actually suggest using the built-in qsort
routine, but I realize this is probably homework and you may not have covered structs yet. 我打算提出类似的建议,并实际上建议使用内置的qsort
例程,但是我意识到这可能是家庭作业,您可能还没有介绍结构。 In that case, it's probably easier to just change your grade
array to store ints instead of c-strings. 在这种情况下,仅更改grade
数组以存储int而不是c字符串可能会更容易。
A number of issues 一些问题
Instead of 代替
char fname[50][100];
char lname[50][100];
char grade[50][100];
Use structure 使用结构
typedef struct {
char fname[100];
char lname[100];
char grade[100];
} Student_t;
Student_t Student[50];
Inside bubble_sort_grades()
, use strcmp()
to compare names. 在bubble_sort_grades()
内部,使用strcmp()
比较名称。
void bubble_sort_grades(Student_t Student[], int amount) {
int c, d;
for (c = 0; c < (amount); c++) {
for (d = 0; d < amount - 1; d++) {
if (strcmp(Student[d].grade, Student[d+1].grade) > 0) {
Student_t t;
t = Student[d];
Student[d + 1] = Student[d];
Student[d] = t;
}
}
}
}
There area other significant issues, but this should get the OP going. 还有其他一些重大问题,但这应该可以推动运营。
One issue is your : 一个问题是您:
void bubble_sort_grades(char [], int);
Should be changed to : 应更改为:
void bubble_sort_grades(char *[], int);
Consider using char *[]
for all your arrays. 考虑对所有数组使用char *[]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.