Click here to Skip to main content
15,354,542 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <stdio.h>
#include <math.h>


int menu (void);
int calBMI(int [], double [][2], int,double []);
int category(double);
double  avgBMI(double [],int);
int avgCategory(double);


int main (void)
{
const int numOfStudents=10, SIZE=25, NUM=2;
char students[numOfStudents][SIZE];
int age[numOfStudents];
double weightHeight[numOfStudents][NUM];
double BMI[numOfStudents];
int underweight[numOfStudents];
int normalweight[numOfStudents];
int overweight[numOfStudents];
int obese[numOfStudents];
int i,j,a=0,b=0,c=0,d=0,count=0,option;
double averageBMI;

FILE*input, *output;    // create two files
input= fopen( "in.dat", "r");
output= fopen( "out.out", "w");

 for(i=0; !feof(input)&&i<numOfStudents; i++)    // !feof means not end of file
 {
    fgets(students[i], 25, input);
    fscanf(input, "%d\t", &age[i]);
    for(j=0; j<NUM; j++)
    {
        fscanf(input, "%lf\t", &weightHeight[i][j]);
    }
 }

  calBMI(age, weightHeight, numOfStudents, BMI);

 while(count<numOfStudents)
 {
    if(BMI[count]<18.5)
    {
        underweight[a]=count;
        a++;
    }
    else if(BMI[count]<25)
    {
        normalweight[b]=count;
        b++;
    }
    else if(BMI[count]<30)
    {
        overweight[c]=count;
        c++;
    }
    else
    {
        obese[d]=count;
        d++;
    }
    count++;
 }
 // for user to choose menu
menu();
scanf( "%d", &option); // read the integer value of option

switch(option)
{
    case 1: printf( "\nList of students\n" );
            for(count=0; count<a; count++)
            {
                printf( "%d. %s\n", count+1, students[underweight[count]]);
            }
            break;
    case 2:  printf( "\nList of students\n" );
             for(count=0; count<b; count++)
             {
                printf( "%d. %s\n", count+1, students[normalweight[count]]);
             }
             break;
    case 3:  printf( "\nList of students\n" );
             for(count=0; count<c; count++)
             {
                printf( "%d. %s\n", count+1, students[overweight[count]]);
             }
             break;
    default: printf( "\nList of students\n" );
             for(count=0; count<d; count++)
             {
                printf( "%d. %s\n", count+1, students[obese[count]]);
             }
             break;
   }


    printf( "\nThe number of students for category underweight is %d\n", a);
    printf( "The number of students for category normalweight is %d\n", b);
    printf( "The number of students for category overweight is %d\n", c);
   printf( "The number of students for category obese is %d\n", d);


   averageBMI=avgBMI(BMI, numOfStudents);
   printf( "\nThe average BMI of all students is %.2f\n", averageBMI);
   avgCategory(averageBMI);

   fprintf(output, "Name\t\t\tAge\tweight(kg)\theight(m)\tBMI\tCategory\n");
   fprintf(output, "==========================\t====\t=======\t======\t======\t========\n");
   fclose(output);

   for(count=0; count<numOfStudents; count++)
   {
    output = fopen( "out.out", "a");
    fprintf(output, "%s", students[count]);
    fprintf(output, "%d\t", age[count]);
    fprintf(output, "%.2f\t\t", weightHeight[count][0]);
    fprintf(output, "%.1f\t\t", weightHeight[count][1]);
    fprintf(output, "%.2f\t", BMI[count]);
    fclose(output);
    category(BMI[count]);
   }


   output = fopen( "out.out", "a");
   fprintf(output, "\nThe number of students for category underweight is %d\n", a);
   fprintf(output, "The number of students for category normalweight is %d\n", b);
   fprintf(output, "The number of students for category overweight is %d\n", c);
   fprintf(output, "The number of students for category obese is %d\n", d);

   // display the average BMI of all students and average category
   fprintf(output, "\nThe average BMI of all students is %.2f\n", averageBMI);
   fprintf(output, "The average category is ");
   fclose(output);
   category(averageBMI);

   fclose(input);
   fclose(output);
   return 0;
   }


int calBMI(int age[], double weightHeight[10][2], int size, double BMI[])
{
int index=0;
do
{
    if(age[index]<=18)
        BMI[index]=(weightHeight[index][1]/pow(weightHeight[index][0]/100,2)*1.2);
    else
        BMI[index]=(weightHeight[index][1]/pow(weightHeight[index][0]/100,2));
    index++;
  } while(index<size);
   return 0;
  }


 int menu(void)
 {
  printf( "\t\t1. Underweight\n");
 printf( "\t\t2. Normalweight\n");
 printf( "\t\t3. Overweight\n");
 printf( "\t\t4. Obese\n");
 printf( "Please enter the category >> ");
 return 0;
  }


  double avgBMI(double BMI[], int size)
 {
int index;
double sum=0;

for(index=0; index<size; index++)
{
    sum+=BMI[index];
}
return(sum/size);
}


int avgCategory(double averageBMI)
{
if(averageBMI>0)
{
    if(averageBMI<18.5)
        printf( "The average category is underweight\n");
    else if(averageBMI<25)
        printf( "The average category is normalweight\n");
    else if(averageBMI<30)
        printf( "The average category is overweight\n");
    else
        printf( "The average category is obese\n");
}
else
    printf( "Invalid\n");
return 0;
}


int category(double BMI)
{
int index;
FILE*output;
output = fopen( "out.out", "a");

if(BMI>0)
{
    if(BMI<18.5)
        printf( "underweight\n");
    else if(BMI<25)
        printf( "normalweight\n");
    else if(BMI<30)
        printf( "overweight\n");
    else
        printf( "obese\n");
}
else
printf( "Invalid\n");
return 0;
}


What I have tried:

Do anyone have any idea regarding the error?
    The error ld returned 1 exit status and i've already check that there is no old 
     instance of program is still running.
Posted
Updated 22-Jun-21 5:20am

1 solution

In the calBMI() you call pow(), which is in libm, not in libc. You need to tell your IDE to add libm to the list of libraries linked to the executable. If you are compiling from the command line, then use
gcc myprog.c -o myprog -lm
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900