P
P
Pinkman2020-02-22 02:48:17
JavaScript
Pinkman, 2020-02-22 02:48:17

What's wrong with #define?

There are 100 elements in the array, all elements are initially zero. You need to make them start changing their value to 1 or 0 in a certain order:
1. Change the value of all elements of the array to 1.
2. Change the value of every second element in the array.
3. change the value of every third element in the array.
4. Do action 1-3 97 more times.
5. Find all 1s in the array and display their index on the screen.
I took the task from here
and everything would be fine, things were done and bloomed, but as I decided to start I got it
5e506aacbd11d125126407.png
. It doesn’t matter if I commented on all printfs or not, I always got this on the exhaust. What is the reason for this, and why does it not work as it should?
ps For the sake of completeness, I will say that I understand define as a thing that simply substitutes some value instead of a human word.
pps so that there are no questions, the source was saved and compiled again.
5e507b0aa2864852876862.png

#include <stdio.h>
#define OPEN 1
#define CLOSE 0

void switch_state(int *status)
{
    if(*status == CLOSE)
    {
        *status = OPEN;
        //printf("OPEN");

    }
    else
    {
        *status = CLOSE;
        //printf("CLOSE");
    }
}

int main() 
{
    int state[100] = {CLOSE};
    int i = 0;
    while (i != 100)
    {
        if (i % 2 == 0)
            switch_state(&state[i]);

        else if(i % 3 == 0)
            switch_state(&state[i]);
            
        else
        {
            switch_state(&state[i]);
            //printf("%i\n", state[i]);
        }
        i++;
        
    }    
    return 0;    
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Bogachev, 2019-06-26
@fapchat

function compareCalories(colaA, colaB) {
    if (colaA.name > colaB.name) {
        return 1;
    } else if (colaA.name === colaB.name) {
        // . . .

Maybe here you need to compare calories, and not the name of the products?

S
Stalker_RED, 2020-02-22
@famousman204

The task itself is strange.

Do action 1-3 97 more times.

since in clause 1 all elements are set to one, the result of the last, 98th repetition will be the same as after the first pass.
Moreover, item 1 is not implemented in your code, like item 3, and item 5.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question