Thread: compara array

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    2

    compara array

    given array A (1,2,3,4), array B(2,3,4,5)
    compare 2 array.find how many % match
    when i run only can get 100 % instead of 75%. any wrong of my code?
    output should be Only 75 percent are matched.Please try again. but still cannot get

    my code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
        int a[4] = {1, 2, 3, 4};
        int b[4] = {2, 3, 4, 5};
        int r, counter = 0, percentage = 0;
    
    
        for (r = 0; r < 4; r++)
        {
            if (a[r] == b[r]);
            {
            counter = counter + 1;
            }
            percentage = (((float)counter / 4) * 100);
        }
    
    
        {
            if (percentage == 100)
              {
               printf("all match");
              }
            else
              {
              printf("Only %d percent are matched.Please try again.", percentage);
              }
    
    
        }
        return 0;
    }
    Last edited by hohoho; 10-03-2012 at 02:36 AM. Reason: already changed still same

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    percentage is an int
    Code:
    percentage=(((float)counter/4)*100);
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You should get a 0% match since you compare the same indexes in the both arrays. Your if statement is followed by ';' so it's empty, meaning that what follows will always execute regardless if it's a match or not. Also, your for loop count from 0 to 3.

    Edit: don't mind the loop counter, I just noticed that you use a separate counter.
    Last edited by Subsonics; 10-03-2012 at 01:54 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters, you should:
    • Change void main to int main.
    • Indent your code properly.

    Thus:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int a[4] = {1, 2, 3, 4};
        int b[4] = {2, 3, 4, 5};
        int r, counter = 0, percentage = 0;
    
        for (r = 0; r < 4; r++)
        {
            if (a[r] == b[r])
                ;
            counter = counter + 1;
            percentage = (((float)counter / 4) * 100);
        }
    
        {
            if (percentage == 100)
                printf("all match");
            else
                printf("Only %d percent are matched.Please try again.", percentage);
        }
        return 0;
    }
    Notice that you have an if statement whose body does nothing. Then, you increment counter on each iteration, thus counter eventually ends up as 4. Notice also that you only needed to compute the percentage after the loop. Furthermore, you created a code block with those braces, but you did not need that code block anyway.

    One thing that can help you here is if you always use braces for if statements, e.g.,
    Code:
    if (a[r] == b[r])
    {
        counter = counter + 1;
    }
    and:
    Code:
    if (percentage == 100)
    {
        printf("all match");
    }
    else
    {
        printf("Only %d percent are matched.Please try again.", percentage);
    }
    EDIT:
    Quote Originally Posted by Subsonics
    Also, your for loop count from 0 to 3.
    That is not a problem. What might be a potential problem is that the magic number 4 is used, but that is okay for now.
    Last edited by laserlight; 10-03-2012 at 01:53 AM.
    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

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    2
    already solve thank you
    some{} problem ..
    Last edited by hohoho; 10-03-2012 at 02:54 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Err... you received three helpful replies. If you don't understand, then you should elaborate on what exactly is it that you don't understand.
    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

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by laserlight View Post
    That is not a problem. What might be a potential problem is that the magic number 4 is used, but that is okay for now.
    I somehow assumed that the loop counter was used in the division, but later discovered that was a separate variable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  2. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  3. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  4. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  5. Replies: 6
    Last Post: 11-09-2006, 03:28 AM