Home>
Doing this will result in all R10_out being zero. Could I ask the reason. include include// for exit (); include

typedef struct {
int ID [15];
char name [15] [25];
double height [15];
double weight [15];
double bmi [15];
} Prog2;

define N 15

// Function prototype declaration
int load_data (Prog2 obj);
int write_data (Prog2 obj);
void calc_bmi (Prog2 obj);
void sort_data (Prog2 obj);

int main (void) {
Prog2 stdt1 = {0};

load_data (stdt1);// Load data from file

calc_bmi (stdt1);// BMI calculation

sort_data (stdt1);// Sort array elements in descending order of BMI

write_data (stdt1);// Output data to file

return 0;
}
// Read data from file
int load_data (Prog2 obj)
{
FILE * fp;// File pointer
int i;// counter

// Open input file (read mode)
fp = fopen ("R10_in.txt", "r");
if (fp == NULL) {
printf ("Can't open 10_in.txt! \ N");
exit (1);
}

// Read data from input file
printf ("% -8s% -12s% 12s% 12s \ n", "ID",
"NAME", "HEIGHT [cm]", "WEIGHT [kg]");
i = 0;
while (fscanf (fp, "% d% s% lf% lf",&obj.ID [i], obj.name [i],&obj.height [i],
&obj.weight [i])! = EOF) {
// Screen output of read data (ID, name, height, weight)
printf ("% -8d% -12s% 12.1f% 12.1f \ n", obj.ID [i], obj.name [i], obj.height [i],
obj.weight [i]);
i ++;
}

fclose (fp);// Close input file

return 0;
}
// Write data to file
int write_data (Prog2 obj)
{
FILE * fp;// File pointer
int i;// counter

// Open output file (write mode)
fp = fopen ("R10_out.txt", "w");
if (fp == NULL) {
printf ("Can't open R10_out.txt! \ N");
exit (1);
}

// Write data to output file
for (i = 0;i fprintf (fp,
"% -8d% -12s% 10.1f% 10.1f% 10.1f \ n", obj.ID [i], obj.name [i], obj.height [i], obj.weight [i], obj. bmi [i]);
}

fclose (fp);// Close input file

return 0;
}
// Calculation of obesity
void calc_bmi (Prog2 obj)
{
int i;

for (i = 0;i // Calculation of BMI value
obj.bmi [i] = obj.weight [i]/((obj.height [i] * 0.01) * (obj.height [i] * 0.01));
}

}

void sort_data (Prog2 obj)
{
int i, j, // counter
ti;// Temporary storage variable for ID
double th, tw, to;// Variable for temporary storage
char wk [25];// Array for temporary storage

// Sort in ascending order of obesity
for (i = 0;i for (j = i + 1;j if (obj.bmi [i] // Exchange ID [i] and ID [j]
ti = obj.ID [i];
obj.ID [i] = obj.ID [j];
obj.ID [j] = ti;

// Exchange height [i] and height [j]
th = obj.height [i];
obj.height [i] = obj.height [j];
obj.height [j] = th;

// Swap weight [i] and weight [j]
tw = obj.weight [i];
obj.weight [i] = obj.weight [j];
obj.weight [j] = tw;

// Exchange bmi [i] and bmi [j]
to = obj.bmi [i];
obj.bmi [i] = obj.bmi [j];
obj.bmi [j] = to;

// Exchange name [i] and name [j]
strcpy (wk, obj.name [i]);
strcpy (obj.name [i], obj.name [j]);
strcpy (obj.name [j], wk);
}
}
}
}

About R10_in

1001 Aoki 172.8 63.1
1002 Enomoto 167.5 68.9
1003 Fujita 182.6 69.3
1004 Hayashi 178.7 55.7
1005 Ishii 170.1 70.2
1006 Kawai 179.3 83.1
1007 Kubo 169.6 56.4
1008 Murakami 165.2 68.7
1009 Nakajima 187.6 61.0
1010 Nomura 172.0 71.5
1011 Okada 180.8 73.8
1012 Sakamoto 177.4 63.7
1013 Shibata 178.2 67.3
1014 Takeda 172.9 78.1
1015 Wada 170.7 65.4

c
  • Answer # 1

    When passing std1 of main to each function, it is passed by value.
    In this case, even if the contents of obj are changed in each function, it will not be reflected in std1 of the caller.
    main side
    param = 9;
    sub (param);
    Each function side
    int sub (int p1)
    {
    P1 = P1 + 100;
    return 0;
    }
    It is the same as the param on the main side is not changed.
    load_data (&stdt1);
    Please pass the pointer as follows.
    The following is a modified version.

    #include<stdio.h>#include<stdlib.h>// for exit ();
    #include<string.h>typedef struct {
        int ID [15];
        char name [15] [25];
        double height [15];
        double weight [15];
        double bmi [15];
    } Prog2;
    #define N 15
    // Function prototype declaration
    int load_data (Prog2 * obj);
    int write_data (Prog2 * obj);
    void calc_bmi (Prog2 * obj);
    void sort_data (Prog2 * obj);
    int main (void)
    {
        Prog2 stdt1 = {0};
        load_data (&stdt1);// Load data from file
        calc_bmi (&stdt1);// BMI calculation
        sort_data (&stdt1);// Sort array elements in descending order of BMI
        write_data (&stdt1);// Output data to file
        return 0;
    }
    // Read data from file
    int load_data (Prog2 * obj)
    {
        FILE * fp;// File pointer
        int i;// counter
    // Open input file (read mode)
        fp = fopen ("R10_in.txt", "r");
        if (fp == NULL) {
            printf ("Can't open 10_in.txt! \ N");
            exit (1);
        }
    // Read data from input file
        printf ("% -8s% -12s% 12s% 12s \ n", "ID",
               "NAME", "HEIGHT [cm]", "WEIGHT [kg]");
        i = 0;
        while (fscanf
               (fp, "% d% s% lf% lf",&obj->ID [i],
     obj->name [i],
    &obj->height [i],&obj->weight [i])! = EOF) {
    // Screen output of read data (ID, name, height, weight)
            printf ("% -8d% -12s% 12.1f% 12.1f \ n", obj->ID [i],
     obj->name [i],
                   obj->height [i],
     obj->weight [i]);
            i ++;
        }
        fclose (fp);// Close input file
        return 0;
    }
    // Write data to file
    int write_data (Prog2 * obj)
    {
        FILE * fp;// File pointer
        int i;// counter
    // Open output file (write mode)
        fp = fopen ("R10_out.txt", "w");
        if (fp == NULL) {
            printf ("Can't open R10_out.txt! \ N");
            exit (1);
        }
    // Write data to output file
        for (i = 0;i<N;i ++) {
            fprintf (fp,
                    "% -8d% -12s% 10.1f% 10.1f% 10.1f \ n", obj->ID [i],
     obj->name [i],
                    obj->height [i],
     obj->weight [i],
     obj->bmi [i]);
        }
        fclose (fp);// Close input file
        return 0;
    }
    // Calculation of obesity
    void calc_bmi (Prog2 * obj)
    {
        int i;
        for (i = 0;i<N;i ++) {
    // Calculation of BMI value
            obj->bmi [i] =
                obj->weight [i]/((obj->height [i] * 0.01) *
                                 (obj->height [i] * 0.01));
        }
    }
    void sort_data (Prog2 * obj)
    {
        int i, j, // counter
         ti;// Temporary storage variable for ID
        double th, tw, to;// Variable for temporary storage
        char wk [25];// Array for temporary storage
    // Sort in ascending order of obesityfor (i = 0;i<N -1;i ++) {/ * Repeat from i = 0 (first data) to i = N-2 * /
            for (j = i + 1;j<N;j ++) {/ * Repeat from j = i + 1 to j = N-1 (last data) * /
                if (obj->bmi [i]<obj->bmi [j]) {// If bmi [i]<bmi [j]
    // Exchange ID [i] and ID [j]
                    ti = obj->ID [i];
                    obj->ID [i] = obj->ID [j];
                    obj->ID [j] = ti;
    // Exchange height [i] and height [j]
                    th = obj->height [i];
                    obj->height [i] = obj->height [j];
                    obj->height [j] = th;
    // Swap weight [i] and weight [j]
                    tw = obj->weight [i];
                    obj->weight [i] = obj->weight [j];
                    obj->weight [j] = tw;
    // Exchange bmi [i] and bmi [j]
                    to = obj->bmi [i];
                    obj->bmi [i] = obj->bmi [j];
                    obj->bmi [j] = to;
    // Exchange name [i] and name [j]
                    strcpy (wk, obj->name [i]);
                    strcpy (obj->name [i],
     obj->name [j]);
                    strcpy (obj->name [j],
     wk);
                }
            }
        }
    }

    In the future, when asking a question or presenting the source, please do as follows.
    Click

    ‘" Enter the language here
    code
    ‘’ ‘
    Characters are displayed.

    Enter the language here as C above and paste the source in ①.
    ‘’ C

    ‘’ ‘

    Then the source will be displayed in the form of a clean indent.

  • Answer # 2

    Since stdt1 in main () is all 0, so will the output.
    Note that obj in load_data and stdt1 in main () are different.
    Isn't it good to display the address for each?

    Since the arguments of each function called from main () are passed by value, it is a copy of stdt1.

  • Answer # 3

    Make the question easier to read by referring to Let's write the source code of the help.

    so. I'd like to ask you what kind of cause you are assuming, but if I say it first

    #include<stdio.h>int func (int v) {
      v = 100;
      return -1;
    }
    int main (void) {
      int val = 0;
      func (val);
      printf ("% d \ n", val);
      return 0;
    }


    I think it is the same principle that 0 is displayed in this program.