Hello my fellow programmers!
Naa napud ko diri because I'm in need of help about my program. Our instructor gave us this problem:
"Write a C program that stores the weight of babies in kilograms. The number of babies depends on the user input. Your program must also do the following;
- First, to compute & display for the average weight
- Second, to display only those babies born with normal weight (weight more than 2.3 kilos)
Format: Baby[index] = <weight>
- Third, to display the weight of the baby with the heaviest weight
- Fourth, to display the weight of the baby with the lightest weight
- Lastly, sort the list of weights in ascending order"
Then, ana among instructor nga mugamit ug functions to perform the tasks, then kato pud nga "dynamicially assign your array in the heap" - meaning katong malloc()/calloc() ug gamit sad daw mi sa sorting algorithms nga gihatag niya.
I already made a C program regarding this question. After compiling then running it, ning output siya ug "scanf : floating point not linked"...
![Sad](images/smilies/sad.gif)
Please help me!
Here's my code:
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int size(void);
float *input(int n);
void average(float [], int n);
void normal(float [], int n);
void heaviest(float [], int n);
void lightest(float [], int n);
void insertion_sort(float [], int n);
void main(void)
{
float *a;
int n;
clrscr();
n = size();
a = input(n);
average(a, n);
normal(a, n);
heaviest(a, n);
lightest(a, n);
insertion_sort(a, n);
getch();
}
int size(void)
{
int n;
printf("Enter number of babies: ");
scanf("%d", &n);
return n;
}
float *input(int n)
{
float *a;
int i;
a = (float *) malloc (sizeof(float) * n);
if (a != NULL)
{
printf("Enter weight of the %d babies in kilograms: ", n);
for (i=0;i<n;i++)
scanf("%f", &a[i]);
}
return a;
}
void average(float a[], int n)
{
int i;
float sum=0, ave;
for (i=0;i<n;i++)
sum+=a[i];
ave = sum / n;
printf("Average weight: %.1f\n", ave);
}
void normal(float a[], int n)
{
int i;
printf("Normal Babies:\n");
for (i=0;i<n;i++)
if (a[i] > 2.3)
printf("Baby[%d] = %.1f\n", i, a[i]);
}
void heaviest(float a[], int n)
{
int i;
float heaviest;
heaviest=a[0];
for (i=1;i<n;i++)
heaviest=(a[i]>heaviest)?a[i]:heaviest;
printf("Heaviest weight: %.1f\n", heaviest);
}
void lightest(float a[], int n)
{
int i;
float lightest;
lightest=a[0];
for (i=1;i<n;i++)
lightest=(a[i]<lightest)?a[i]:lightest;
printf("Lightest weight: %.1f\n", lightest);
}
void insertion_sort(float a[], int n)
{
int i, j, temp;
for (i=1;i<n;i++)
{
temp=a[i];
j=i;
while ((j>0) && (a[j-1]>temp))
{
a[j] = a[j-1];
j--;
}
a[j]=temp;
}
printf("Sorted weights in ascending order:\n");
for (i=0;i<n;i++)
printf("Baby[%d] = %f\n", i, a[i]);
}
Check lang ko ani nga thread nako time to time until tomorrow because inig Monday nani namo i-submit ang final written output. Thank you kaayo ninyo guys!