I wrote an Ansi C# code that finds the smallest number in a several unknown function calls.

You can see the code, it is very clear. just need help with how to make the program to not ignore always the first number in the list of each argument list... and another, how to handle with call like:

lowest_ever ( -1);

The range of numbers is between 0 to +100 ONLY ! and every call to function must end with -1 (those are the roles....).

My tryout:

#include <stdarg.h>
#include <stdio.h>

int lowest_ever (int frst,...)
{
  	va_list mylist;
  static int lowest_num=101;
  static int  next_num;
  
  va_start (mylist, frst);		/*Initialize the argument list*/

  
  next_num= va_arg(mylist, int);
  
  while (next_num!=-1)
  {
	  if (next_num <lowest_num)
		  lowest_num= next_num;
		  next_num = va_arg(mylist, int);
  }  
  
	
  va_end (mylist);			/*Clean up */
  return lowest_num;
}

int
main (void)
{
  /*This call prints 5*/
  printf ("%d\n", lowest_ever (5, 78, 90, 20, -1));

  /*This call prints 2*/
  printf ("%d\n", lowest_ever (70, 40, 2, -1));

/*This call prints 2*/
  printf ("%d\n", lowest_ever (40, 30, -1));
  
  return 0;
}

Thanks !!

to not ignore always the first number in the list

static int lowest_num=frst;

how to handle with call like: lowest_ever ( -1);

if(frst == -1)
    return -1;

Edited 6 Years Ago by WaltP: Do NOT use QUOTE tags unless quoting someone else's post

static int lowest_num=frst;
if(frst == -1)
    return -1;

Can you add those lines in the correct places and post the all code? because I already tried the first one you suggest ( static int lowest_num=frst; ) and it hasn't work :)

Edited 6 Years Ago by Adami: n/a

I just realize you have it static. Of course it wouldn't work. You need

if(frst < lowest_num)
    lowest_num = frst;

I just realize you have it static. Of course it wouldn't work. You need

if(frst < lowest_num)
    lowest_num = frst;

Can you put it in the correct place in the code and show the all of the improved code ?

thanks :)

You do not want your variable to be static .

Set lowest_int to the first value in the parameter list, then check all the others.

You do not want your variable to be static .

Set lowest_int to the first value in the parameter list, then check all the others.

Can you fix the complete fixed code you suggest ? :)

I just realize you have it static. Of course it wouldn't work. You need

if(frst < lowest_num)
    lowest_num = frst;

Still doesn't working....

Show me your code.

#include <stdarg.h>
#include <stdio.h>

int lowest_ever (int frst,...)
{
  	va_list mylist;
  static int lowest_num;
  static int  next_num;
  
  va_start (mylist, frst);		/*Initialize the argument list*/

  
  next_num= va_arg(mylist, int);
  
  while (next_num!=-1)
  {
	  if (frst <lowest_num)
		  lowest_num= frst;
		  next_num = va_arg(mylist, int);
  }  
  
	
  va_end (mylist);			/*Clean up */
  return lowest_num;
}

