Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    0
    ReputationRep:






    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.

    Offline

    0
    ReputationRep:
    Double U Tee EFF...
    Offline

    2
    ReputationRep:
    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
    	add up diagonal here
    }while(diagonal < 7)
    Print numberOfTries
    • Thread Starter
    Offline

    0
    ReputationRep:
    hmm, I tried a do-while loop like you suggested and it makes more sense now, but I'm still not getting it.
    • Thread Starter
    Offline

    0
    ReputationRep:
    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.
    Offline

    20
    ReputationRep:
    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.
    • Thread Starter
    Offline

    0
    ReputationRep:
    (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 forgot to correct the previous poster about this too
    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 :beard:
    Offline

    20
    ReputationRep:
    (Original post by purplerainn)
    I forgot to correct the previous poster about this too
    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 :beard:
    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
    >=7 instead of ==7

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

    10
    ReputationRep:
    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
 
 
 
  • 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
    Has a teacher ever helped you cheat?
    Useful resources
  • 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

    Write a reply...
    Reply
    Hide
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.