Thread: Array function not returning correct valuse

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    41

    Array function not returning correct valuse

    Hi Guys, I have been trying to make a function that compares all the values of the array and if they are all equal will return a value to print true. The problem I am having is that regardless of what values I enter the function is always returning true. I couldn't really figure out a way to tell the program o check all the values in one command instead I put them each, If you guys see any thing wrong or any advice it would be greatly appreciated. Thanks again.
    Code:
     #include<stdio.h>
    
    #include<stdlib.h>
    
    
     
    
     
    
     
    
    int compare(int arrayA[], int arrayB[]);
    
    
    int main()
    
    {
    
    
    int i = 0;
    
    int k = 0;
    
    int arrayA [10];
    
    int arrayB [10];
    
    int T = 0;
    
    
    for(i = 0; i < 10; i++)
    
    {
    
        scanf("%d",&arrayA[i]);
    
    
    }
    
    
    printf("\n\n");
    
    
    for(k = 0; k < 10; k++)
    
    {
    
        scanf("%d",&arrayB[k]);
    
    }
    
    
        T = compare(arrayA, arrayB);
    
    
    if(T = 1)
    
        {
    
        
    
            printf("\n True");
    
        }
    
    
    else
    
        {
    
        
    
            printf("\n False");
    
        }
    
    
     
    
     
    
     
    
     
    
    }
    
    int compare(intarrayA[], intarrayB[])
    
    {
    
    
    int T = 0;
    
        
    
    
    if(arrayA[0] == arrayB[0] && arrayA[1] == arrayB[1] && arrayA[2] == arrayB[2] && arrayA[3] == arrayB[3] && arrayA[4] == arrayB[4] && arrayA[5] == arrayB[5] && arrayA[6] == arrayB[6] && arrayA[7] == arrayB[7] && arrayA[8] == arrayB[8] && arrayA[9] == arrayB[9])
    
    {
    
        
    
        T = 1;
    
        
    
    
    return(T);
    
    }
    
    
    else
    
    {
    
        T = 0;
    
    return(T);
    
    }
    
    
     
    
    }  


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters, you need to indent your code properly. Your problem is likely due to this line:
    Code:
    if(T = 1)
    Recall the difference between = and ==.

    By the way, instead of naming your function "compare", a name like "is_equal", "all_equal", "compare_equal" or even just "equal" would likely be more descriptive.
    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

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    41
    Wow, lol thank you so much after staring at the code you just don't see simple mistakes especially since I'm new to this lol. I want to really thank you for your help and I will try to indent and give better descriptive names. Thanks again for your help it is greatly appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 09-25-2012, 01:31 AM
  2. function not returning correct values
    By Paul Adams in forum C Programming
    Replies: 2
    Last Post: 02-24-2012, 09:58 PM
  3. Not returning the correct string
    By jk1998 in forum C++ Programming
    Replies: 3
    Last Post: 07-27-2007, 03:25 PM
  4. correct way of returning strings
    By marsface in forum C++ Programming
    Replies: 5
    Last Post: 06-11-2003, 10:33 AM
  5. Time - not returning correct day
    By MethodMan in forum C Programming
    Replies: 8
    Last Post: 03-26-2003, 01:21 AM