-1

I'm having trouble finding an answer to my "problem".

I created a function that takes a varying amount of integers ('findMinVal(int x, ...)') and returns the lowest number in all calls made. right now my program gets the arguments straight through the code:

int main(){

printf("\nThe minimal number in each call for \"findMin\" is : \n");


findMinVal(90,78,5,20,-1);
findMinVal(70,40,2,-1);
findMinVal(40,30,-1);

return 0;

}/* main */

I would like it to accept variables from a file but I don't know how I can do this..? Any help would be appreciated.

Raskanskyz
  • 65
  • 6
  • You can't populate a `va_list` manually. – Some programmer dude Jun 01 '13 at 12:47
  • Duplicate question : http://stackoverflow.com/questions/988290/populating-a-va-list . In short, this is a bad idea and you should use something like a dynamically built array of ints with a counter (i.e. in readFile : build the array, count items and pass the count and array pointer to your findMinVal() function. – wldsvc Jun 01 '13 at 12:52
  • It looks like the minimum is `-1`, no? ;v) – Potatoswatter Jun 01 '13 at 14:50
  • Unfortunately this is an assignment and I have to use va_list..thanks though – Raskanskyz Jun 01 '13 at 21:14

2 Answers2

1

The only standard way to make variadic parameter list is to make a call to function that accept it passing all params at a time.

findMinValInArray(int n, int a[]) {
    switch(n) {
        case 1: return findMinVal(a[0], -1);
        case 2: return findMinVal(a[0], a[1], -1);
        case 3: return findMinVal(a[0], a[1], a[2], -1);
        case 4: return findMinVal(a[0], a[1], a[2], a[3], -1);
        case 5: return findMinVal(a[0], a[1], a[2], a[3], a[4], -1);
        case 6: return findMinVal(a[0], a[1], a[2], a[3], a[4], a[5], -1);
        case 7: return findMinVal(a[0], a[1], a[2], a[3], a[4], a[5], a[6], -1);
        case 8: return findMinVal(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], -1);
        case 9: return findMinVal(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], 
            a[8], -1);
        case 0xA: return findMinVal(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7],
            a[8], a[9], -1);
        case 0xB: return findMinVal(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7],
            a[8], a[9], a[0xA], -1);
        case 0xC: return findMinVal(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7],
            a[8], a[9], a[0xA], a[0xB], -1);
        case 0xD: return findMinVal(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7],
            a[8], a[9], a[0xA], a[0xB], a[0xC], -1);
        case 0xE: return findMinVal(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7],
            a[8], a[9], a[0xA], a[0xB], a[0xC], a[0xD], -1);
    }
}

Anyway varargs should be avoided if possible.

Vovanium
  • 3,798
  • 17
  • 23
0

While you could use varargs as described by Vovanium, it's much better to just change your findMinVal function to accept an array instead. For example:

int findMinVal(size_t length, int * values){
    /* Find minimum value... 
       (Hint: start with min = values[0] and the loop iterator with 1) */
}

Then call it like this:

/* allocate values and fill it */
int min = findMinVal(length, values);
Kninnug
  • 7,992
  • 1
  • 30
  • 42