user2229471
user2229471

Reputation: 31

C Programming Array Loop

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);                    

Upvotes: 3

Views: 1844

Answers (3)

Aswin Murugesh
Aswin Murugesh

Reputation: 11070

i think there is a problem in the line

if(a==30&&random2[a][a]>=7)

since you say the array is 30x30, The maximum value of the indices is 29x29. so random2[30][30] does not exist. As to the code, try

while(count<=1000000)
{
  for(a=0;a<30;a++)
  {
    for(b=0;b<30;b++)
    {
        random2[a][b]=rand()%10;
        if(a==b)
        {
            while(random2[a][a]<7)
            {
                  ++count;
                  random2[a][b]=rand()%10;
            }
        }
    }
  }
}

this will give you the array you need

if you want the value of count to be less, use,

if(random2[a][a]<7)
{
    random2[a][a]=(rand()+7)%10
}

Upvotes: 1

nullptr
nullptr

Reputation: 11058

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;
    }
}

Upvotes: 1

Dave
Dave

Reputation: 46249

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).

Upvotes: 1

Related Questions