1

I have an array filled with ones and zeros, also i have two LED (1 = Left LED / 0 = Right LED )

int Game[100]

And I have an array where I write user actions ( Using joystick ). (( The initial thought with this array was to constantly increase its size by one ... but I read that you can’t do this and you need to implement a “Linked list”, but for now, okay. ))

int Player[2]

I want to go through all the elements of the array "Game", but only through one element

    int i;
    for(i = 0; i <= 100; i++) {
        if(Game[i] == 1) {
            // Left LED ON
            // Left LED OFF
        }
        else if (Game[i] == 0) {
            // Right LED ON
            // Right LED ON
        }

I mean, first I want to take two elements from the array, turn on the LEDs...wait for user input... then take another element (Repeat the first two ) and so on. Is there any way i can do this ?

Vlad Paskevits
  • 388
  • 2
  • 12
  • 2
    I don't understand what you're trying to do. But that `i <= 100` should be `i < 100`. – interjay Jul 10 '20 at 08:30
  • What is the role of `int Player[2]` in this? – anastaciu Jul 10 '20 at 08:30
  • `this array was to constantly increase its size by one ... but I read that you can’t do this and you need to implement a “Linked list”` : you can resize an array allocated in the heap using `realloc`, but what is the role of your array here ? why do you think you need to resize it ? – bruno Jul 10 '20 at 08:41

1 Answers1

0

I'm not sure what your exact motive is but I think I can help you with accessing the elements one by one depending on the user input. Check my code: (Hope you find something useful)

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

int main(void)
{
    int Game[100];
    int i,j=0;
    //filling each element with 1s and 0s
    for(i=0;i<100;i++)
    {
        if(i%2==0)
           Game[i]=0;
        else
           Game[1]=1;
     }
     i=0;
     printf("User Input(-1 to Quit): ");
     scanf("%d",&j);
     while(j!=-1 && i<100)
     {
         //your code
         if(Game[i] == 1)
         {
            // Left LED ON
            // Left LED OFF
         }
         else
         {
            // Right LED ON
            // Right LED ON
         }
         ++i;
         printf("Another User Input(-1 to quit): ");
         scanf("%d",&j);
    }
    return 0;
}