残破的冒泡程序错误。已更新code [英] Broken bubblesort program errors. UPDATED CODE

查看:153
本文介绍了残破的冒泡程序错误。已更新code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我停留在如何正确调试这个程序,让它运行。任何人都可以摆脱一些见解。它假设排序数组或名称,然后年龄的数组。

  prog.c中:在函数'主':
prog.c中:24:22:警告:从兼容的指针类型传递'bubblesortname'的参数1
       bubblesortname(全名,年龄,大小);
                      ^
prog.c中:9:8:注:应为**字符',但参数的类型为CHAR(*)[25]
       无效bubblesortname(字符*全名[],INT *年龄,INT大小);

免费饼干的人谁可以得到这个运行:)
**

 的#define SIZE 5
    #包括LT&;&stdio.h中GT;
    #包括LT&;&string.h中GT;
    #包括LT&;&stdio.h中GT;
    无效的输入(字符全名[] [25],年龄INT []);
    无效输出(CHAR全名[] [25],年龄INT []);
    无效bubblesortname(字符全名[] [25],为int *年龄,INT大小);
    无效bubblesortage(字符全名[],INT *年龄,INT大小);    INT主(INT ARGC,CHAR *的argv [])
    {
        焦炭全名[SIZE] [25];
        INT年龄[SIZE]        //为姓名和年龄PROMT用户
        输入(全名,年龄);
        //输出未排序的姓名和年龄
        输出(全名,年龄);        bubblesortname(全名,年龄,大小);        输出(全名,年龄);        //年龄排序
        bubblesortage(全名,年龄,大小);
        //
        输出(全名,年龄);
        返回0;
    }    无效的输入(字符全名[] [25],年龄INT [])
    {
        INT I;
        对于(i = 0; I<大小;我++)
        {
            fflush(标准输入);
            的printf(请输入全名\\ n);
            // scanf函数(%[\\ ^ N] \\ n,全称[I]);
            与fgets(全称[I],40,标准输入);
            的printf(请输入年龄\\ n);
            scanf函数(%d个,&安培;年龄[I]);        }
    }    无效输出(CHAR全名[] [25],年龄INT [])
    {
        INT I;
        对于(i = 0; I<大小;我++)
            的printf(%S,%d个\\ N,全称[I],年龄[I]);
    } //函数结束    无效bubblesortname(字符全名[] [],INT *年龄,INT大小)
    {
         INT temp_age;
          字符* temp_name;
          INT J,I;          对于(i = 0; I<大小 - 1 ++ I)
          {
            为(J = 0; I<大小 - 1 ++ j)条
            {
              如果(STRCMP(全称[J],全称[J + 1])0)
              {
                temp_age =年龄[I]
                年龄[J] =年龄[J + 1];
                年龄[J + 1] = temp_age;                temp_name =全称[J]。
                全称[J] =全称[J + 1];
                全称[J + 1] = temp_name;
                 }//万一            } //结束的内        } //结束了    } //函数结束            bubblesortage(CHAR全名[] [],INT *年龄,INT大小)
            {
                INT J,I;
                INT temp_age;
                字符* temp_name;
                炭温度[25];
                    对于(i = 0; I<大小 - 1 ++ I)
                      {
                         为(J = 0; J<大小 - 1 ++ j)条
                            {
                                 如果(年龄[J]>年龄[J + 1])
                                  {
                                    的strcpy(温度,全名并[d索引1>]);
                                    的strcpy(全名[索引1],全名[索引2]);
                                    的strcpy(全名[索引2],温度);
                                    temp_age =年龄[J]。
                                    年龄[J] =年龄[J + 1];
                                    年龄[J + 1] = temp_age;
                                    temp_name =全称[J]。
                                    全称[J] =全称[J + 1];
                                    全称[J + 1] = temp_name;                                    } //结束的内                            } //结束的外
                        } //函数结束


解决方案

给我我那该死的饼干:)

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdio.h中GT;#定义5号无效的输入(字符全名[] [25],年龄INT []);
无效输出(CHAR全名[] [25],年龄INT []);
无效bubblesortname(字符全名[] [25],为int *年龄,INT大小);
无效bubblesortage(字符全名[] [25],为int *年龄,INT大小);
无效fflush_stdin();INT主要(无效)
{
    焦炭全名[SIZE] [25];
    INT年龄[SIZE]    //为姓名和年龄PROMT用户
    输入(全名,年龄);    //输出未排序的姓名和年龄
    的printf(\\ n个输入提供:\\ n \\ n);
    输出(全名,年龄);    //排序按名称
    bubblesortname(全名,年龄,大小);    的printf(\\ n按名称排序:\\ n \\ n);
    输出(全名,年龄);    //年龄排序
    bubblesortage(全名,年龄,大小);    的printf(\\ n按年龄排序:\\ n \\ n);
    输出(全名,年龄);
    返回0;
}无效的输入(字符全名[] [25],年龄INT [])
{
    INT I = 0;
    为size_t NCHR = 0;
    对于(i = 0; I<大小;我++){
        的printf(\\ n输入完整的名称:);
        如果(与fgets(全称[I],24,标准输入)!= NULL)
        {
            NCH​​R = strlen的(全名[I]);
            而(NCHR大于0和放大器;及(全称[I] [-1人权中心] =='\\ n'||全名[我] [-1人权中心] =='\\ r'))
                全名[I] [ - 人权中心] = 0;
        }
        的printf(请输入年龄:);
        scanf函数(%d个,&安培;年龄[I]);
        fflush_stdin();
    }
}无效输出(CHAR全名[] [25],年龄INT [])
{
    INT I;
    对于(i = 0; I<大小;我++)
        的printf(%-30s%D \\ n,全称[I],年龄[I]);
} //函数结束无效bubblesortname(字符全名[] [25],为int *年龄,INT大小)
{
    诠释J = 0,I = 0;
    INT temp_age = 0;
    焦炭temp_name [25] = {0};    对于(i = 0; I<大小 - 1 ++ I){
        //为(J = 0; I<大小 - 1 ++ j)条{
        为(J = 0; J<大小 - 1 - 我; ++ j)条{
            如果(STRCMP(全称[J],全称[J + 1])0){
                temp_age =年龄[J + 1];
                年龄[J + 1] =年龄[J]。
                年龄[J] = temp_age;                的strcpy(temp_name,全称[J + 1]);
                的strcpy(全称[J + 1],全称[J]);
                的strcpy(全称[J],temp_name);
            } //万一
        } //结束的内
    } //结束了
} //函数结束无效bubblesortage(字符全名[] [25],为int *年龄,INT大小)
{
    诠释J = 0,I = 0;
    INT temp_age = 0;
    焦炭temp_name [25] = {0};    对于(i = 0; I<大小 - 1 ++ I){
        //为(J = 0; J<大小 - 1 ++ j)条{
        为(J = 0; J<大小 - 1 - 我; ++ j)条{
            如果(年龄[J]>年龄[J + 1]){
                temp_age =年龄[J + 1];
                年龄[J + 1] =年龄[J]。
                年龄[J] = temp_age;                的strcpy(temp_name,全称[J + 1]);
                的strcpy(全称[J + 1],全称[J]);
                的strcpy(全称[J],temp_name);
            } //结束的内
        } //结束的外
    } //函数结束
}无效fflush_stdin()
{INT℃;而((C =的getchar())='\\ n'和;!&安培;!C = EOF); }

输出

  $ ./bin/freecookies输入全名:乔治·卡弗
输入年龄:143输入全名:阿尔伯特·爱因斯坦
输入年龄:115输入全名:马弗格森
输入年龄:131输入全名:乔治·查尔斯·巴特
输入年龄:116输入全名:亚历山大·汉密尔顿
输入年龄:277 输入提供: 乔治·卡弗,143
 阿尔伯特·爱因斯坦,115
 马弗格森,131
 乔治·查尔斯·巴特,116
 亚历山大·汉密尔顿,277 按名称排序: 阿尔伯特·爱因斯坦,115
 亚历山大·汉密尔顿,277
 乔治·卡弗,143
 乔治·查尔斯·巴特,116
 马弗格森,131 按年龄排序: 阿尔伯特·爱因斯坦,115
 乔治·查尔斯·巴特,116
 马弗格森,131
 乔治·卡弗,143
 亚历山大·汉密尔顿,277

说真的,你必须是下列问题:(1)你就泡各种指标是完全错误的,(2)您不能分配字符串给对方,你必须复制字符串到字符串,以及(3) fflush(标准输入)从来都不是正确造成的未定义行为

So I am stuck on how to debug this program correctly and have it run. Can anyone shed some insight. It's suppose to sort an array or names then an array of ages.

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);

Free cookies to anyone who can get this to run :) **

    #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

解决方案

Gimme my damn 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); }

Output

$ ./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

Seriously, the issues you had were (1) your indexes on the bubble sorts were completely wrong, (2) you cannot assign strings to each other, you must copy string-to-string, and (3) fflush(stdin) is never correct resulting in Undefined Behavior.

这篇关于残破的冒泡程序错误。已更新code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