Thread: Loop that contains arrays

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    35

    Loop that contains arrays

    I have an assignment that requires me to write code that compares two arrays and confirms if every element in the first is equal to every element in the second. this is my code.
    Code:
    #include <stdio.h>
    
    #define DIM 16
    
    int main() {
       int ndx, vals[DIM], vals2[DIM];
    
       printf("Enter %d numbers: ", DIM);
       for (ndx = 0; ndx < DIM; ndx++)
          scanf("%d", &vals[ndx]);
    
       printf("And another %d numbers: ", DIM);
       for (ndx = 0; ndx < DIM; ndx++)
          scanf("%d", &vals2[ndx]);
       
       for (ndx = 0; vals[ndx] == vals2[ndx]; ndx++){}
       if (ndx == DIM + 2)
    	   printf("Equal\n");
       else
    	   printf("Not equal\n");
    
    }
    This code works (I came up with everything after the 2nd scanf). I get no compile errors and I pass all test cases. However, my professor's auto grader is not accepting it and it is not saying why. Can someone tell me what is wrong with my code? I don't see what it is. Thank you for your help.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    for (ndx = 0; vals[ndx] == vals2[ndx]; ndx++){}
       if (ndx == DIM + 2)
    	   printf("Equal\n");
       else
    	   printf("Not equal\n");
    First of all, I don't think the braces after the "for()" statement are intended to be like that.

    Second of all, you seem to have the logic all skewed. This is how I read it:

    (1) nxd = 0
    (2) if ndx == 16+2, print "equal" - otherwise, print "not equal"
    (3) increment ndx
    (4) if the values in the two arrays are not equal, the "for()" loop ends - otherwise, loop.

    You aren't basing the "equal"/"not equal" output on the equality of the array indices, but on the equality of your counter variable to a seemingly arbitrary value.

    Also, the "for()" loop is not guaranteed to stop before you overrun the bounds of your arrays; i.e., "ndx" can in some situations run beyond the value of DIM.
    Last edited by Matticus; 07-19-2011 at 10:58 PM.

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Why is it "DIM + 2" instead of just plain DIM?

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    35
    those braces are unnecessary i guess. so you're saying i'm not asking for "equal/not equal" based on weather or not the indices are equal, but only if my counter gets up to a number I want it to?

    my logic was:

    1. nxd = 0
    2. if val[nxd] == val2[nxd], increment ndx.
    3. repeat 1 and 2, until val[ndx] != val[ndx], stop the loop.
    4. if at the point the loop stops, ndx is not equal to the size of the NMD,
    that is, if I didn't get 16 "val[nxd] == val2[ndx] is true" instances, then the arrays were not equal.

    it seemed right and i got it to work how I wanted, but it seems a little strange to me now.

    it's DIM + 2, because when all of the indices of both arrays are equal, "ndx" goes up to 18. I don't know why it goes up to 18 though.
    Last edited by christianB; 07-19-2011 at 11:12 PM.

  5. #5
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    As Matticus says, you are looking at "array elements" out past the ends of your arrays once ndx exceeds DIM-1. There is no way of knowing what is in those memory locations. Maybe they contain the same information for a few more tries, but this is a matter of pure chance. It doesn't work on my machine for instance.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    1. I see what you were thinking but your implementation is flawed. You should never just allow the program to go marching into memory it doesn't own, aka unchecked incrementing past the bounds of your array. You are just asking for problems. You could do it in one line like:
    Code:
    for(ndx=0;(vals[ndx] == vals2[ndx]) && ndx < DIM;ndx++);
    This way you ensure you do not pass the bounds of your array.

    Why is it DIM+2? Well when you access memory that isn't yours you are into undefined behaviour and anything can happen. Don't do it.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    35
    so because I didn't have "ndx < DIM" as a condition in the loop it was going on past the 15th index of both the arrays? It was just my luck that those values happened to be equal huh? I understand though. Originally, i had the if statement in the loop, but that would just give me a whole column of "Equal"s, so I was just trying to find a way around that. Thanks again for the help.

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You had a good idea, just missing some pieces. It is all part of the learning process.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    35
    so this should work right?

    Code:
    #include <stdio.h>
    
    #define DIM 4
    
    int main() {
       int ndx, vals[DIM], vals2[DIM];
    
       printf("Please enter 16 numbers.\n");
       for (ndx = 0; ndx < DIM; ndx++){
    	   scanf("%d", &vals[ndx]);
       }
    
       printf("And another 16 numbers.\n");
       for (ndx = 0; ndx < DIM; ndx++){
    	   scanf("%d", &vals2[ndx]);
       }
    
       for(ndx=0; (vals[ndx] == vals2[ndx]) && ndx < DIM; ndx++);
    
       if (vals[ndx] == vals2[ndx])
    	   printf("Equal\n");
       else
    	   printf("Not equal\n");
    
    }

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
       for(ndx=0; (vals[ndx] == vals2[ndx]) && ndx < DIM; ndx++);
    
       if (vals[ndx] == vals2[ndx])
    	   printf("Equal\n");
    You've already taken care of the array comparison in the "for()" loop. Now check the "ndx" value in the "if()" statement. If the arrays were all equal, then "ndx" would have incremented all the way to DIM. If a set of array indices were not equal, then the loop would have broken and "ndx" would be less than DIM.

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Uh....no. Let's look:
    Code:
    #define DIM 4
    
    int main() {
       int ndx, vals[DIM], vals2[DIM];
    
       printf("Please enter 16 numbers.\n");
    You only are capable of taking in 4 numbers but you ask the user for 16.

    Code:
    //this will stop looping if either: values are not equal or you are at the last number of your array
    for(ndx=0; (vals[ndx] == vals2[ndx]) && ndx < DIM; ndx++);
    //that means you need to do if(ndx==DIM)....to continue with your logic
       if (vals[ndx] == vals2[ndx])
    	   printf("Equal\n");
       else
    	   printf("Not equal\n");
    Last edited by AndrewHunter; 07-20-2011 at 12:09 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Registered User
    Join Date
    Jul 2011
    Posts
    35
    of course. i'm just being redundant. it works now. thanks for all the help. I changed DIM to 4 because I was tired of typing 16 numbers over and over to test it. 16 is supposed to be a %d that reads in DIM. I changed it back though.

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by christianB View Post
    of course. i'm just being redundant. it works now. thanks for all the help. I changed DIM to 4 because I was tired of typing 16 numbers over and over to test it. 16 is supposed to be a %d that reads in DIM. I changed it back though.
    No, your code wouldn't have worked; it isn't redundant. You are trying to access outside of your array again. Think about how the for statement would actually terminate. Here is a simple example to demonstrate the problem, can you guess what value ndx is?
    Code:
    #include <stdio.h>
    
    int main(void){
    
    	const int DIM = 10;
    	int ndx = 0;
    
    	for(ndx=0;ndx < DIM;ndx++);
    
    	printf("%d",ndx);
    	getchar();
    	return(0);
    }
    Remember, in C arrays begin with zero, so if you have val[10] you can only access val[0] through val[9].
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Hard way...
    Code:
       for(ndx=0; (vals[ndx] == vals2[ndx]) && ndx < DIM; ndx++);
    
       if (vals[ndx] == vals2[ndx])
    	   printf("Equal\n");
       else
    	   printf("Not equal\n");
    Easy way...
    Code:
       for(ndx=0; ndx < DIM; ndx++)
         if (vals[ndx] != vals2[ndx])
           { printf("Not Equal\n");
             return 0; }
    
        printf("Equal\n");
    
        return 0; }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with for loop with arrays
    By jorgejags in forum C Programming
    Replies: 4
    Last Post: 11-03-2008, 06:42 PM
  2. Arrays and For Loop
    By Jack1982 in forum C++ Programming
    Replies: 9
    Last Post: 10-10-2007, 04:57 AM
  3. Loop printing arrays
    By CHurst in forum C Programming
    Replies: 2
    Last Post: 12-14-2005, 08:13 PM
  4. pointers, arrays, for loop, class
    By Lucid003 in forum C++ Programming
    Replies: 7
    Last Post: 10-18-2005, 04:36 PM
  5. pointers in arrays... Declaring objects in a loop.
    By Guest852 in forum C++ Programming
    Replies: 10
    Last Post: 04-05-2005, 06:57 AM