Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
the code does not get build
the code is to find sum and average of 2 numbers using functions

What I have tried:

C
#include<stdio.h>
#include<stdlib.h>

int main (){
           float a,b;
           printf("Enter the tow numbers whose sum and average are needed\n");
           scanf("%lf",&a);
           scanf("%lf",&b);
           printf("The sum is %f\n",sum(a,b));
           printf("The average is %f\n",average(a,b));
}
float sum(float a ,float b){
                   float sum=a+b;
                   return sum;
}
float average(float a,float b){
                         float average=(a+b)/2;
                         return average;
}
Posted
Updated 4-Jul-21 20:21pm
v2
Comments
Patrice T 4-Jul-21 11:16am    
Try to give error messages.

Quote:
the code does not get build

Means very little to us without actual error messages ... and we can't see your screen, we have no idea what compiler you are using, we can't read your mind!

But ... "%lf" as a printf format expects a pointer to a double not a float - so some compilers will complain. I'd suggest changing "float" to "double" throughout.

And you haven't declared your functions before you try to call them - and all compilers will complain about that! See here: Forward declaration - Wikipedia[^]

Do yourself a favour: look at error messages and try to work out what you did wrong from that - they tell you the line number (at the very least) that the problem was discovered on. Learning to do this yourself will save you a huge amount of time for very little effort - you will be getting compiler errors for a very, very long time! (I've be writing software for over 40 years, and I still get them!)
Relying on others to fix them for you will take considerably longer than doing it yourself, as nobody is going to respond immediately ....
 
Share this answer
 
Comments
[no name] 8-Jul-21 3:07am    
Thank u guy (who has been writing code for last 40 years) .
also I started to code 40 days ago and its the first question I have ever asked online so,
Even I can't read your mind you old fool
The major problems of your program are the missing function prototypes.
I've fixed the code for you
C
#include<stdio.h>
#include<stdlib.h>

float sum(float a, float b);
float average(float a, float b);

int main ()
{
  float a,b;
  printf("Enter the tow numbers whose sum and average are needed\n");
  scanf("%f",&a);
  scanf("%f",&b);
  printf("The sum is %f\n",sum(a,b));
  printf("The average is %f\n",average(a,b));
  return 0;
}

float sum(float a, float b)
{
  float sum = a + b;
  return sum;
}

float average(float a, float b)
{
  float average = ( a + b ) / 2;
  return average;
}
 
Share this answer
 
Comments
[no name] 8-Jul-21 3:07am    
Thank u
CPallini 8-Jul-21 3:09am    
You are welcome.

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