Outputting numbers in arrays

This is a discussion on Outputting numbers in arrays within the C++ Programming forums, part of the General Programming Boards category; Hi, I need to write a program that has 2 arrays both with 6 numbers each and compares the two ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    30

    Outputting numbers in arrays

    Hi,
    I need to write a program that has 2 arrays both with 6 numbers each and compares the two arrays to see if they have any matching numbers.
    I have figured out how to output the amount of matching numbers but not the numbers themselves. Here is my function trying to do that...

    Code:
    int numbers (int draw[], int entry[])
    {
        int samenumbers = 0;
        for(int i=0; i<MAXSIZE; i++)
        {
            for(int j=0; j<MAXSIZE; j++)
            {
                if(draw[i]==entry[j])
                samenumbers = draw[i];
                break;
            }
            
        }
        return samenumbers;
    }
    draw and entry are the names of the 2 arrays.
    Any ideas of what is wrong?
    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Once you have found a match, print the number out, or store it in another array.

    If you will have larger arrays, you will find that sorting the arrays and then checking for matches in a single pass would be faster.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    Thanks but I'm not really sure how to use print.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by rachael033 View Post
    Thanks but I'm not really sure how to use print.
    ....

    Use std::cout....

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    Do i use the print inside or outside my function?

    And I have done my own homework this is just a bit of it I couldn't figure out which is the whole point of discussion boards.....

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Do i use the print inside or outside my function?
    If you have another array to store the matched numbers, then you can loop over that array and print from outside your function. If not, it is simpler to just print in your function, as soon as you determine a match.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    So would it be something like this?

    Code:
    int numbers (int draw[], int entry[])
    {
         for(int i=0; i<MAXSIZE; i++)
        {
            for(int j=0; j<MAXSIZE; j++)
            {
                if(draw[i]==entry[j])
                
                break;
                
            }
            printf; draw[i];
        }
        
    }

    Sorry if that is totally wrong we just havn't learnt about how to use print!

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by rachael033 View Post
    So would it be something like this?

    Code:
    int numbers (int draw[], int entry[])
    {
         for(int i=0; i<MAXSIZE; i++)
        {
            for(int j=0; j<MAXSIZE; j++)
            {
                if(draw[i]==entry[j])
                
                break;
                
            }
            printf; draw[i];
        }
        
    }

    Sorry if that is totally wrong we just havn't learnt about how to use print!
    Forget printf(). Use std::cout, as I said. If you see that draw[i] and entry[i] are equal, then you can print them right then and there like this:

    Code:
    std::cout << "Matching element = " << draw[i] << std::endl;

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    Ok thanks it seems to be kind of working but I am getting the right numbers printed out but then a big random number at the end.
    I input 1 2 3 4 5 6 in both arrays so that should be the output but instead of...
    1 2 3 4 5 6
    i get
    1 2 3 4 5 6 2293468

    Here is the function...

    Code:
    int numbers (int draw[], int entry[])
    {
        
        for(int i=0; i<MAXSIZE; i++)
        {
            for(int j=0; j<MAXSIZE; j++)
            {
                if(draw[i]==entry[j])
                cout << draw[i] << " ";
            }
    
        }
        
    }
    I output it like this..

    Code:
    cout << numbers(draw, entry) << endl;
    Any reason why the extra number is showing up?
    Thanks

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    When you call this:

    Code:
    cout << numbers(draw, entry) << endl;
    You end up trying to call numbers() and then print it's return number.

    That would be fine if you're returning a number that makes sense to your program, but if you look at your definition of the function, you declare it to return an int, but you're returning nothing, which is wrong.

    I wrote multiple posts related to this subject, in detail for the x86 processor, but the important thing to understand is that your function will return a value no matter if you forget to return one or not (and assuming it compiles and runs). It just so happens that 2293468 was the number returned.

    I don't think you should be printing the return value of numbers() anyway, but whatever you decide to do with it, you should explicitly return a value, or declare it to return void.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    Ok I used void instead and it works, finally!
    Thankyou so much for your help.
    Sorry i seemed so clueless..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 06:28 AM
  2. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 04:03 PM
  3. Replies: 4
    Last Post: 04-19-2005, 08:05 PM
  4. Doing multiplication using numbers in arrays
    By neandrake in forum C++ Programming
    Replies: 17
    Last Post: 11-05-2004, 10:07 AM
  5. using arrays to sort numbers
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:14 PM

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