Thread: Array question.

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

    Array question.

    I'm stuck. I have written a program to obtain 10 intergers for 2 different array set. I am trying to test the elements of each array and I have gotten stuck. Could someone guide me in the right direction.

    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

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    (Looks more like C than C++)

    Your third for loop contains the assignment operator, and not the comparison operator. You're assigning the elements of setB to the elements of setA and then using the negation operator. This means that whenever an element in setB is 0, you'll print "False", otherwise you'll print "True" (after assigning it to the corresponding element in setA).

    You probably meant:
    Code:
    if (!(setA[i] == setB[i]))
    Or better still (because it's easier to understand):
    Code:
    if (setA[i] != setB[i])

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

    Thank You.

    One more question. How would I get my if statement to stop after one false statement?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by silhoutte75
    How would I get my if statement to stop after one false statement?
    Put in a break after the printf statement:
    Code:
    for (int i = 0; i < 10; i++)
        if (!(setA[i] == setB[i]))
        {
            printf ("False.\n\n");
            break;
        }
        else
            printf ("True.\n\n");
    ... or you could set i to something >= 10 which would stop the loop from executing any further.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

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

    Thank You.

    Your suggestion worked and it did what I wanted it to do. Is there a way to write this part of the program so that I get only one true or one false statement for the entire run of the program?

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    If you want to print "True" if the first element matches, put a break after the printf ("True.\n\n"); (within proper braces, of course).

    It seems more likely that you want to print "True" when all the values in both arrays match. For that, check if (i==10) after the end of the loop. If it is, you've traversed through the array without finding a different pair of values, so print "True".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM