Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercise 03_01 #171

Open
calbch opened this issue Jun 21, 2024 · 4 comments
Open

Exercise 03_01 #171

calbch opened this issue Jun 21, 2024 · 4 comments

Comments

@calbch
Copy link

calbch commented Jun 21, 2024

Shouldn't it say "why it's not 10" in the bonus?

Since i is shadowed inside of the scope of the for loop, final value would be the initial random value assigned during it's definition.

What am I missing here?

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

// DO NOT CHANGE THIS
static int track = 0, count = 0;

// DO NOT CHANGE THIS
void verify_count(int *count, int i) {
    assert(i == track++);
    (*count)++;
}

int main() {
    int i = rand() % 100 + 11; // DO NOT CHANGE THIS

    // YOUR CODE HERE
    for (int i = 0; i < 10; ++i) {
        // END YOUR CODE
        printf("%d\n", i);
        verify_count(&count, i); // DO NOT CHANGE THIS
    }

    // BONUS: print the value of i here and try to understand why it's 10
    printf("final value: %d\n", i);

    // DO NOT CHANGE THIS
    assert(count == 10);
    return 0;
}
@sleaper
Copy link
Contributor

sleaper commented Jun 21, 2024

I think you are right, but this exercise was done by @7etsuo. So let's wait for his explanation of what he meant by that.

@danwritecode
Copy link
Owner

I'll take a look at this tonight.

@7etsuo
Copy link
Collaborator

7etsuo commented Jun 26, 2024

Reference Code:

/** LEARNING
 * Your goal is to understand how the for statement works.
 *
 * The for statement has the following syntax:
 *
 * for (initialization; condition; increment) {
 *		statement;
 * 	 	statement;
 * 	 	...
 * }
 *
 * The for statement is equivalent to the following while statement:
 *
 * initialization;
 * while (condition) {
 *		statement;
 * 		statement;
 * 		...
 *		increment;
 *	}
 */

/** EXERCISE
 * Your job is to write a for statement equivalent to the following 
 * while statement:
 *
 * i = 0; // initialization
 * while (i < 10) { // condition
 *	printf("%d\n", i);
 *	verify_count(&count, i);
 *	i++; // increment
 * }
 */

// ❌ I AM NOT DONE

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

// DO NOT CHANGE THIS
static int track = 0, count = 0;

// DO NOT CHANGE THIS
void verify_count (int *count, int i) {
	assert(i == track++);
	(*count)++;
}

int main() {
	int i = rand() % 100 + 11; // DO NOT CHANGE THIS

	// YOUR CODE HERE
	for (initialization; condition; increment) {
	// END YOUR CODE
		printf("%d\n", i);
		verify_count(&count, i); // DO NOT CHANGE THIS
	}

	// BONUS: print the value of i here and try to understand why it's 10

	// DO NOT CHANGE THIS
	assert (count == 10);
	return 0;
}

Solution:

/ * i = 0; // initialization
  * while (i < 10) { // condition
  *	printf("%d\n", i);
  *	verify_count(&count, i);
  *	i++; // increment
  * } 
  */
	int i = rand() % 100 + 11; // DO NOT CHANGE THIS

	// YOUR CODE HERE
	for (i=0; i<10; i++) {
	        // END YOUR CODE
		printf("%d\n", i);
		verify_count(&count, i); // DO NOT CHANGE THIS
	}

	// BONUS: print the value of i here and try to understand why it's 10
        /* Hint look at the last value i printed in the for loop */
        printf("i=%d\n", i); 
	// DO NOT CHANGE THIS
	assert (count == 10);
	return 0;

@7etsuo
Copy link
Collaborator

7etsuo commented Jun 26, 2024

In the context of C programming, within a for loop, the initialization expression is distinct from variable declaration, although it often includes variable declarations as a convenience.

Here's a breakdown:

Variable Declaration: This involves specifying a type and associating it with a variable name. For example: int i; declares a variable i of type int.

Initialization: This step assigns a value to the variable at the time of its creation or at the start of the loop. In a for loop, the initialization expression is executed only once, before the loop begins. It often includes declaring a variable, but can also be used to set an existing variable to a new value.

for (int i = 0; i < 10; i++) { 
} 

int i = 0 is both declaring and initializing i. However, if i were declared before the loop:

int i;
for (i = 0; i < 10; i++) {
}

Here, i = 0 in the loop is purely initialization, not declaration.

The details are in the definitions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants