Thread: array ?

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    69

    array ?

    I mistakenly posted this code in wrong forum. Could anyone help me. I am stuck trying to evaluate the elements of two different arrays and determine whether or not they are equal.

    I get my program to compile but it is not reading the individual elements of my array. I'm stuck and need some help please.

    Thank You,

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void)
    {
    	//Local Declarations
    	int setA[10];
    	int setB[10];
    	int a = 1;
    	int b = 2;
    
    	//Statements
    	printf ("Please enter 10 integers for set A values:\n\n");
    		for ( int i = 0; i < 10; i++)
    			scanf ("%d", &setA[i]);
    		
    		printf ("\nPlease enter 10 intergers for set B values:\n\n");
    		for ( int i = 0; i < 10; i++)
    			scanf ("%d", &setB[i]);
    		
    
    
    		for (int i = 0; i < 10; i++)
    		if (!(setA[i] = setB[i]))
    		    printf ("False.\n\n");
    		else
    			printf ("True.\n\n");
    
    	system ("PAUSE");
        return 0;
    }//main
    any help is appreciated.

  2. #2
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    if (!(setA[i] == setB[i]))
    That's what you want equality and not assignment.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    69

    Thank You.

    It worked. One other question how might I get my if statement to stop after one false statement.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by silhoutte75 View Post
    It worked. One other question how might I get my if statement to stop after one false statement.
    There are several ways:
    You can put a break in the loop when you have determined the "not equal".
    You can use a variable that you set to "true" when you start ["assume equal until proven unequal"], then set it to "false" when you find it unequal, and add this variable to the loop conditin.

    You may also want to move your printf("TRUE") out of the loop, as it's got the same problem as your printf("false");

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    69

    Problem figuring this out.

    I have attempted several different steps to get just one true or false. None of my methods have successed. What am I doing wrong.

    Code:
    for (int i = 0; i < 10; i++)
            if (setA[i] == setB[i])
              {
    	printf ("True.\n\n");
              }
           else 
    	printf("False.\n\n");
    and I've tried:

    Code:
    if (setA[i] != setB[i}
     {
       printf ( "false.\n\n");
     }
    else
      printf ("True.|n\n");
    and

    Code:
    for(sum = 0, i = 0; i < 10; i++);
         if (setA[i] == setB[i]);
          {
             scanf ("%d", &i);
             sum += i;
             printf ("True.\n\n");
          }
        else
           printf ("False.\n\n");
    Please help me understand better.
    Thank you.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Just use this

    Code:
    for (int i = 0; i < 10; i++)
            if (setA[i] == setB[i])
              {
    	       printf ("True.\n\n");
                    break;
              }
           else 
    	printf("False.\n\n");
    ssharish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM