Having trouble understanding the for loop

understanding for loops
for statement
loops in programming
loops computer science
python loops explained
#include <stdio.h>

int n, a[100001], x, y;
int main() {
    scanf("%d", &n);
    while (n--) {
        scanf("%d.%d", &x, &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

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);

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.

Difficulty understanding for loops and what exactly they are capable , So long story short I am new to python and programming in general and I am having difficulties getting this to print to the console only using: • Generate the output  The inner loop does use j in the regular fashion, although it isn't read inside the loop. It still gets iterated and used in the conditional. The outer loop with i doesn't actually use i but the conditional still changes on each iteration so it should still exit. That use case is usually done with a while loop.

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.

Having trouble understanding the answer for loop practice level 3 , Having trouble understanding the answer for loop practice level 3 exercise 5. Praiseeee. I don't understand how this part of the code works: for(j=2;j<i;j++){  Having trouble understanding this loop Hello again everyone. I don't understand how console.log(prop) would print just the property name to the console, but console.log(person[prop]) would print just the property value.

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)

Trouble understanding 'for' loops - MATLAB Answers, Trouble understanding 'for' loops. When you have a for loop, then the loop variable is assigned the first column in the list of values, and then  In the above code, n++; is a part of the loop and the value of n increments with every iteration and finally the control moves out of the loop when n is 6. Note: The loop does not terminate when n is 5. It terminates when n is 6 since you are asking the loop to run when n<=5. So it will run even when n is 5.

How to understand loops in programming, Let me tell you a story about how I “invented” loops (No, I'm not the inventor of loops. Originally Answered: How do I get a better understanding of loops in any so that you will solve likely problems and problems with a little tweak in future. Having trouble understanding nested for loops. Still a work in Progress- Still need to understand how to have the transcribed data into a word file - This was

I have a problem understanding a for loop assignment, Hello, So I have this problem where I can't wrap my head around it. Here the assignment: A programmer walks into a flower shop. He has $100 in his pocket,  MATLAB - The for Loop - A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

I'm having trouble understanding loops. In the "While and Do While , In the "While and Do While Loops Tutorial," what does the line "Int w = 0;" mean. Also have trouble understanding the statement: printf("letter  Step 1: First initialization happens and the counter variable gets initialized. Step 2: In the second step the condition is checked, where the counter variable is tested for the given condition, if Step 3: After successful execution of statements inside the body of loop, the counter variable is

  • Btw. this is bad code, you should not learn something like this, but instead write/learn proper, readable code.
  • Input/Output sample would help
  • if you are unclear about the line specified by your comment, I recommend you to learn increment and decrement operators in c
  • You show is a "specific part" that you're wondering about. But what about that part are you wondering about? Can you please specify?
  • --a[i], ++c; can be written a[i] = a[i] - 1; c = c + 1;. Was it that you didn't understand?
  • Note that the code is also incorrect, as it (I think) will sort 8.3 as smaller than 8.29 or even 8.300.
  • @tengfred you're correct. The numbers need to be entered with 3 digit fractional precision for it to work.