0

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 by WaltP: Added CODE Tags

5
Contributors
11
Replies
13
Views
7 Years
Discussion Span
Last Post by Nick Evan
Featured Replies
  • 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 … Read More

  • 1

    @sourabhtripathi: That's horrible. Really. 1. Test your code first before posting it here. 2. Use [URL="http://www.daniweb.com/forums/announcement8-3.html"]code-tags[/URL]. 3. Learn to [URL="http://en.wikipedia.org/wiki/Indent_style"]indent your code[/URL]. 4. Don't give [URL="http://www.daniweb.com/forums/announcement8-2.html"]away freebies[/URL]. 5. Read the [URL="http://www.daniweb.com/forums/faq.php?faq=daniweb_policies"]rules[/URL] and follow them. Read More

0

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 by Ancient Dragon: n/a

0

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 by WaltP: Added CODE Tags again

0

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

0

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 by Ancient Dragon: n/a

0

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 by WaltP: Added CODE Tags for a third time!

0

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

1

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.

0

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 by Nick Evan: added code-tags

-1
#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 by Nick Evan: added code-tags (again)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.