Home > OS >  Greatest Value to Lowest Value
Greatest Value to Lowest Value

Time:04-24

Good day, I was practicing about C and encounter a problem. The problem was this part or somewhere here. for (ctr = num2; ctr <= num1; ctr ) For example the output is

Enter the value of n1: 3
Enter the value of n2: 10
4 6 8 10 

I would like it to start from the greatest 10 8 6 4 instead of the least 4 6 8 10.

#include <stdio.h>

int main(void) {
    int num1, num2, ctr;

    printf("Enter value of n1: ");
    scanf("%d", &num2);
    printf("Enter value of n2: ");
    scanf("%d", &num1);
    for (ctr = num2; ctr <= num1; ctr  ) {
        if (ctr % 2 == 0)
            printf("%d ", ctr);
    }
    return 0;
}

CodePudding user response:

In your for, simply replace

ctr = num2; ctr <= num1; ctr  

with

ctr = num1; ctr >= num2; ctr--

Your current for loop first initializes ctr to num2. As we go through the loop, ctr is incremented such that it's value becomes num2 1, then num2 2, ..., then num1 - 1, then num1. After ctr is num1, the statement in the loop is executed one last time, then ctr is incremented and it's value becomes num1 1, which exceeds num1, causing the loop to end.

  • ctr takes on the values in {num2, num2 1, ... , num1 - 1, num1, num1 1}. The statement in the loop is executed for each of the values ctr takes except for num1 1.

To reverse this, you initialize ctr to num1 and as we go through the loop, ctr should be decremented such that it's value becomes num1 - 1, then num1 - 2, ..., then num2. After ctr is num2, the statement in the loop is executed one last time, then ctr is decremented and it's value becomes num2 - 1, which is less than num2, causing the loop to end.

  • ctr takes on the values in {num1, num1 - 1, ..., num2 1, num2, num2 - 1}. The statement in the loop is executed for each of the values ctr takes except for num2 - 1.

Notice how in both loops, the statement is executed when ctr takes on a value in {num2, num2 1, ..., num1 - 1, num1}. The only difference is that in the first loop, ctr starts with num2, moving up to num1, whereas in the second loop, ctr starts at num1, moving down to num2.

After making the above change, your program should function as expected. In the code below, I have (1) made the suggested change above and (2) included checks for the return value of scanf. It is not necessary to make change (2), but it is typically good practice to check the return value of scanf. For reference (from C99 Standard)

The scanf function returns the value of the macro EOF if an input failure occurs before any conversion. Otherwise, the scanf function returns the number of input items assigned, which can be fewer than provided for, or even zero, in the event of an early matching failure.

In our case, we expect a scanf call of the form scanf("%d", &num2) to return 1. A return value not equal to 1 means there was an error in scanf. Currently, our function simply stops the program if there is an error. However, using the feof and ferror functions, one would be able to know the type of error that occurred and the programmer can choose to make the program behave differently depending on the type of error. For brevity I have omitted the use of feof and ferror (both from <stdio.h>), though I thought I would mention them since we are on the topic of scanf errors.

It is also worth noting that you need to be careful that the user does not enter a number for num2 that is equal to INT_MIN (a macro defined in <limits.h>), the minimum value for an object of type int. While assigning INT_MIN to an int such as num2 is not by itself wrong, if num2 is INT_MIN, eventually ctr will equal num2, the statement in the loop will be executed one "last" time, but then num2 will be decremented which would mean trying to assign to num2 a number lower than the minimum value for an object of type int. That is, the program will have undefined behavior. The likely result of setting num2 to INT_MIN is that the loop will run forever.

Program

#include <stdio.h>
#include <stdlib.h>

int main(void){

    int num1, num2, ctr;

        printf("Enter value of n1: ");
        if (scanf("%d", &num2) != 1) {
            printf("scanf: error\n");
            exit(EXIT_FAILURE);
        }

        printf("Enter value of n2: ");
        if (scanf("%d", &num1) != 1) {
            printf("scanf: error\n");
            exit(EXIT_FAILURE);
        }

        for (ctr = num1; ctr >= num2; ctr--)
            if (ctr % 2 == 0)
                printf("%d ", ctr);
        printf("\n");

    return 0;

}

Example Session

Enter value of n1: 3
Enter value of n2: 10
10 8 6 4 
  •  Tags:  
  • c
  • Related