Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Array problem

  • 16-03-2009 4:47pm
    #1
    Registered Users, Registered Users 2 Posts: 26,584 ✭✭✭✭


    i have a 2d array of a fixed size, say 5 x 5 and i assign it random values. i want to be able to ask the user how many numbers they want to turn to 0 and then zero elements in the array. for example...

    the array looks like this.
    4, 5, 6, 7, 8,
    2, 12, 345, 554, 32,
    6, 3, 1, 7, 3,
    122, 145, 312 ,211, 111,
    999, 223, 221, 111, 124
    
    i want to then ask the user to input a value between 0-25 and have it zero out the elements based on this number. so if the user inputs 4 the array will look like this.
    4, 0, 0, 0, 0,
    2, 12, 345, 554, 32,
    6, 3, 1, 7, 3,
    122, 145, 312 ,211, 111,
    999, 223, 221, 111, 124
    
    or if they enter in greater than 5 elements i.e. 8 elements, the array looks like this:
    0, 0, 0, 0, 0,
    2, 12, 0, 0, 0,
    6, 3, 1, 7, 3,
    122, 145, 312 ,211, 111,
    999, 223, 221, 111, 124
    

    below is what i've got so far, i know it's wrong and i'm probably overlooking something so simple and just can't see it. what the code below does is it zeros out everything.
    #include <stdio.h>
    
    int main(int argc, char** argv)
    {
        int array[5][5] = {    4, 5, 6, 7, 8,
                        2, 12, 345, 554, 32,
                        6, 3, 1, 7, 3,
                    122, 145, 312 ,211, 111,
                    999, 223, 221, 111, 124
                  };
        int i, j ;
        int user_input = 0;
        int loop_counter = 0;    
    
        printf("How many values do you want to zero out?\n");
        scanf("%d",&user_input);
    
    
    
        for(i = 0 ; i < 5 ; i++)
        {
            for(j = 5 ; j >= 0 ; j--)
            {
                array[i][j] = 0 ; 
                loop_counter++;
                printf("element [%d,%d] has the value of %d\n", i, j, array[i][j]);
            }
            
            if(loop_counter == user_input)
                break;
    
        }
    }
    

    anyone have any ideas?


Comments

  • Moderators Posts: 51,917 ✭✭✭✭Delirium


    for(i = 0 ; i < 5 ; i++)
        {
            for(j = 5 ; j >= 0 ; j--)
            {
                array[i][j] = 0 ; 
                loop_counter++;
                printf("element [%d,%d] has the value of %d\n", i, j, array[i][j]);
            }
     
            if(loop_counter == user_input)
                break;
     
        }
    

    I would think the if statement and break should be inside the inner for loop.
    for(i = 0 ; i < 5 ; i++)
        {
            for(j = 5 ; j >= 0 ; j--)
            {
                array[i][j] = 0 ; 
                loop_counter++;
                printf("element [%d,%d] has the value of %d\n", i, j, array[i][j]);
     
                if(loop_counter == user_input)
                  break;
     
            }
     
     
        }
    

    If you can read this, you're too close!



  • Registered Users, Registered Users 2 Posts: 6,240 ✭✭✭hussey


    Not quite ..
    the logic above is correct the loop needs to break on the 2nd loop and 1st

    teh break would only break out of the 2nd loop and 1st would continue

    the reason is the loop would go like this

    <do 5 iterations of inner loop>
    check the break
    <do another 5>

    so if it was 4 the user typed in
    if(loop_counter == user_input) = if (5 == 4)

    also you have
    for(j = 5 ; j >= 0 ; j--) array[j] = 0 ;

    would this not look for array[0][5] first?? throw a Null pointer as it should be [0][4] first

    boolean breakFlag = true;
    
            for(i = 0 ; i < 5 && breakFlag; i++)
            {
                for(j = 4 ; j >= 0 && breakFlag; j--)
                {
                    array[i][j] = 0 ; 
                    loop_counter++;
                    System.err.println("element ["+i+","+j+"] has the value of " + array[i][j]);
                    
                    if(loop_counter == user_input)
                        breakFlag = false;
                }     
            }
    

    works


  • Closed Accounts Posts: 60 ✭✭LARDO


    hussey wrote: »
    Not quite ..
    the logic above is correct the loop needs to break on the 2nd loop and 1st

    teh break would only break out of the 2nd loop and 1st would continue

    the reason is the loop would go like this

    <do 5 iterations of inner loop>
    check the break
    <do another 5>

    so if it was 4 the user typed in
    if(loop_counter == user_input) = if (5 == 4)

    also you have
    for(j = 5 ; j >= 0 ; j--) array[j] = 0 ;

    would this not look for array[0][5] first?? throw a Null pointer as it should be [0][4] first

    boolean breakFlag = true;
    
            for(i = 0 ; i < 5 && breakFlag; i++)
            {
                for(j = 4 ; j >= 0 && breakFlag; j--)
                {
                    array[i][j] = 0 ; 
                    loop_counter++;
                    System.err.println("element ["+i+","+j+"] has the value of " + array[i][j]);
                    
                    if(loop_counter == user_input)
                        breakFlag = false;
                }     
            }
    

    works

    some compilers have different ideas of operator precedence and may evaluate "j >= 0 && breakFlag" in an unanticipated way ie not left to right,
    "(j >= 0) && breakFlag is safer.


  • Registered Users, Registered Users 2 Posts: 26,584 ✭✭✭✭Creamy Goodness


    cheers guys that works.

    i'm now trying to get this part working.

    i ask the user how many elements they want to display first of all. and then ask them how many they want to zero out.

    so it looks like this.
    #include <stdio.h>
    
    int main(int argc, char** argv)
    {
        int array[5][5] = {    4, 5, 6, 7, 8,
                        2, 12, 345, 554, 32,
                        6, 3, 1, 7, 3,
                    122, 145, 312 ,211, 111,
                    999, 223, 221, 111, 124
                  };
        int i, j ;
        int user_input = 0;
        int loop_counter = 0;    
       
        printf("how many values of the array do you want to display?\n");
        scanf("%d", &user_input);
    
        //print the array for how many elements they want to display.
        for(i = 0 ; i < 5 ; i++)
        {
              for(j = 0 ; j < user_input ; j++)
              {
                     printf("element [%d,%d] has the value of %d\n", i, j, array[i][j]);
               }
        }       
    
    
    
        printf("How many values do you want to zero out?\n");
        scanf("%d",&user_input);
    
       int flag = 1
        for(i = 0 ; (i < 5) && flag == 1  ; i++)
        {
            for(j = 4 ; (j >= 0) && flag == 1 ; j--)
            {
                array[i][j] = 0 ; 
                loop_counter++;
                printf("element [%d,%d] has the value of %d\n", i, j, array[i][j]);
            }
            
            if(loop_counter == user_input)
                break;
    
        }
    }
    

    so a sample run of the program would run like this:

    How many elements do you wish to display: 7
    4, 5, 6, 7, 8,
    2, 12

    how many elements do you wish to zero out: 6
    0, 0, 0, 0, 0,
    2, 0


  • Registered Users, Registered Users 2 Posts: 2,297 ✭✭✭Ri_Nollaig


        int size = 5;  
    
        ...
    
        printf("How many values do you want to zero out?\n");
        scanf("%d",&user_input);
       
        for(i = 0 ; i < size; i++)
        {
            for(j = size - 1; j >= 0; j--)
            {
     
                if(loop_counter != user_input) {
                   array[i][j] = 0 ; 
                   loop_counter++;
                }
                printf("%d ",  array[i][((size - j) - 1)]);
            }
            printf("\n");
        }
    }
    

    im at work so cant test, but I think that should work for you.


  • Advertisement
Advertisement