1

Hi I'm a beginner learning to write c and I was wondering how you would write a code that promts you to give a positive integer and ends once you've entered 0 and the previous numbers before 0 is at least 2 positive integers. The program then determines the largest positive integer input and smallest integer input.

#include <stdio.h>

int main(void)
{
    int integer=0, largest_int, smallest_int;

    while (integer) {
        printf("Please enter a positive integer: \n");
        scanf("%d", &integer);
        if (integer = 0) {
            while (integer<=0) {
                printf("Please enter a positive integer: \n");
                scanf("%d", &integer);
                    if (integer>0) {
                        printf("Please enter a positive integer: \n");
                        scanf("%d", &integer);

                        if (integer>0) {
                            printf("the largest integer is.....and the smallest integer is......");printf("Please enter a positive integer: \n");
                            break;
                        }
                        else {
                            printf("Please enter a positive integer: \n");
                            scanf("%d", &integer);
                        }
                }
            }
        }
    }

    return 0;
}

at first I tried to use do/while loop but it didn't work at all.

I'm also onfused with how the compiler is reading the loops when there's an if ese statment in the loop.

Here's what I was thinking: I thought about having the "Enter positive integer" ask repetitivly until you input a two positive numbers. Then I'd have the program check if the next input is equal to zero if it doesn;t then it continues the loop again. If the user does input at least two positive integers and then a zero the loop breaks and I print out the greatest int and smallest int. What also confuses me is how you would be able to filter through all the user's inputs and determine these values because I could keep entering a integers 100000000000000000000 times and out of all those times how would I know?

4
  • 1
    the user is able to input as many integers as they want the there is not maximum amount of integers Oct 12, 2012 at 15:37
  • Your code does virtually nothing since you have initialized integer to 0 and using it in the while loop
    – Raj
    Oct 12, 2012 at 15:47
  • Is a switch statement of for loop a better choice? Oct 12, 2012 at 15:49
  • clarification: at least positive consecutive integers before the zero Oct 12, 2012 at 16:17

4 Answers 4

1

As this seems to be homework, some points for you to ponder.

Your while (integer) is never executed as you initialize integer to zero just before it. So what you have really is while(0), and nothing inside the loop is executed. You could replace it by a do { ... } while(condition) but I'd rather keep the kind of current loop and add an extra input reading before the loop (the reason is to initialize variables for maximum and minimum finding -- see below).

Add a variable, say NPos to count how many of the last entries where a positive integer. initialize it to zero. Each time a number is entered, if it is positive increase the value of NPos. Otherwise set it to zero (this can be tricky as if the entry is zero it may also mean stop execution instead of reset NPos). When the number entered is 0, check that NPos is at least 2 and, if so, stop.

For finding extreme values, create two variables (max and min) and initialize them to the first user input. Then, at each valid input, i. e., not an input meaning end of execution, check whether the value is greater than max or less thanmin. If so, replace max or min by the input.

These are the greater lines of the algorithm. The implementation is up to you.

A final note: I do not like to name a value integer, but that is maybe just me.

1
0

This is not the solution but some coding errors in your code:

your code will not work first as follows

 int integer=0, largest_int, smallest_int;

            while (integer) {
                    ........
                            }

Here integer is 0 and you are giving this in while which will comes out at the first check.

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

int main(void)
{
    int integer, largest_int=INT_MIN, smallest_int=INT_MAX;

    do
    {
        printf("Enter a positive integer: ");
        scanf("%d", &integer);
        if (integer < 0)
        continue;
        else if (integer == 0)
        break;
        else
        {
            if (integer > largest_int)
            largest_int = integer;
            if (integer < smallest_int)
            smallest_int = integer;
        }
    } while(1);

    printf("Largest integer is %d\nSmallest integer is %d", largest_int, smallest_int);
}
0

Example

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

int main() {
  int integer, largest_int = INT_MIN, smallest_int = INT_MAX;
  int count_positive = 0;

  for ( ; ; ) {
    printf("Please enter a positive integer: \n");
    scanf("%d", &integer); //XXX add error handling
    if (integer == 0 && count_positive > 1) 
      break;
    else if (integer > 0) {
      count_positive = count_positive > 1 ? 2 : count_positive + 1;
      if (largest_int < integer)
        largest_int = integer;
      if (smallest_int > integer)
        smallest_int = integer;
    }
  }
  printf("%d %d\n", smallest_int, largest_int);
  return 0;
}

if "at least 2 positive integers" requirement means that they should be consecutive then reset count_positive to zero when you've encountered an integer less than or equal to zero. Add to the loop:

else if (integer <= 0) 
  count_positive = 0;

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.