Andy Baker
Andy Baker

Reputation: 1

I was trying to find the sum and average of 3 inputs and use call by reference to do it

#code to find sum and average#

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

//function prototype

void sum_n_avg(double n1,double n2, double n3,double *sum, double *average);
int main()
{
    double s, avg, one, two,three; // declare my values

    printf("enter 3 numbers---> ");

    scanf("%lf%lf%lf",&one,&two,&three);

  sum_n_avg(&s,&avg);

       printf("the sum of the numbers is %lf\n the average of the numbers is ",s, avg);

return 0;
}

void sum_n_avg(double n1,double n2, double n3, double *sum, double *average)
{

  *sum = n1 + n2 + n3;


  *average= (*sum)/3;


}

#I get error messages and I do not know what it means or how to fix it #

 error: incompatible type for argument 1 of 'sum_n_avg' 

#error occurs when I call my function#

Upvotes: 0

Views: 572

Answers (2)

MED LDN
MED LDN

Reputation: 669

This code can help you :

void somme(double n1,double n2, double n3,double *k,double *l)
{
    *k=n1+n2+n3;
    *l=*k/3;
}

int main()
{
    double s1=0, s2=0, one=0, two=0,three=0; 
    
    printf("enter 3 numbers---> ");

    scanf("%lf%lf%lf",&one,&two,&three);

    somme(one,two,three,&s1,&s2);
    //s1=one+two+three;
    //s2=s1/3; 

    printf("\nthe sum of the numbers is %f and the average of the numbers is %f\n",s1, s2);
return 0;
}

Upvotes: 0

Tommy
Tommy

Reputation: 100642

You've declared sum_n_avg to take three floats followed by two pointers to float.

You're calling it with two pointers to floats — the incompatible type for argument 1 (a pointer to float rather than an actual float) should be just one of the errors you receive.

You probably wanted:

sum_n_avg(one, two, three, &s, &avg);

Upvotes: 2

Related Questions