vendredi 19 février 2021

Storing and iterating through stored values in C

In the function below the user inputs 10 numbers 5 stored in X_users_inputs and Y_users_inputsare being stored either in X_users_inputs if the k value is 0 or in Y_users_inputs if k is 1. However the if-else statement is faulty I am trying to see all the stored values from both of the users. How could i fix the if statements down below.

Variables

#include <stdio.h>
int main(void)
{
  int input = 0, k = 0;
  char playerSelect[][2] = {"1", "2"};
  int X_users_inputs[10] = {0};
  int O_user_inputs[10] = {0};

  int s_count = 1;
  int l_count = 1;

Function

  for (int i = 1; i <= 11; i++)
  {
    printf("\nPlayer %s input: ", playerSelect[k]);

    scanf("%d", &input);

    if(k== 0){
        X_users_inputs[i-1] = input;
        for(int l =0; l < l_count; l++){
                printf("X user %d\n",X_users_inputs[l]);}
        ++l_count;
        }
    else if(k== 1) {
        O_user_inputs[i-1] = input;
        for(int s =0; s < s_count; s++){
            printf("Y user %d\n",X_users_inputs[s]);}
        ++s_count;}

    k = ((i % 2 == 0) ? 0 : 1);
  }
  return 0;
}

Output

Player X input: 1
X user 1

Player O input: 2
Y user 1

Player X input: 3
X user 1
X user 0

Player O input: 4
Y user 1
Y user 0

Expected Output

Player X input: 1
X user 1

Player O input: 2
Y user 2

Player X input: 3
X user 1
X user 3

Player O input: 4
Y user 2
Y user 4

Aucun commentaire:

Enregistrer un commentaire