Tuto
Tuto

Reputation: 112

Having trouble understanding the for loop

#include <stdio.h>

int n, a[100001], x, y;
int main() {
    scanf("%d", &n);
    while (n--) {
        scanf("%d.%d", &x, &y);
        a[x*1000+y]++;
    }
    for (int i = 0, c = 0; i <= 100000; i++) {
        while (a[i]) {
            --a[i], ++c;
            printf("%d.%03d\n", i / 1000, i % 1000);
            if (c == 7) return 0;
        }
    }

    return 0;
}

This is the code that receives an integer n, then the program is expected to receive n number of double or integer variables.

The program is supposed to print out the smallest 7 variables among the input variables to 3 decimal points.

Now the question is i can't seem to figure out how this code in for loop

while (a[i]) {
    --a[i], ++c;  // <- specifically this part
    printf("%d.%03d\n", i / 1000, i % 1000);
    if (c == 7) return 0;
}

generates 7 smallest variables.

Any help would be much appreciated

Upvotes: -1

Views: 98

Answers (3)

Swanand
Swanand

Reputation: 4115

As hellow mentioned, This is bad code and if you are a student, stay away from such programming style (Not just student, everyone should stay away).

What this code does is it creates sort of "Look-up" table. Whenever a number is entered, it increases a count at that array instance.

e.g. If I input 3.2, it increments a[3002] th location. Code for this is:

scanf("%d.%d", &x, &y);
        a[x*1000+y]++;

x = 3 and y = 2 so a[3*1000+2]++ --> a[3002] = 1

(Note: Code assumes that array a is initialized with 0 - another bad habit)

Now say I entered 1.9, code will increment a[1009]. If I enter 3.2 again, a[3002] will be incremented again.

This was input part.

Now code parses entire array a starting from 0. At first it will encounter 1009, code will print 1.9 and keep on parsing array. When it finds 7 non=zero locations, loop exits.

When you enter same number again, like 3.2, while(a[i]) executes twice printing same number again.

As smaller number will be at lower location in array and array parsing starts from 0, it prints smallest 7 numbers. If you reverse the for loop, you can print 7 biggest numbers.

Upvotes: 0

Mindaugas
Mindaugas

Reputation: 1735

The answer here is how the input data is being stored.

User entered values populate array a. It does not store actual entered numbers, but a COUNT how many times the value was entered (code makes lots of assumptions about data sanity, but lets ignore that)

The data is naturally Sorted from smallest to largest, so to find 7 smallest inputs you just take first 7 values (iterations tracked by index i, c tracks how many values we already did print out) where the COUNT is not zero (a[i], non zero value indicates how many times user entered corresponding value)

Upvotes: -1

Gautham M
Gautham M

Reputation: 4935

Suppose 8.3 is an input, then you are storing the 8003rd index of the array to 1. i.e a[8003]=1. if 8.3 is input twice then a[8003] will be equal to 2. So in the for loop when i=8003, a[8003] is non zero that that means there was an input 8.3. So it is considered in the top 7 smallest input values and the loop exits when count reaches 7.

Upvotes: 2

Related Questions