Checking elements of an arrays

This is a discussion on Checking elements of an arrays within the C Programming forums, part of the General Programming Boards category; Hi, I have a system that now specifies from the user a value to determine the size of an array. ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    19

    Checking elements of an arrays

    Hi,

    I have a system that now specifies from the user a value to determine the size of an array. the user can then enter value into the array one by one, until they get to the limit specified, all that works fine.

    The array I have created is for input values for an AND gate. The AND gate will only produce a 1 if all the values in the array are set to 1. the way I want to get it is that I run a for loop through the AND array of values for the inputs and the first value that it encounters that is a 0, it will run the printf statement that reads 0.

    I can't seem to get it to do it, it always seems to evalate to 1, even thought i have placed a 0 in the array.

    Code:
    int and(int inputs)
    {
    	printf("\nPlease Set The Vlues For Your Inputs: ");
    	int array[inputs];
    
    	int i =0;
    	for (i = 0; i < inputs; i++){
    		printf("Enter input %d:", i);
    		scanf("%d",&array[i]);
    		}
    
    		for (i=0; i<inputs; i++) {
    			printf("\n%d", array[i]);
    		}
    
    		if (array[i] == 0) {
    			printf("output = 0");
    		}
    		else {
    			printf("output = 1");
    		}
    here is a snippet of where I am stuck, any ideas will be great, thanks for listening
    Attached Files Attached Files

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    result = !!input[0]; // Start off with the first input. 
    for(i = 1; i < inputs; i++)
    {
       result &= !!input[i];
    }
    The !! can be removed it input[x] is always guaranteed to be 0 or 1.

    This code basically walks through every operand, performing an and with the result of the previous operands.

    --
    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.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    Hi, thanks for the reply, thats great. I was just tweaking around with my code and included the if statement within the for loop before it.

    I wasn't quite clear before, sorry, but what I want is a way to check the whole array, to see if it contains a certain element.

    In my case, for the AND array I want a piece of code that will check the entire array of it's values and see what printf statement to respond with.

    As it is the, for loop checks every element and printf's the corresponding statement, which I guess is a start, but I don't want that, I want it to printf one statement. So if the user inputted these values [0,1,1,1,0] in the input array then the final output printf statement would read "Output =0"

    Same for if the user inputtede [1,1,1,1] the statement would read "Output =1"

    I hope that makes sense, and thanks for your response.

    Archie

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So ... don't print anything inside the loop? Wait until you're done, and then print? What exactly is the problem here?

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    Hi, even when I take it out the loop it still doesn't seem to do what I want it to.
    Code:
    if (array[i] == 0) {
    			printf("output = 0");
    		}
    		else {
    			printf("output = 1");
    		}
    Am I right in thinking that this bit of code checks the entire array. Even so when I input say [0,1,1,1] you would expect it to output a 0 but instead it ouputs a 1. I need to find a way of checking the whole array of inputs and then determine whether or not produce the printf statement that pronys out a 1 or 0.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, since that is OUTSIDE the loop, i is actually pointing one beyond inputs, which is probably not set to anything meaningfull.

    And if you use the loop I suggested, you only need a single if-statement, printing "Ouptut = %d", result.

    --
    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.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    Hi, wel this is what I have now;

    Code:
    #include <stdio.h>
    
    int and(int n)
    {
    	printf("\nPlease Set The Values For Your Inputs: ");
    	int array[n];
    
    	int i = 0;
    	for (i = 0; i < n; i++){
    		printf("Enter input %d:", i);
    		scanf("%d",&array[i]);
    
    	}
    
    		if (array[n] ==0) {
    			printf("\noutput = 0\n");
    		}
    		else {
    			printf("\noutput = 1\n");
    		}
    		return 1;
    }
    where abouts would i include your for loop? I spent so muh time looking at my code I'm just not seein things straight.

    Thanks again

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You would either make it part of the loop with scanf() in it, or immediately after that loop.

    --
    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.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    Code:
    int result = !!array[0]; // Start off with the first input.
    	for(i = 1; i < n; i++)
    	{
    	   result &= !!array[i];
    	}
    With what you suggested the AND function works fine thankyou, but I have never used the '&=!!array[i]' before, what does that actually do. I do not to use code that I cannot explain.

    Thanks again, I take it that for the NAND gate function all I will have to do is the invert of it

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    x&= y means the same as x = x &y;

    !!x means bool invert and invert again, which is a clever trick to translate any number into either 0 (if it was zero in the first place) or 1 (for all other values)。 

    --
    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.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    Brilliant, thats helped alot, but how can that be inverted to produce the logic for the NAND? Or is it a different process altogether.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:59 PM
  2. arrays with elements
    By bradleyd in forum C Programming
    Replies: 5
    Last Post: 04-10-2007, 01:00 PM
  3. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 07:57 PM
  4. summing an array's elements
    By gomindi in forum C++ Programming
    Replies: 10
    Last Post: 11-21-2003, 04:37 PM
  5. calculating elements of arrays
    By Kristin in forum C Programming
    Replies: 2
    Last Post: 11-02-2003, 10:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21