hi i just started c programming and i am stuck on a question

Write a void C function called largest that takes two integers passed by value and returns the larger of the two using a third integer parameter which is passed by reference.

this is what i came up so far :

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

void diff();

void diff(int num1, int num2)
{

printf("enter 2 numbers \n");
scanf("%d %d", &num1,&num2);
if(num1 > num2) {
printf("  %d is larger than %d ",num1,num2);
 }
else{
printf("  %d is larger than %d ",num2,num1);
}
}


int main()
{

im stuck on what to do next anyone can help

Edited 6 Years Ago by WaltP: Added CODE Tags

The instructions say that there must be three parameters to the function, not two. The third parameter is passed by reference.

int main()
{
   int num1, num2, result;
   num1 = 1;
   num2 = 2;
   diff(num1, num2, &result);
}

The diff() function should do nothing more than calculate the difference. Move those prompts and scanf() into main().

Edited 6 Years Ago by Ancient Dragon: n/a

this is what i got now im kinda stuck it does not annswer the question which is

Write a void C function called largest that takes two integers passed by value and returns the larger of the two using a third integer parameter which is passed by reference.

im stuck on the third parameter which should be pass by refrence

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

int diff(int a);
int diff(int a){
return (a < a);


}



int main()
{
   int num1, num2, result;
   num1 = diff(1);
   num2 = diff(2);
printf("%d is larger than %d",diff(2),diff(1));
return 0;
   
}

Edited 6 Years Ago by WaltP: Added CODE Tags again

and can you please explain pass by refrence and pass by value and whats the difference i want this to stick to my head like glue

Pass by value just means that the function receives a copy of the integer, while pass by reference means that the function receives a pointer to the integer. Your text book should explain the difference. You can also find explainations by using google.

In the diff function, the first two parameters are passed by value, while the third is passed by reference. For parameters passed by reference, any changes that diff() makes to the referenced integer will be made to the calling function's integer.

The requirements you posted want you to write a function with three parameters. You was pretty close in the code you first posted -- just needed to add that third parameter. void diff(int a, int b, int* answer)

Edited 6 Years Ago by Ancient Dragon: n/a

orry i really messed it up here i think i gave the wrong code which does not work

this is the code i have and does work

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

void largest();

void largest()
{
int num1;
int num2;
printf("enter 2 numbers \n");
scanf("%d %d", &num1,&num2);
if(num1 > num2) {
printf(" %d is larger than %d ",num1,num2);
}
else{
printf(" %d is larger than %d ",num2,num1);
}
}


int main()
{
largest();

}

my main problem is how to incorporate the third parameter to work with my code sorry just a beginner i appreciate your help by the way. i got int num 1 and 2 which are my pass by values i just dont know how to introduce the third parameter

Edited 6 Years Ago by WaltP: Added CODE Tags for a third time!

its supposed to be called largest as it says on the question as the function should work out the largest out of 2 numbers

Read on google and figure this out yourself. If you know how to pass by reference, declare a function, pass by value, subtract two ints, and use if statements to compare ints, then you can definitely solve this problem. If you can't do any of those things then read any basic C programming tutorial.

i have got it working this is what i have came up with

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

void largest();

void largest(int num1, int num2, int *larger)
{

if(num1 > num2) {
printf("  %d is larger than %d ",num1,num2);
 }
else{
printf("  %d is larger than %d ",num2,num1);
}
}



int main()

{
int num;
largest(4,5,&num);

}

but i do not really understand the code i want this to be stuck in my head and my text book does not give a good enogh example

Edited 6 Years Ago by Nick Evan: added code-tags

#include<stdio.h>

int main()
{
 int a,b;
int c= max(a,b);
printf("max value="%d",*c);
}

int max(int a,int b)
{
if(a>b)
 {return &a;
}
else
{return &b;
}
}
 

}



}

Edited 6 Years Ago by Nick Evan: added code-tags (again)

This article has been dead for over six months. Start a new discussion instead.