Page 1 of 1

C shell program with pointers

PostPosted: Apr 28th, '13, 21:32
by ofbodur
my problem is:
write a C program that prompts the user for three numbers, finds the largest and smallest number and prompts the three numbers entered, the largest number and the smallest number. However, you should read the three numbers in your main program, echo them in the main program then pass the three numbers to a subprogram. Your subprogram should calculate the largest and smallest number in the three entries and return them to the calling program. Remember that since you are returning two values, you need a five element argument list (3 inputs and 2 results) so that you should use pointers where appropriate. The calling program must finally echo the smallest and largest number.

my code is:


Code: Select all
#include <stdio.h>

int main ()
{
    int first, second, third, *min;
         printf("\nEnter three integers:");
         scanf("%i,%i,%i", &first, &second, &third);


   findminimum( first, second, third, *min );
    return 0;
}

    int findminimum( first, second, third, *min )
{

     first = *min;
       if( second < *min )
          {
           second=*min;
          }
       if( third<*min )
          {
           third=*min;
          }
printf ("The minimum of three numbers is: %i", *min);
return 0;
}

but, it gives the error below:
ÖmerFarukBODURQ3.c:14:44: error: expected ‘)’ before ‘*’ token

I gave 1 hour but could not fix it..
Please help me to find my error...
Thanks in advance

Re: C shell program with pointers

PostPosted: Apr 28th, '13, 21:40
by tom_
ofbodur wrote:ÖmerFarukBODURQ3.c:14:44: error: expected ‘)’ before ‘*’ token


the compiler suggests that the error is in line 14 column 44

I can't see your line numbers, but I guess it is this line
Code: Select all
    int findminimum( first, second, third, *min )

where you forgot to specify the type of each argument

check the syntax of "A function that takes an argument"

http://www.mattababy.org/~belmonte/Teaching/CCC/CrashCourseC.html#A2.1

Re: C shell program with pointers

PostPosted: Apr 28th, '13, 21:58
by ofbodur
I rearranged my code as follows:


Code: Select all
#include <stdio.h>

int main ()
{
    int first, second, third, *min, *max;
         printf("\nEnter three integers:");
         scanf("%i,%i,%i", &first, &second, &third);


   findminimum ( int first, int second, int third, int *min );
   findmaximum ( int first, int second, int third, int *max );
   return 0;
}

    int findminimum ( int first, int second, int third, int *min )
{

     first = *min;
       if( second < *min )
          {
           second=*min;
          }
       if( third<*min )
          {
           third=*min;
          }
printf ("The minimum of three numbers is: %i", *min);
return 0;
}

   int findmaximum ( int first, int second, int third, int *max )
{

     *max=first;
      if ( second > *max )
       {
        second=*max;
       }
      if ( third > *max )
       {
       third=*max;
       }
printf ("The maximum of three numbers is: %i", *max);
return 0;
}


but, its output gives this error this time:

ÖmerFarukBODURQ3.c: In function ‘main’:
ÖmerFarukBODURQ3.c:10:18: error: expected expression before ‘int’
ÖmerFarukBODURQ3.c:11:18: error: expected expression before ‘int’

Re: C shell program with pointers

PostPosted: Apr 28th, '13, 22:14
by tom_
the solution is in the link I already posted, read it carefully

Re: C shell program with pointers

PostPosted: Apr 28th, '13, 22:34
by ofbodur
I corrected it but there is another error...


Code: Select all
#include <stdio.h>

int main ()
{
    int first, second, third, min, max;
        printf("\nEnter three integers:");
         scanf("%i,%i,%i", &first, &second, &third);


   int findminimum(int first, int second, int third, int min);
   int findmaximum(int first, int second, int third, int min);

 return 0;
}
    int findminimum ( int first, int second, int third, int min)
{

     min=first;
       if( second < min )
          {
           second=min;
          }
       if( third<min )
          {
           third=min;
          }
printf ("The minimum of three numbers is: %i", min);

return min;
}

   int findmaximum (int first, int second, int third, int max)
{

     max=first;
      if ( second > max )
       {
        second=max;
       }
      if ( third > max )
       {
       third=max;
       }
printf ("The maximum of three numbers is: %i", max);
return max;

}


at the output, it takes 3 integers from me but gives nothing!!
no error as well..

Re: C shell program with pointers

PostPosted: Apr 29th, '13, 18:05
by tom_
I guess you mean that you have no output on the screen

if you want print a line, you must declare that the line is ended adding a \n at the end

Code: Select all
printf ("The maximum of three numbers is: %i\n", max);


there are also other problem in your code,
but now you are able to print an output on a cosole so you have a nice to do a debug session,
I'm sure you'll find them by yourself!

Bye

Re: C shell program with pointers

PostPosted: Apr 29th, '13, 20:43
by ofbodur
tom_ wrote:I guess you mean that you have no output on the screen

if you want print a line, you must declare that the line is ended adding a \n at the end

Code: Select all
printf ("The maximum of three numbers is: %i\n", max);


there are also other problem in your code,
but now you are able to print an output on a cosole so you have a nice to do a debug session,
I'm sure you'll find them by yourself!

Bye


The problem is not about line deficiency Tom..
I think the problem is on when I call the subprograms in the main program... Having been aware of that, yet, I could not find it!...:/

Would you please (truely) help me?

Re: C shell program with pointers

PostPosted: Apr 30th, '13, 11:22
by tom_
in your main you are declaring 2 functions,
but I presume you want call them and have their return value;
if so the fourth parameter is unuseful

try this (untested)

Code: Select all
int main ()
{
    int first, second, third, min, max;
        printf("\nEnter three integers:");
         scanf("%i,%i,%i", &first, &second, &third);

   min = findminimum(first, second, third);
   max = findmaximum(first, second, third);

return 0;
}

 int findminimum ( int first, int second, int third)
{
     int min; //local variable
     min=first;
       if( second < min )
          {
           second=min;
          }
       if( third<min )
          {
           third=min;
          }
printf ("The minimum of three numbers is: %i\n", min);

return min;
}