Placing random values in array











up vote
2
down vote

favorite












I'm new to coding and debugging. As an assignment I'm trying to make a Nim game for two players. However whenever I reach a point in my code it give me a segmentation fault. I thought the problem would be something as pointing to a wrong place in an array, but changing those values did not help. Eventually I tried to debug using gdb, and it said that I had a segmentation fault at the line where I assign a random number to the array. I wouldn't know what to do with the information I got while debugging though. Here's the part where I assign random numbers from 1 to 6 into an array:



for (int x; x < aantalrijen; x++) {
if (geinitialiseerd == 0) {
fiches[x] = rand()% 6 + 1 ;
geinitialiseerd = 1;
}
printf("ntRij %d:t", x+1);
for (int y = 0; y < fiches[x]; y++) {
printf(" @ ");
}
fichesum = fichesum + fiches[x];
}
}


Surprisingly enough I only get the segmentation fault if I enter a 1 or a 10 as aantalrijen, but not when I enter anything inbetween. However the code still doesn;t work as it should when I do enter a number from 2 to 9. This is the entire code I have written up to now, even though it's not complete yet:



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

//Initialiseren van speler namen, wiens beurt het is, de aantal rijen,
de aantal fiches op ieder rij, het totaal aantal fiches en de variant
die gespeeld wordt.
char speler1[10];
char speler2[10];
int geinitialiseerd;
int beurt;
int aantalrijen;
int fiches[10];
int fichesum;
int rijnemen;
int aantalnemen;
int variant;

int output () {

//For loop voor initialisatie van het spel. Hiermee wordt bedoelt dat x aantal rijen een random waarde krijgen tussen 1 en 6. Deze worden ook meteen geprint.
for (int x; x < aantalrijen; x++) {
if (geinitialiseerd == 0) {
fiches[x] = rand()% 6 + 1 ;
geinitialiseerd = 1;
}
printf("ntRij %d:t", x+1);
for (int y = 0; y < fiches[x]; y++) {
printf(" @ ");
}
fichesum = fichesum + fiches[x];
}
}

int main () {

//Introductie en vraag om namen
printf("==========================================================================================n**Welkom bij Nim! Dit is een spel gemaakt door Michael Francis voor twee spelers!t**n==========================================================================================n**Wat is de naam van speler 1?tttttttt**n");
scanf("%s", &speler1);
printf("**Wat is de naam van speler 2?tttttttt**n");
scanf("%s", &speler2);
printf("n**Kies het aantal rijen waarmee u wilt spelen (tussen de 1 en 10):ttt**n==========================================================================================n");
scanf("%d", &aantalrijen);
printf("==========================================================================================n**Kies de variant die u wilt spelen:ttttttt**nt1. Verliezer neemt laatste fichent2. Winnaar neemt laatste fichen==========================================================================================n");
scanf("%d", &variant);
printf("==========================================================================================n**Nieuw gegenereerd spel:tttttttt**n");
output();
printf("nn==========================================================================================n");
fichesum = 100;
while (fichesum != 0) {

//For-loop om huidige fiches uit te printen
//for (int a; a < aantalrijen; a++) {
// printf("ntRij %d:t", a+1);
// for (int b = 0; b < fiches[a]; b++) {
// printf(" @ ");
// }
//}
output();

fichesum = 0;
if (beurt == 0) {
printf("nn==========================================================================================n%s is aan de beurt!", speler1);
beurt = 1;
} else {
printf("nn==========================================================================================n%s is aan de beurt!", speler2);
beurt = 0;
}
printf("nUit welk rij wilt u fiches nemen?n");
scanf("%d", &rijnemen - 1);
printf("nHoeveel fiches wilt u nemen?n");
scanf("%d", &aantalnemen);
fiches[rijnemen] = fiches[rijnemen] - aantalnemen;
printf("%d", fiches[rijnemen]);

}

return 0;
}









