mistylove98
mistylove98

Reputation: 39

pointers in c not getting correct output

Im trying to make a program with 3 functions called getnumber(ask for number) findlargest(find largest between 2 numbers) and displaylargest that shows which one was larger. I am new to pointers and when I enter 2 numbers no matter if the first number is larger it always says the second number is larger can someone tell me why?

#include <stdio.h>

float getnumbers();
float findlargest(float*,float*);
void displaylargest(float,float*,float*);

int main ()
{
    float num1;
    float num2;
    float largest;

    num1=getnumbers();
    num2=getnumbers();

    largest=findlargest(&num1,&num2);

    displaylargest(largest,&num1,&num2);
    return 0;
}

float getnumbers()
{
    float num;
    printf("Enter a number\n");
    scanf("%2f",&num);
    return num;
}


float findlargest(float*num1ptr,float*num2ptr)
{
    float num1;
    float num2;

    *num1ptr=num1;
    *num2ptr=num2;

    if (num1>num2) {
        return num1;
    } else {
        return num2;
    }
}

void displaylargest(float largest,float*num1ptr,float*num2ptr)
{
    printf("\nthe largest is %2f ",largest);
}

Upvotes: 0

Views: 93

Answers (5)

Hamza Mehboob
Hamza Mehboob

Reputation: 142

// Try this

include < stdio.h> // remove the space

float getnumbers();

float findlargest(float*,float*);

void displaylargest(float); // one parameter is enough in this function

int main () {

float num1;

float num2;

float largest;

num1=getnumbers();
num2=getnumbers();

largest=findlargest(&num1,&num2);

displaylargest(largest);

while(1); // to see the output

return 0;

}

float getnumbers()

{ float num;

printf("Enter a number\n");

scanf("%2f",&num);

return num;

}

float findlargest(floatnum1ptr,floatnum2ptr)

{

float num1;

float num2;

num1 = *num1ptr;

num2 = *num2ptr;

if (num1>num2) {

    return num1;

} 

else {

    return num2;

}

}

void displaylargest(float largest) // required changes here

{

printf("\nthe largest is %2f ",largest);

}

Upvotes: 0

2niru
2niru

Reputation: 56

Look at your first condition if(num1 > num2) this is unlikely to be true, so you can't expect an specific value. Why it is like that? Because you're doing *ptr = value; assignment where you're intended to do value = *ptr;, and since you didn't initialized num1 and num2 they will have unspecified values.

For methods like this you can use ternary/conditional operator return condition ? trueValue : falseValue; so:

float largest(float a, float b) {
    return a > b ? a : b;
}

You shouldn't use pointers where you don't need it, it makes your code more complex unnecessarily.

How did you even identified the problem?

Upvotes: 0

Shiv Kumar
Shiv Kumar

Reputation: 1114

You have made a mistake while typing instead of assigning num1=*num1ptr you did *nump1tr=num1 and same for num2 your code should be like

float findlargest(float*num1ptr,float*num2ptr)
{
  float num1;
  float num2;

  num1=*num1ptr; // not *num1ptr=num1
  num2=*num2ptr; // not *num2ptr=num2

  if (num1>num2) {
      return num1;
    } 
  else {
      return num2;
  }
}

Upvotes: 3

paxdiablo
paxdiablo

Reputation: 882406

float findlargest(float *num1ptr, float *num2ptr)
{
    float num1;
    float num2;

    *num1ptr = num1;
    *num2ptr = num2;

This does not do what you (apparently) think it does. Declaring num1 and num2 here sets them to arbitrary values which you then use to overwrite whatever useful data you may have had in the variables pointed to by num1ptr and num2ptr.

A better variant would simply be:

float findLargest(float num1, float num2)
{
    if (num1 > num2)
        return num1;
    return num2;
}

and then just call it with (from main):

printf("The largest is %.2f\n", findLargest(num1, num2));

Upvotes: 0

Aganju
Aganju

Reputation: 6405

You are declaring float num1; and float num2;, but you never assign them any value, so they will contain some random number that happens to be in your computer’s memory. Then you check if (num1>num2), based on these random values.

Chances are that you get the same ‘random’ values every time you run it, as the computer has no reason to ever change that place in memory, so the result will always be the same.

You need to assign the values the other way round: num1=*numptr1; (or use the parameters directly in the comparison - there is no reason to copy them around).

Upvotes: 0

Related Questions