Thread: Hi, I need help with this code.

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    29

    Question Hi, I need help with this code.

    This code suppose to check if in every row and column every number appears only once.
    It only succeeds sometimes....
    Thanks...

    Code:
    #include <stdio.h>
    #define NUM 3
    int main()
    {
    int i,j,flag=1,l ;
    int mat[NUM][NUM] ;
    
    
    printf("please enter %d*%d matrice: \n" ,NUM ,NUM);
    for (i=0 ; i<NUM ; i++)
        for(j=0 ; j<=NUM ; j++)
                scanf("%d" ,&(mat[i][j]));
    
    for (j=0 ; j<NUM && flag!=0; j++)
         for(i=0; i<NUM && flag!=0; i++)
                  for(l=(i+1);l<NUM && flag!=0 ;l++)
                                    if (mat[i][j]!=mat[i+l][j])
                                       flag=1;
                                    else
                                        flag=0;
    
    for (i=0 ; i<NUM && flag!=0 ; i++)
           for(j=0 ; j<NUM && flag!=0; j++)
                   for(l=(j+1) ; l<NUM && flag!=0; l++)
                               if(mat[i][j]!=mat[i][j+l])
                                   flag=1;
                               else
                                   flag=0;
    
    if (flag==1)
       printf("YES\n");
    else 
         printf("NO\n");
    system("pause");
    return 0;
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    So besides just dumping your code here and asking us to fix it for free, would you like to pretend to be courteous and try to tell us exactly what you mean when your brain spits out the phrase "It only succeeds sometimes"?

    How about error messages.... output..... something to give us an idea where you messed up?

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    I suggest you to do the following:
    - First, initialize the multidimensional array, and insert its values into a new array:
    Code:
    int newarr[NUM * 2];
    - Sort the array (use bubble-sort or merge sort or w/e).
    - Go over the array and make a loop that has a condition in it which says: (NOTE that you gotta check the condition of your loop so it doesn't reach newarr[NUM * 2]: for(...;i < (num * 2) - 1;...)
    Code:
    if(newarr[i] == newarr[i + 1]) return -1;
    I'm returning -1 because a number is repeated twice (might be more but we don't care).
    If it doesn't return -1 and returns something else it means that the multidimensional array doesn't have "repeating-numbers" (numbers that appear more than one time).
    Last edited by eXeCuTeR; 07-07-2008 at 11:37 AM.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    29
    hmmmmm well thanks for trying to help, I guess. I thought that was one of the purposes of the forum. And that people just enjoy helping unpaid students with their questions in C Because they like programming and enjoy helping others.

    Maybe I can be more specific...

    For example,
    if I enter the matrix
    1 2 3
    4 5 6
    7 8 9

    it returns "yes"

    If I enter the matrix
    1 1 1
    2 2 2
    3 3 3

    it returns "no"

    but if i enter something like

    1 2 3
    4 4 5
    6 7 8

    it returns "yes" and it shouldn't.

    I appreciate your help.
    Thanks.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think the problem is that you are setting the flag on every iteration. You should initialise the flag to 0, then only when you find a match, set it to 1 and break from the loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I don't understand your algorithm.
    But your innermost loop accesses elements that don't exist
    Code:
    for(l=(j+1) ; l<NUM && flag!=0; l++)
         if(mat[i][j]!=mat[i][j+l])
    btw: using l as a variable is not very wise ( hard to see a difference to 1 ).
    Kurt

  7. #7
    Just a Human Anuradh_a's Avatar
    Join Date
    Jan 2008
    Posts
    50
    hey Livnat

    your code does not accept 3*3 matrix it accepts 3*4 one. i modified little bit of your code
    here is what i got


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define NUM 3
    int main()
    {
    int i,j,flag=1,l ;
    int mat[NUM][NUM] ;
    
    
    printf("please enter %d*%d matrice: \n" ,NUM ,NUM);
    for (i=0 ; i<NUM ; i++)
        for(j=0 ; j<NUM ; j++)
                scanf("%d" ,&(mat[i][j]));
    
    	i=j=l=0;
    for ( j=0 ; j<NUM && flag!=0; j++)
         for( i=0; i<NUM && flag!=0; i++)
            for( l=1;l<=NUM && flag!=0 ;l++)
     if ((mat[i][j]!=mat[i+l][j])&&(mat[i][j]!=mat[i][j+l]))
                                       flag=1;
    								
                                    else 
                                       flag=0;
    
    
    if (flag==1)
       printf("YES\n");
    else 
         printf("NO\n");
    system("pause");
    return 0;
    }

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    29
    Thank you all...
    It was all very helpful
    Last edited by Livnat; 07-07-2008 at 12:53 PM.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by Livnat View Post
    Thank you all...
    It was all very helpful
    This is what I did eventually...


    Code:
    #include <stdio.h>
    #define NUM 3
    int main()
    {
    int i,j,flag=0,l ;
    int mat[NUM][NUM] ;
    
    
    printf("please enter &#37;d*%d matrix: \n" ,NUM ,NUM);
    for (i=0 ; i<NUM ; i++)
        for(j=0 ; j<NUM ; j++)
                scanf("%d" ,&(mat[i][j]));
    
    for (j=0 ; j<NUM && flag==0; j++)
         for(i=0; i<NUM && flag==0; i++)
                  for(l=(i+1);l<NUM && flag==0 ;l++)
                                    if (mat[i][j]==mat[l][j])
                                       flag=1;
                                  
    
    for (i=0 ; i<NUM && flag==0 ; i++)
           for(j=0 ; j<NUM && flag==0; j++)
                   for(l=(j+1) ; l<NUM && flag==0; l++)
                               if(mat[i][j]==mat[i][l])
                                   flag=1;
                             
    if (flag==0)
       printf("YES\n");
    else
    if (flag==1)
         printf("NO\n");
    system("pause");
    return 0;
    }
    That's really not efficient, that works on O(n^3), you could have done it way better (look at my post)
    BTW, why did you post these 6 loops, only 3 is needed (not including the initialization)
    Code:
    for(int i = 0; i < NUM && flag == 0; i++)
     for(int j = 0; j < NUM && flag == 0; j++)
       for(int k = 0; k < NUM && flag == 0; k++)
              if(mat[i][j] == mat[i][k])
                     flag = 1;
    Last edited by eXeCuTeR; 07-07-2008 at 12:58 PM.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Are you trying to check for duplicates across all rows and columns, or just individually? For example:

    Code:
    please enter 3*3 matrix:
    0
    1
    2
    0
    3
    4
    5
    6
    7
    NO
    Press any key to continue . . .
    Code:
    please enter 3*3 matrix:
    0
    1
    2
    3
    0
    4
    5
    6
    7
    YES
    Press any key to continue . . .
    Is this intended behavior? Or should both examples show duplicates?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by eXeCuTeR
    That's really not efficient, that works on O(n^3), you could have done it way better (look at my post)
    Indeed, though bubble sort was the wrong thing to suggest for improving the complexity.

    Quote Originally Posted by MacGyver
    Are you trying to check for duplicates across all rows and columns, or just individually?
    It seems quite clear that the check is for duplicates across each row and each column, as that is how I interpret "if in every row and column every number appears only once".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by laserlight View Post
    Indeed, though bubble sort was the wrong thing to suggest for improving the complexity.


    It seems quite clear that the check is for duplicates across each row and each column, as that is how I interpret "if in every row and column every number appears only once".
    Yep, bubble sort is n^2 which is still faster than his code but anyways he could have used merge sort or any other good sort (heap sort and stuff..)

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yep, bubble sort is n^2 which is still faster than his code
    It is not faster than his code in terms of complexity since the code needs to check for duplicates across each row and column, so a sort merely speeds up the check across a particular row or column. Of course, if the duplicates are to be checked across the entire grid then it would indeed be faster, but a naive check for uniqueness that checks each element against every other element would also take quadratic time (and this is currently the case).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

Tags for this Thread