share|improve this question


























    up vote
    2
    down vote

    favorite












    I'm new to coding and debugging. As an assignment I'm trying to make a Nim game for two players. However whenever I reach a point in my code it give me a segmentation fault. I thought the problem would be something as pointing to a wrong place in an array, but changing those values did not help. Eventually I tried to debug using gdb, and it said that I had a segmentation fault at the line where I assign a random number to the array. I wouldn't know what to do with the information I got while debugging though. Here's the part where I assign random numbers from 1 to 6 into an array:



    for (int x; x < aantalrijen; x++) {
    if (geinitialiseerd == 0) {
    fiches[x] = rand()% 6 + 1 ;
    geinitialiseerd = 1;
    }
    printf("ntRij %d:t", x+1);
    for (int y = 0; y < fiches[x]; y++) {
    printf(" @ ");
    }
    fichesum = fichesum + fiches[x];
    }
    }


    Surprisingly enough I only get the segmentation fault if I enter a 1 or a 10 as aantalrijen, but not when I enter anything inbetween. However the code still doesn;t work as it should when I do enter a number from 2 to 9. This is the entire code I have written up to now, even though it's not complete yet:



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

    //Initialiseren van speler namen, wiens beurt het is, de aantal rijen,
    de aantal fiches op ieder rij, het totaal aantal fiches en de variant
    die gespeeld wordt.
    char speler1[10];
    char speler2[10];
    int geinitialiseerd;
    int beurt;
    int aantalrijen;
    int fiches[10];
    int fichesum;
    int rijnemen;
    int aantalnemen;
    int variant;

    int output () {

    //For loop voor initialisatie van het spel. Hiermee wordt bedoelt dat x aantal rijen een random waarde krijgen tussen 1 en 6. Deze worden ook meteen geprint.
    for (int x; x < aantalrijen; x++) {
    if (geinitialiseerd == 0) {
    fiches[x] = rand()% 6 + 1 ;
    geinitialiseerd = 1;
    }
    printf("ntRij %d:t", x+1);
    for (int y = 0; y < fiches[x]; y++) {
    printf(" @ ");
    }
    fichesum = fichesum + fiches[x];
    }
    }

    int main () {

    //Introductie en vraag om namen
    printf("==========================================================================================n**Welkom bij Nim! Dit is een spel gemaakt door Michael Francis voor twee spelers!t**n==========================================================================================n**Wat is de naam van speler 1?tttttttt**n");
    scanf("%s", &speler1);
    printf("**Wat is de naam van speler 2?tttttttt**n");
    scanf("%s", &speler2);
    printf("n**Kies het aantal rijen waarmee u wilt spelen (tussen de 1 en 10):ttt**n==========================================================================================n");
    scanf("%d", &aantalrijen);
    printf("==========================================================================================n**Kies de variant die u wilt spelen:ttttttt**nt1. Verliezer neemt laatste fichent2. Winnaar neemt laatste fichen==========================================================================================n");
    scanf("%d", &variant);
    printf("==========================================================================================n**Nieuw gegenereerd spel:tttttttt**n");
    output();
    printf("nn==========================================================================================n");
    fichesum = 100;
    while (fichesum != 0) {

    //For-loop om huidige fiches uit te printen
    //for (int a; a < aantalrijen; a++) {
    // printf("ntRij %d:t", a+1);
    // for (int b = 0; b < fiches[a]; b++) {
    // printf(" @ ");
    // }
    //}
    output();

    fichesum = 0;
    if (beurt == 0) {
    printf("nn==========================================================================================n%s is aan de beurt!", speler1);
    beurt = 1;
    } else {
    printf("nn==========================================================================================n%s is aan de beurt!", speler2);
    beurt = 0;
    }
    printf("nUit welk rij wilt u fiches nemen?n");
    scanf("%d", &rijnemen - 1);
    printf("nHoeveel fiches wilt u nemen?n");
    scanf("%d", &aantalnemen);
    fiches[rijnemen] = fiches[rijnemen] - aantalnemen;
    printf("%d", fiches[rijnemen]);

    }

    return 0;
    }









    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I'm new to coding and debugging. As an assignment I'm trying to make a Nim game for two players. However whenever I reach a point in my code it give me a segmentation fault. I thought the problem would be something as pointing to a wrong place in an array, but changing those values did not help. Eventually I tried to debug using gdb, and it said that I had a segmentation fault at the line where I assign a random number to the array. I wouldn't know what to do with the information I got while debugging though. Here's the part where I assign random numbers from 1 to 6 into an array:



      for (int x; x < aantalrijen; x++) {
      if (geinitialiseerd == 0) {
      fiches[x] = rand()% 6 + 1 ;
      geinitialiseerd = 1;
      }
      printf("ntRij %d:t", x+1);
      for (int y = 0; y < fiches[x]; y++) {
      printf(" @ ");
      }
      fichesum = fichesum + fiches[x];
      }
      }


      Surprisingly enough I only get the segmentation fault if I enter a 1 or a 10 as aantalrijen, but not when I enter anything inbetween. However the code still doesn;t work as it should when I do enter a number from 2 to 9. This is the entire code I have written up to now, even though it's not complete yet:



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

      //Initialiseren van speler namen, wiens beurt het is, de aantal rijen,
      de aantal fiches op ieder rij, het totaal aantal fiches en de variant
      die gespeeld wordt.
      char speler1[10];
      char speler2[10];
      int geinitialiseerd;
      int beurt;
      int aantalrijen;
      int fiches[10];
      int fichesum;
      int rijnemen;
      int aantalnemen;
      int variant;

      int output () {

      //For loop voor initialisatie van het spel. Hiermee wordt bedoelt dat x aantal rijen een random waarde krijgen tussen 1 en 6. Deze worden ook meteen geprint.
      for (int x; x < aantalrijen; x++) {
      if (geinitialiseerd == 0) {
      fiches[x] = rand()% 6 + 1 ;
      geinitialiseerd = 1;
      }
      printf("ntRij %d:t", x+1);
      for (int y = 0; y < fiches[x]; y++) {
      printf(" @ ");
      }
      fichesum = fichesum + fiches[x];
      }
      }

      int main () {

      //Introductie en vraag om namen
      printf("==========================================================================================n**Welkom bij Nim! Dit is een spel gemaakt door Michael Francis voor twee spelers!t**n==========================================================================================n**Wat is de naam van speler 1?tttttttt**n");
      scanf("%s", &speler1);
      printf("**Wat is de naam van speler 2?tttttttt**n");
      scanf("%s", &speler2);
      printf("n**Kies het aantal rijen waarmee u wilt spelen (tussen de 1 en 10):ttt**n==========================================================================================n");
      scanf("%d", &aantalrijen);
      printf("==========================================================================================n**Kies de variant die u wilt spelen:ttttttt**nt1. Verliezer neemt laatste fichent2. Winnaar neemt laatste fichen==========================================================================================n");
      scanf("%d", &variant);
      printf("==========================================================================================n**Nieuw gegenereerd spel:tttttttt**n");
      output();
      printf("nn==========================================================================================n");
      fichesum = 100;
      while (fichesum != 0) {

      //For-loop om huidige fiches uit te printen
      //for (int a; a < aantalrijen; a++) {
      // printf("ntRij %d:t", a+1);
      // for (int b = 0; b < fiches[a]; b++) {
      // printf(" @ ");
      // }
      //}
      output();

      fichesum = 0;
      if (beurt == 0) {
      printf("nn==========================================================================================n%s is aan de beurt!", speler1);
      beurt = 1;
      } else {
      printf("nn==========================================================================================n%s is aan de beurt!", speler2);
      beurt = 0;
      }
      printf("nUit welk rij wilt u fiches nemen?n");
      scanf("%d", &rijnemen - 1);
      printf("nHoeveel fiches wilt u nemen?n");
      scanf("%d", &aantalnemen);
      fiches[rijnemen] = fiches[rijnemen] - aantalnemen;
      printf("%d", fiches[rijnemen]);

      }

      return 0;
      }









      share|improve this question













      I'm new to coding and debugging. As an assignment I'm trying to make a Nim game for two players. However whenever I reach a point in my code it give me a segmentation fault. I thought the problem would be something as pointing to a wrong place in an array, but changing those values did not help. Eventually I tried to debug using gdb, and it said that I had a segmentation fault at the line where I assign a random number to the array. I wouldn't know what to do with the information I got while debugging though. Here's the part where I assign random numbers from 1 to 6 into an array:



      for (int x; x < aantalrijen; x++) {
      if (geinitialiseerd == 0) {
      fiches[x] = rand()% 6 + 1 ;
      geinitialiseerd = 1;
      }
      printf("ntRij %d:t", x+1);
      for (int y = 0; y < fiches[x]; y++) {
      printf(" @ ");
      }
      fichesum = fichesum + fiches[x];
      }
      }


      Surprisingly enough I only get the segmentation fault if I enter a 1 or a 10 as aantalrijen, but not when I enter anything inbetween. However the code still doesn;t work as it should when I do enter a number from 2 to 9. This is the entire code I have written up to now, even though it's not complete yet:



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

      //Initialiseren van speler namen, wiens beurt het is, de aantal rijen,
      de aantal fiches op ieder rij, het totaal aantal fiches en de variant
      die gespeeld wordt.
      char speler1[10];
      char speler2[10];
      int geinitialiseerd;
      int beurt;
      int aantalrijen;
      int fiches[10];
      int fichesum;
      int rijnemen;
      int aantalnemen;
      int variant;

      int output () {

      //For loop voor initialisatie van het spel. Hiermee wordt bedoelt dat x aantal rijen een random waarde krijgen tussen 1 en 6. Deze worden ook meteen geprint.
      for (int x; x < aantalrijen; x++) {
      if (geinitialiseerd == 0) {
      fiches[x] = rand()% 6 + 1 ;
      geinitialiseerd = 1;
      }
      printf("ntRij %d:t", x+1);
      for (int y = 0; y < fiches[x]; y++) {
      printf(" @ ");
      }
      fichesum = fichesum + fiches[x];
      }
      }

      int main () {

      //Introductie en vraag om namen
      printf("==========================================================================================n**Welkom bij Nim! Dit is een spel gemaakt door Michael Francis voor twee spelers!t**n==========================================================================================n**Wat is de naam van speler 1?tttttttt**n");
      scanf("%s", &speler1);
      printf("**Wat is de naam van speler 2?tttttttt**n");
      scanf("%s", &speler2);
      printf("n**Kies het aantal rijen waarmee u wilt spelen (tussen de 1 en 10):ttt**n==========================================================================================n");
      scanf("%d", &aantalrijen);
      printf("==========================================================================================n**Kies de variant die u wilt spelen:ttttttt**nt1. Verliezer neemt laatste fichent2. Winnaar neemt laatste fichen==========================================================================================n");
      scanf("%d", &variant);
      printf("==========================================================================================n**Nieuw gegenereerd spel:tttttttt**n");
      output();
      printf("nn==========================================================================================n");
      fichesum = 100;
      while (fichesum != 0) {

      //For-loop om huidige fiches uit te printen
      //for (int a; a < aantalrijen; a++) {
      // printf("ntRij %d:t", a+1);
      // for (int b = 0; b < fiches[a]; b++) {
      // printf(" @ ");
      // }
      //}
      output();

      fichesum = 0;
      if (beurt == 0) {
      printf("nn==========================================================================================n%s is aan de beurt!", speler1);
      beurt = 1;
      } else {
      printf("nn==========================================================================================n%s is aan de beurt!", speler2);
      beurt = 0;
      }
      printf("nUit welk rij wilt u fiches nemen?n");
      scanf("%d", &rijnemen - 1);
      printf("nHoeveel fiches wilt u nemen?n");
      scanf("%d", &aantalnemen);
      fiches[rijnemen] = fiches[rijnemen] - aantalnemen;
      printf("%d", fiches[rijnemen]);

      }

      return 0;
      }






      c






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 22:53









      0963038

      133




      133
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          If you compile with -Wall -O2, you'd get a warning on the for loop in output:



          dut.c: In function ‘output’:
          dut.c:21:11: warning: ‘x’ may be used uninitialized in this function [-Wmaybe-uninitialized]
          for (int x; x < aantalrijen; x++) {
          ^


          Change:



          for (int x; x < aantalrijen; x++) {


          Into:



          for (int x = 0; x < aantalrijen; x++) {




          Another warning to fix



          Change:



              scanf("%s", &speler1);
          scanf("%s", &speler2);


          Into:



              scanf("%s", speler1);
          scanf("%s", speler2);





          share|improve this answer





















          • Awesome, that did the trick! Thank you very much!
            – 0963038
            Nov 11 at 13:22











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244216%2fplacing-random-values-in-array%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          If you compile with -Wall -O2, you'd get a warning on the for loop in output:



          dut.c: In function ‘output’:
          dut.c:21:11: warning: ‘x’ may be used uninitialized in this function [-Wmaybe-uninitialized]
          for (int x; x < aantalrijen; x++) {
          ^


          Change:



          for (int x; x < aantalrijen; x++) {


          Into:



          for (int x = 0; x < aantalrijen; x++) {




          Another warning to fix



          Change:



              scanf("%s", &speler1);
          scanf("%s", &speler2);


          Into:



              scanf("%s", speler1);
          scanf("%s", speler2);





          share|improve this answer





















          • Awesome, that did the trick! Thank you very much!
            – 0963038
            Nov 11 at 13:22















          up vote
          0
          down vote



          accepted










          If you compile with -Wall -O2, you'd get a warning on the for loop in output:



          dut.c: In function ‘output’:
          dut.c:21:11: warning: ‘x’ may be used uninitialized in this function [-Wmaybe-uninitialized]
          for (int x; x < aantalrijen; x++) {
          ^


          Change:



          for (int x; x < aantalrijen; x++) {


          Into:



          for (int x = 0; x < aantalrijen; x++) {




          Another warning to fix



          Change:



              scanf("%s", &speler1);
          scanf("%s", &speler2);


          Into:



              scanf("%s", speler1);
          scanf("%s", speler2);





          share|improve this answer





















          • Awesome, that did the trick! Thank you very much!
            – 0963038
            Nov 11 at 13:22













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          If you compile with -Wall -O2, you'd get a warning on the for loop in output:



          dut.c: In function ‘output’:
          dut.c:21:11: warning: ‘x’ may be used uninitialized in this function [-Wmaybe-uninitialized]
          for (int x; x < aantalrijen; x++) {
          ^


          Change:



          for (int x; x < aantalrijen; x++) {


          Into:



          for (int x = 0; x < aantalrijen; x++) {




          Another warning to fix



          Change:



              scanf("%s", &speler1);
          scanf("%s", &speler2);


          Into:



              scanf("%s", speler1);
          scanf("%s", speler2);





          share|improve this answer












          If you compile with -Wall -O2, you'd get a warning on the for loop in output:



          dut.c: In function ‘output’:
          dut.c:21:11: warning: ‘x’ may be used uninitialized in this function [-Wmaybe-uninitialized]
          for (int x; x < aantalrijen; x++) {
          ^


          Change:



          for (int x; x < aantalrijen; x++) {


          Into:



          for (int x = 0; x < aantalrijen; x++) {




          Another warning to fix



          Change:



              scanf("%s", &speler1);
          scanf("%s", &speler2);


          Into:



              scanf("%s", speler1);
          scanf("%s", speler2);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 23:33









          Craig Estey

          13.5k21029




          13.5k21029












          • Awesome, that did the trick! Thank you very much!
            – 0963038
            Nov 11 at 13:22


















          • Awesome, that did the trick! Thank you very much!
            – 0963038
            Nov 11 at 13:22
















          Awesome, that did the trick! Thank you very much!
          – 0963038
          Nov 11 at 13:22




          Awesome, that did the trick! Thank you very much!
          – 0963038
          Nov 11 at 13:22


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244216%2fplacing-random-values-in-array%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python