You are Here: Home

C Loop question Watch

1. This is what I've done so far. Creating the arrays is fine. Just need help on the loop; not really sure what to do other than what I've tried.

2. Double U Tee EFF...
3. Don't know any C, but you will probably want an outer do, while loop, to create the array and check whether the main diagonal is greater than or equal to 7, and if not starting again, incrementing an int keeping track of the number of tries. So inside this loop you will have the code to create the array like you have above, then at the end a check to add up the main diagonal.
So roughly

Code:
```numberOfTries = 0
do{
numberOfTries = numberOfTries  + 1
Generate the array here
}while(diagonal < 7)
Print numberOfTries```
4. hmm, I tried a do-while loop like you suggested and it makes more sense now, but I'm still not getting it.
5. I'm sure, it's something along the lines of

while( something )
{

create loop

for i=0,i<30,i++
if(my_array[i][i]<7)
counter++

}

I don't know what that something is though.
6. Since you want to stop when you've found an array where the sum of diagonal elements is 7, your something should be
Code:
`counter == 0`
you'd want another variable to store the number of arrays you've tried in.
7. (Original post by Joinedup)
Since you want to stop when you've found an array where the sum of diagonal elements is 7, your something should be
Code:
`counter == 0`
you'd want another variable to store the number of arrays you've tried in.
I want it to stop when all the elements in the main diagonal are >=7

And I'm also supposed to stop it from looping if such an array isn't produced within a million tries
8. (Original post by purplerainn)
I want it to stop when all the elements in the main diagonal are >=7

And I'm also supposed to stop it from looping if such an array isn't produced within a million tries
well if you had a variable called trycount which you incremented every time you tried a new array... You could use

while (counter == 0 && trycount < 1000000 )

to make continuing the loop depend on two conditions.

in your inner loop you need to add the value at array [i][i] to a variable each new value of i ... Then after the loop test that variable using

you need to store zero in that variable every time before starting the for loop otherwise it'll contain the total for *all* the arrays so far instead of starting at zero for each new array.
9. Is there any reason why you are filling the array row by row ?

Since C random number generator is uniformly distributed and the generating is independant, the probability of generating a valid array by generating the main diagonal first then filling the rest of the array is the same as the probablility of generating a valid array by generating rows of the array. But the first method is computationaly much cheaper.
Code:
```#include <time.h>
#include <stdlib.h>

//main part
//start random seed
srand(time(NULL));

int validDiagonalEntriesCount = 0;
int totalAttempts = 0;

//Will generate until valid diagonal is created, number of elements in diagonal is the array dimension
while ( validDiagonalEntriesCount < arraySize || totalAttempts <= maxNumberAttempts ){

//if random generated value is valid for diagonal  increment else reset
validDiagonalEntriesCount =  rand() % 10 >= 7 ? validDiagonalEntriesCount++ :0;

//since there was a reset (entries == 0) increment attempts
totalAttempts += i == 0 ? 1:0;
}

printf("%i" , totalAttempts);```
Also please next time paste code to http://pastebin.com/ or direct paste rather the screen shot. Your code looks cryptic

TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

This forum is supported by:
Updated: March 30, 2013
Today on TSR

Should I ask for his number?

Discussions on TSR

• Latest
• See more of what you like on The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

• Poll
Useful resources

Groups associated with this forum:

View associated groups
Discussions on TSR

• Latest
• See more of what you like on The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

• The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE