C shell program with pointers

This forum is dedicated to basic help and support :

Ask here your questions about basic installation and usage of Mageia. For example you may post here all your questions about getting Mageia isos and installing it, configuring your printer, using your word processor etc.

Try to ask your questions in the right sub-forum with as much details as you can gather. the more precise the question will be, the more likely you are to get a useful answer

C shell program with pointers

Postby ofbodur » Apr 28th, '13, 21:32

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
There is nothing worse than having failed to explain what you believe.
User avatar
ofbodur
 
Posts: 35
Joined: Mar 10th, '13, 12:33
Location: Istanbul

Re: C shell program with pointers

Postby tom_ » Apr 28th, '13, 21:40

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
tom_
 
Posts: 423
Joined: Sep 3rd, '11, 12:26
Location: Porto Ercole, Italy

Re: C shell program with pointers

Postby ofbodur » Apr 28th, '13, 21:58

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’
There is nothing worse than having failed to explain what you believe.
User avatar
ofbodur
 
Posts: 35
Joined: Mar 10th, '13, 12:33
Location: Istanbul

Re: C shell program with pointers

Postby tom_ » Apr 28th, '13, 22:14

the solution is in the link I already posted, read it carefully
tom_
 
Posts: 423
Joined: Sep 3rd, '11, 12:26
Location: Porto Ercole, Italy

Re: C shell program with pointers

Postby ofbodur » Apr 28th, '13, 22:34

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..
There is nothing worse than having failed to explain what you believe.
User avatar
ofbodur
 
Posts: 35
Joined: Mar 10th, '13, 12:33
Location: Istanbul

Re: C shell program with pointers

Postby tom_ » Apr 29th, '13, 18:05

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
tom_
 
Posts: 423
Joined: Sep 3rd, '11, 12:26
Location: Porto Ercole, Italy

Re: C shell program with pointers

Postby ofbodur » Apr 29th, '13, 20:43

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?
There is nothing worse than having failed to explain what you believe.
User avatar
ofbodur
 
Posts: 35
Joined: Mar 10th, '13, 12:33
Location: Istanbul

Re: C shell program with pointers

Postby tom_ » Apr 30th, '13, 11:22

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;
}
tom_
 
Posts: 423
Joined: Sep 3rd, '11, 12:26
Location: Porto Ercole, Italy


Return to Basic support

Who is online

Users browsing this forum: No registered users and 1 guest