2

The question is as follows: Write a function that prompts the user to enter a series of positive integers, terminated by the value -1. The function must then return the maximum and minimum values entered by the user. You can assume that the user enters at least one positive integer before entering -1.

So what I thought was to create an array to store all the values entered. The only problem is that I'm not quite sure how to declare an array with unknown number of entries since obviously I would not know how many integers the user will enter before -1.

Then for the latter part, I was thinking to make maximum/minimum=arr[0], and then index++, once arr[index]>maximum or arr[index]

Thank you!

JuicyThio
  • 21
  • 2
  • 4
    Since you only need to report the min and max, why not just have two *values*, set them both to the first input, then replace whichever needs it (if either even do) as you process each new input. So you only need three values total (min, max, and cur). In other words, if the user enters a value and you already know it isn't the min or max, why bother keeping it at all? – WhozCraig Mar 01 '13 at 05:47
  • @WhozCraig looks like our thoughts are matching at exactly the same speed.. –  Mar 01 '13 at 05:55

3 Answers3

2

use dynamic memory allocation functions like malloc calloc and realloc or if you are interested in doing some real good code use linkedlist.

BTW is it necessary to store all the numbers in an array??? u can check the min and max on the fly???

a simple algo would be:

int maxval=0, minval = 0;
while(inputval != -1)
{
    if(inputval < minval)
       minval = inputval;

    if(inputval > maxval)
       maxval = inputval;
}
  • Is there an easier way to do this? I've just started learning C and am pretty sure I've never heard of those. However this is given as sample question for our midterm, so there is probably another way to do it? – JuicyThio Mar 01 '13 at 05:51
  • @JuicyThio This is easy and should learn how to use these functions. –  Mar 01 '13 at 05:52
  • @H2CO3 Okay! I will, after my midterm! – JuicyThio Mar 01 '13 at 05:54
  • Now you've mentioned it, I think I can just check the min and max without storing them! – JuicyThio Mar 01 '13 at 05:55
  • @crashed The code will work, but it spoils the exercise for JuicyThio. I try to hint at the algorithm in plain words and not to give away a complete solution for homework questions. – Peter G. Mar 01 '13 at 06:30
0

Unfortunately, you can't -- at least not directly. Using malloc() you can allocate an array with a variable length, and you can use that to keep allocating more space. For example

int* numbers = malloc(sizeof(int)*how_many);
if (!numbers) {
  //error
}

//do stuff.

In your example you could do something like

while(not_done) {
  if( array-is-full ) {
    how_many *= 2;
    int* new_numbers = (int*)malloc(sizeof(int)*how_many);
    for(int i = 0; i < index;i++)
       new_numbers[i] = numbers[i];
    free(numbers);
    numbers = new_numbers;
  }

  //read user input
  //stick it into the array


}
bchurchill
  • 1,410
  • 8
  • 23
  • [Read this.](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) –  Mar 01 '13 at 05:52
0

you can use dynamic memory allocation and especially realloc. It allow you to resize your array each time you enter a new element

BTW If you want to extract the min and the max from your input then you do nt need to save the input parametr into an array.

Any way here after how you save element into a dynamic array and extract min and max

int min = -1;
int max = -1;
int i = 0;

int *array=malloc(sizof(int));

while (scanf("%d", &x)>0 && x>0)
{
    if(i>0)
        realloc(array, (i+1)*sizeof(int));
    else {
        min = x;
        max = x;
    }
    array[i] = x;
    if (x>max) max = x;
    if (x<min) min = x;
    i++;
} 
MOHAMED
  • 41,599
  • 58
  • 163
  • 268