0

Simple question. I am trying to build a simple script that will ask you for a number. That integer will be the amount of numbers you will enter in total. Then your program finds the biggest number and the smallest. And I have build that. Easy.

  int CHECK=100;
  int a[50],size,i,max,min;

  printf("\nEnter the size of the array: ");
  scanf("%d",&size);
  printf("\nEnter %d numbers: ", size);
  for(i=0;i<size;i++)
  scanf("%d",&a[i]);
  }
  max=a[0];
  for(i=1;i<size;i++){
     if(max<a[i])
       max=a[i];
  }
 printf("\nLargest element: %d",max);
 printf("\n");
 min=a[0];
 for(i=1;i<size;i++){
  if(min>a[i])
       min=a[i];
}
printf("Smallest element: %d",small);
printf("\n");

return 0;
}

My question is, how can I tell to check for conditions. For instance, the user can input only positive numbers and smaller than 100. So if you enter -4 or 201 an Error has to be displayed telling you to try again. Any ideas as of how we can do that in a simple way. Or even point me to the right direction.

I appreciate it. Thank you.

6
  • 2
    Why do you feel that you must store the numbers? Consider looking for the smallest/largest as each number is entered, then you don't have to have a static limit (50) on how many numbers can be accepted. Designing programs to not have static limits is generally preferable. Commented Nov 18, 2012 at 12:46
  • 1
    Side issue: scanf() is terrible for inputting values, because if it can't match the input to the format string (e.g. if a letter like "A" was typed) then it will leave the remaining characters in the input buffer and they will screw up the next call to scanf(). Commented Nov 18, 2012 at 12:56
  • 1
    Once you replace scanf() with something nicer, like fgets() followed by sscanf() (DO NOT use gets() because it can cause a buffer overflow!): What you need to do is check whether the result matched your expectations (e.g. the return value from sscanf() should be 1, and the value should be in range), and if it's not then print an error message and do --i, so that the effect of the loop causes i to remain unchanged on the next loop cycle. Commented Nov 18, 2012 at 12:58
  • You guys have all been incredible help. Thank you very much. I am just trying to learn C on my own and still trying to understand scanf() or fgets() and all the different types. But j_random_hacker you have a point. I will try with fgets() and see what I can do. Commented Nov 18, 2012 at 18:12
  • You're welcome :) One more tip: if you stick a "@" directly in front of a person's username in a comment, they'll be notified. Commented Nov 19, 2012 at 12:18

1 Answer 1

1

It really is quite easy - at least as easy as in Python. I'm sticking with your scanf to illustrate the condition, but see @j_random_hacker's comments (++).

for(i=0; i < size; i++) {
    scanf("%d",&a[i]);
    if ( a[i] < 0 || a[i] > 99 ) {
        fprintf(stderr, "Number '%d' is out of range, try again: ", a[i]);
        i--;
    }
}

By the way you had a missing { in your code, I assume it was a transposition error. (Consistent indentation will help pickup errors like this - it is not just a good idea in Python).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you cdarke. Indeed it is quite easy now that I saw your sample. I appreciate you taking the time and helping me out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.