Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Programming Array Loop

Tags:

c

Any help would be much appreciated with this problem I've been having. I'm trying to create a loop to find a 30x30 array whose main diagonal values are all greater or equal than 7. The arrays are filled with random integers.

My issue is the loop itself, I can't seem to figure out how to create such a large series of loops.. driving me nuts!

When I've managed to create an array that meets the criteria, I need to display how many attempts it took. If it takes more than 1 million attempts, the program should simply "give up", displaying that it is not possible.

This is what i've managed to come up with so far (not the entire code, just the bit I need help with), however the output is literally nothing. I guarantee I'm not even close to the right solution... but I would appreciate a point in the right direction!

Thanks in advance :)

count=0;

for(a=0;a<30;++a)
for(b=0;b<30;++b)
    random2[a][b]=rand()%10;

while(count<=1000000)
{
for(a=0;a<30;++a)
{
    if(random2[a][a]>=7)
    {    
        ++a;
        if(a==30&&random2[a][a]>=7)
            printf("%d", count);
    }
        else
        {
            ++count;
            for(a=0;a<30;++a)
                for(b=0;b<30;++b)
                    random2[a][b]=rand()%10;
        }
}
}

printf("%d", count);                    
like image 716
user2229471 Avatar asked Aug 01 '26 05:08

user2229471


2 Answers

You have done some odd things in your loops. The simple fixes are to remove one of the ++as (I'd suggest the second), and to break in the else.

The better fix is to code as you mean. To minimally change your example:

int count,a,b;
for(count = 0; count<=1000000; ++ count) {
    for(a=0;a<30;++a)
        for(b=0;b<30;++b)
            random2[a][b]=rand()%10;
    for(a=0;a<30;++a)
    {
        if(random2[a][a]>=7)
        {
            if(a==30&&random2[a][a]>=7)
            printf("%d", count);
        }
        else
        {
            break;
        }
    }
    if( a == 30 ) {
        break;
    }
}

An even better fix is to calculate your array algorithmically instead of relying on random chance to give you a good result. For example, set your diagonal to be randomly 7, 8, or 9 (which would give the same end result and probabilities).

like image 71
Dave Avatar answered Aug 06 '26 02:08

Dave


You are mixing the inner for-loop counter 'a' with the value counter (which is also 'a' in your program). Try something like this:

int found = 0;
for (count = 1; count <= 1000000 && !found; count++) {
  // (re)fill the array with random numbers
  for (a = 0; a < 30; a++) for (b = 0; b < 30; b++) random2[a][b] = rand() % 10;
  // now check the array
  found = 1; // assume all values are >= 7
  for (a = 0; a < 30; a++)
    if (random2[a][a] < 7) { // found a bad value
      found = 0;
      break;
    }
}
like image 24
nullptr Avatar answered Aug 06 '26 03:08

nullptr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!