Home > OS >  for loop doesn't run with the rest of the code
for loop doesn't run with the rest of the code

Time:09-21

I am very new to C programming language, and I ran into a problem. I need to write code that displays the number of times the numbers 3 and 6 appear consecutively in a array[100] with random integers. I have tried writing my code in different ways, however, whenever I run the code it doesn't execute along with the rest of the code. Here are my attempts.

Created another for loop and an if condition inside:

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

#define SIZE 100
#define MAX 990
#define MIN 100

int main(void) {

  int index;
  int array[SIZE];
  int q;

  srand(time(NULL));

  for (index = 0; index < SIZE; index  ){
    
    array[index] = (rand() % (MAX - MIN   1)   MIN) / 10;

    printf("Índice [%i]: %i\n", index, array[index]); 
  }

  for(index = 0; index < SIZE; index  ){
    if (array[index] == 3 && array[index   1] == 6){
      q  ;
      printf("Relatório: 3 e 6 foram encontrados consecutivamente %i vezes.", q);
    }
  }

  
  return 0;
}

Removed the for loop and left the if statement alone:

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

#define SIZE 100
#define MAX 990
#define MIN 100

int main(void) {

  int index;
  int array[SIZE];
  int q;

  srand(time(NULL));

  for (index = 0; index < SIZE; index  ){
    
    array[index] = (rand() % (MAX - MIN   1)   MIN) / 10;

    printf("Índice [%i]: %i\n", index, array[index]); 
  }

 
    if (array[index] == 3 && array[index   1] == 6){
      q  ;
      printf("Relatório: 3 e 6 foram encontrados consecutivamente %i vezes.", q);
    }


  
  return 0;
}

I added the if condition in the first for loop, which was the only one to be executed when I clicked run:

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

#define SIZE 100
#define MAX 990
#define MIN 100

int main(void) {

  int index;
  int array[SIZE];
  int q;

  srand(time(NULL));

  for (index = 0; index < SIZE; index  ){
    
    array[index] = (rand() % (MAX - MIN   1)   MIN) / 10;

    printf("Índice [%i]: %i\n", index, array[index]); 

    if (array[index] == 3 && array[index   1] == 6){
      q  ;
      printf("Relatório: 3 e 6 foram encontrados consecutivamente %i vezes.", q);
    }
  }


  
  return 0;
}

What am I doing wrong and why didn't any of the solutions above work? How can I make it work?

CodePudding user response:

Beyond the

  • out-of-bounds access for array[index 1] and
  • q being uninitialized (your compiler will have warned about this)

the real issue is you've set up MIN and MAX to never give you 3 or 6 in the array.

With a smaller range, this occasionally finds a 3-6 sequence (godbolt link rigged with a static srand seed).

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

#define SIZE 100

int main(void) {
  int index;
  int array[SIZE];
  int q = 0;
  srand(time(NULL));

  for (index = 0; index < SIZE; index  ) {
    array[index] = 1   rand() % 7;
  }

  for (index = 0; index < SIZE - 1; index  ) {
    if (array[index] == 3 && array[index   1] == 6) {
      q  ;
    }
  }

  printf("Relatório: 3 e 6 foram encontrados consecutivamente %i vezes.", q);

  return 0;
}

CodePudding user response:

Several other respondents have noted "referencing out of bounds" on the indexing. That bug is hard to see with so much code.

You can populate the array AND detect the sequence at the same time.

Here, the modulo range is made functional giving more 'hits' and the array expanded for more 'hits', too.

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

#define SIZE 500
#define MOD_VAL (rand() % 7) // 0,1,2,..6

int main() {
    srand( time( NULL ) );

    int i = 0, count = 0, arr[ SIZE ];

    arr[ i ] = MOD_VAL; // this is the first
    while(   i < SIZE ) // cannot step out of bounds by looking backwards
        arr[i] = MOD_VAL;
        // Is this 6 and was previous value 3 ?
        if( arr[i] == 6 && arr[i-1] == 3 ) {
            printf( "[%i] & [%i]\n", i-1, i );
            count  ;
        }

    printf( "Count: %i", count );

    return 0;
}

Output

[73] & [74]
[125] & [126]
[283] & [284]
[370] & [371]
[405] & [406]
[484] & [485]
Count: 6
  • Related