Don't know how to fix this.

Something is wrong here, and i dont really know why. Maybe i got an order wrong. If you are curious what i am trying to do, its a game of life.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct space {
bool state;
};
struct grid {
int sizes;
space cell[100][100];
int sSwitch(int x,int y)
{
    cell.state[x][y]=(cell.state[x][y]==0);
}
int getmany(int x,int y)
{
    int howmany=0;
    if(cell[(x-1)][y].state==1)
        howmany++;
}
};
What should the function grid::sSWitch() do?
Should it set all the 'cells' to 0?
Something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <limits>

struct space {
    bool state;
};

struct grid {
    int sizes;
    space cell[100][100];
    void sSwitch()
    {
        for(int x = 0; x < 100; x++) {
            for(int y = 0; y < 100; y++) {
                cell[x][y].state = false;
            }
        }
    }
    int getmany()
    {
        int howmany = 0;
        for(int x = 0; x < 100; x++) {
            for(int y = 0; y < 100; y++) {
                if(cell[x][y].state == true) {
                    howmany++;
                }
            }
        }
        return howmany;
    }
};

void waitForEnter();

int main()
{
    grid g;
    g.sSwitch(); // set all 'cells' to false
    g.cell[7][11].state = true;
    g.cell[21][33].state = true;
    std::cout << "There are currently " << g.getmany() << " cells set to true.\n";

    waitForEnter();
    return 0;
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

No, the sSwitch should switch the state of a cell from 1 to 0
bump?
What happens if the cell is already zero?

If you think you've asked a proper question, then you are a mindless retard.
sSwitch should switch the state of a cell from 1 to 0

Something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <limits>

struct space {
    bool state;
};

struct grid {
    int sizes;
    space cell[100][100] { false };
    void sSwitch(int x, int y)
    {
        cell[x][y].state = false;
    }
    int getmany()
    {
        int howmany = 0;
        for(int x = 0; x < 100; x++) {
            for(int y = 0; y < 100; y++) {
                if(cell[x][y].state == true) {
                    howmany++;
                }
            }
        }
        return howmany;
    }
};

void waitForEnter();

int main()
{
    grid g;
    g.cell[7][11].state = true;
    g.cell[21][33].state = true;
    std::cout << "There are currently " << g.getmany() << " cells set to true.\n";
    g.sSwitch(21, 33);
    std::cout << "Now there are " << g.getmany() << " cells set to true.\n";
    waitForEnter();
    return 0;
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}


Or, as jlb asked
jlb wrote:
What happens if the cell is already zero?

if the value is 1 it must become 0 and if it’s 0 it must become 1?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <limits>

struct space {
    bool state;
};

struct grid {
    int sizes;
    space cell[100][100] { false }; // create 'cell' and set all values to false
    void sSwitch(int x, int y)
    {
        cell[x][y].state = !cell[x][y].state;
    }
    int getmany()
    {
        int howmany = 0;
        for(int x = 0; x < 100; x++) {
            for(int y = 0; y < 100; y++) {
                if(cell[x][y].state == true) {
                    howmany++;
                }
            }
        }
        return howmany;
    }
};

void waitForEnter();

int main()
{
    grid g;
    g.cell[7][11].state = true;
    g.cell[21][33].state = true;
    std::cout << "There are currently " << g.getmany() << " cells set to true.\n";
    g.sSwitch(21, 33);
    std::cout << "Now there are " << g.getmany() << " cells set to true.\n";
    g.sSwitch(99, 7); // from false to true
    std::cout << "And now there are " << g.getmany() << " cells set to true again.\n";
    waitForEnter();
    return 0;
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

Last edited on
Somewhere the syntax was incorrect, sorry for not formulating my question properly.
Don't worry, i fixed it.
Topic archived. No new replies allowed.