int
main (void)
{
  /*This call prints 5*/
  printf ("%d\n", lowest_ever (5, 78, 90, 20, -1));

  /*This call prints 2*/
  printf ("%d\n", lowest_ever (70, 40, 2, -1));

/*This call prints 2*/
  printf ("%d\n", lowest_ever (40, 30, -1));
  
  return 0;
}
int lowest_ever (int frst,...)
{
...
  while (next_num!=-1)

Man... turn on your logic. Or even better use a debugger. When you call lowest_ever(-1) , what do you think frst would be?

Edited 6 Years Ago by nezachem: n/a

I just realize you have it static. Of course it wouldn't work.

You do not want your variable to be [B]static[/B] .

Your code:

static int lowest_num;
  static int  next_num;

Do you listen this well in class too?

Your code:

static int lowest_num;
  static int  next_num;

Do you listen this well in class too?

Strictly speaking, this is one of the rarest situations where static is called for:

that finds the smallest number in a several unknown function calls

Edited 6 Years Ago by nezachem: n/a

I got over the frst problem, but what should I need to do in order to handle with case like: printf ("%d\n", lowest_ever (-1));
???
MY CODE:

#include <stdarg.h>
#include <stdio.h>

int lowest_ever (int frst,...)
{
  	va_list mylist;
  static int lowest_num=101;
  static int  next_num;
  
  va_start (mylist, frst);		/*Initialize the argument list*/

  next_num= va_arg(mylist, int);
  
	if (frst <lowest_num)
		  lowest_num= frst;
	if (frst = -1)
		printf("ERROR"); /* ADDON*/
  while (next_num!=-1)
  {
	  if (next_num <lowest_num)
		  lowest_num= next_num;
	  next_num = va_arg(mylist, int);
  }  
  
	
  va_end (mylist);			/*Clean up */
  return lowest_num;
}

int
main (void)
{
  /*This call prints 5*/
  printf ("%d\n", lowest_ever (5, 78, 100, 20, -1));

  /*This call prints 2*/
  printf ("%d\n", lowest_ever (70, 40, 2, -1));

/*This call prints 2*/
  printf ("%d\n", lowest_ever (40, 30, -1));

  /*This call prints error*/
  printf ("%d\n", lowest_ever (-1));  /* ADDON*/

  
  return 0;
}

Thanks !!

I got over the frst problem, but what should I need to do in order to handle with case like: printf ("%d\n", lowest_ever (-1));
???

return lowest_num anyway, perhaps?

You still have a small problem though. You shall not call va_arg until you sure there is a next argument. Besides, the call to lowest_ever (-1) sets lowest_num to -1; that is not likely what you want.
To deal with all this I'd restructure it as follows (you've done enough to see the solution):

int lowest_ever (int frst,...)
{
  va_list mylist;
  static int lowest_num=101;
  int  num;
  
  va_start (mylist, frst);		/*Initialize the argument list*/
  for(num = frst; num != -1; num = va_arg(mylist, int)) {
	  if (num <lowest_num)
		  lowest_num = num;
  }  
  va_end (mylist);			/*Clean up */
  return lowest_num;
}

return lowest_num anyway, perhaps?

You still have a small problem though. You shall not call va_arg until you sure there is a next argument. Besides, the call to lowest_ever (-1) sets lowest_num to -1; that is not likely what you want.
To deal with all this I'd restructure it as follows (you've done enough to see the solution):

I have only 1 problem left....

I made everything but still with printf ("%d\n", lowest_ever (-1)); I can't handle...

my code is:

#include <stdarg.h>
#include <stdio.h>

int lowest_ever (int frst,...)
{
    va_list mylist;
  static int lowest_num=101;
  static int  next_num;

  va_start (mylist, frst);      //Initialize the argument list

  next_num= va_arg(mylist, int);

    if (frst <lowest_num)
          lowest_num= frst;
    if (frst == -1)
    {
         return (-1); 
    }
  while (next_num!=-1)
  {
      if (next_num <lowest_num)
          lowest_num= next_num;
      next_num = va_arg(mylist, int);
  }  


  va_end (mylist);          //Clean up 
  return lowest_num;
}




      int
main (void)
{
  //This call prints 5
  printf ("%d\n", lowest_ever (5, 78, 100, 20, -1));

  //This call prints 
  printf ("%d\n", lowest_ever (70, 40, 2, -1));

//This call prints 
  printf ("%d\n", lowest_ever (40, 30, -1));

  //This call prints error
  printf ("%d\n", lowest_ever (-1));  


  return 0;
}

I just need to know how do I deal with the single -1 call.... :'(

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I just need to know how do I deal with the single -1 call.... :'(

Wouldn't you want to do what the function says and return the lowest ever?

