Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

pointers in c not getting right output

Victoria TibbsVictoria Tibbs Member Posts: 2

i have to create a program and i must use pointers. i have to write a function getnumbers that i can only call one time but ask the user for 2 numbers. then a function findlargest that compares the numbers and figure out which one is bigger. then a function that displays the largest number.I can only call each function one time and can only use local variables. I wrote a program but whenever i enter 2 numbers no matter what i enter it always says the second number is largest even if its not. can someone please help me? here is my code
`
#include <stdio.h>
float getnumbers();

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

    int main ()
      {



          float num2;
          float num;
         float largest;


         num=getnumbers();


     largest=findlargest(&num,&num2);
    displaylargest(largest);  



return 0;
 }

       float getnumbers()
   {

           float num;

         printf("Enter a number\n");
            scanf("%2f",&num);

       printf("Enter a number\n");
            scanf("%2f",&num);

           return num;
}

float findlargest(float*numptr,float*num2ptr)
     {
          float num;
          float num2;

         num=*numptr; 
         num2=*num2ptr; 

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

}
void displaylargest(float largest)

{

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

}

`

Tagged:
Sign In or Register to comment.