#include <stdio.h>
#include <stdarg.h>
#include <limits.h>

int lowest_ever(int first, ...)
{
   static int lowest = INT_MAX;
   va_list arglist;
   /**
    * Handle the first argument.
    */
   va_start(arglist, first);
   if ( first == -1 )
   {
      return lowest; /* list began with end sentinel */
   }
   if ( first < lowest )
   {
      lowest = first;
   }
   /**
    * Handle the remaining '...' arguments.
    */
   for ( ;; )
   {
      int  next = va_arg(arglist, int);
      if ( next == -1 )
      {
         break;
      }
      if ( next < lowest )
      {
         lowest = next;
      }
   }
   va_end(arglist);

   return lowest;
}

int main (void)
{
   printf ("%d\n", lowest_ever (5, 78, 100, 20, -1));
   printf ("%d\n", lowest_ever (70, 40, 2, -1));
   printf ("%d\n", lowest_ever (40, 30, -1));
   printf ("%d\n", lowest_ever (-1));
   printf ("%d\n", lowest_ever (-10, -1));
   printf ("%d\n", lowest_ever (40, 30, -1));
   printf ("%d\n", lowest_ever (100, 75, -20, -1));
   return 0;
}

/* my output
5
2
2
2
-10
-10
-20
*/

Wouldn't you want to do what the function says and return the lowest ever?

How about this ?

#include <stdarg.h>
#include <stdio.h>

/* This function gets unknown length list with numbers. */

int lowest_ever (int frst,...) /*gets unknown list, returns integer#*/
{
  	va_list mylist;
  static int lowest_num=101; /*must be static for saving values & higher than 100
                               because of the range we deal with 0 to +100*/
  static int  next_num;			/*represent the next value on list*/
  
  va_start (mylist, frst);		/*Initialize the argument list*/

  next_num= va_arg(mylist, int); /*using the va_arg() to get the next value in list*/
  
	if (frst <lowest_num)		/*if first value in list will be lower than the next,
								  he will be store as the lowest*/
		  lowest_num= frst;
	if (frst == -1)				/*case of lowest_ever(-1)*/
	{
		 return (999);			/*error code 999.Deal with lowest_ever (-1) call.
								  See main() for further information*/
	}
  while (next_num!=-1)			/*keep going head with -1*/
  {
	  if (next_num <lowest_num)	/*Every time head with lower value than current it will update the lowest#*/
		  lowest_num= next_num;
	  next_num = va_arg(mylist, int);
  }  
  
	
  va_end (mylist);			/*Clean up mylist*/ 
  return lowest_num;		/*return the up to date lowest number*/
}

  int main (void)
{
int result; /* holds the result value to deal with */


//This call prints '5'
  result=lowest_ever (90,78,5,20,-1);
if (result!=999)
	printf("Current lowest number until this call is: %d\n\n", result);
else
	printf("Error calling to function 'lowest ever'.\n\nNo value in list, please enter valid values !\n\n\n\n");



//This call prints '2'
  result=lowest_ever (70,40,2,-1);
if (result!=999)
	printf("Current lowest number until this call is: %d\n\n", result);
else
	printf("Error calling to function 'lowest ever'.\n\nNo value in list, please enter valid values !\n\n\n\n");



//This call prints '2'
  result=lowest_ever (40,30,-1);
if (result!=999)
	printf("Current lowest number until this call is: %d\n\n", result);
else
	printf("Error calling to function 'lowest ever'.\n\nNo value in list, please enter valid values !\n\n\n\n");



//This call prints 'ERROR'
  result=lowest_ever (-1);
if (result!=999)
	printf("Current lowest number until this call is: %d\n\n", result);
else
	printf("Error calling to function 'lowest ever'.\n\nNo value in list, please enter valid values !\n\n\n\n");


}

BUT !! will this be accept with the exercise requirements ?

Thanks !

This question has already been answered. Start a new discussion instead.