C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-09-2009, 12:08 AM   #1
Registered User
 
Join Date: Sep 2009
Location: Texas
Posts: 60
finding common numbers

I am trying to find the numbers in the two arrays that are in common.

Say they are:

int A[3] = {1, 2, 5};
int B[4] = {1, 5, 9, 10};

Answer should say A \ B = {1, 5}


Code:
#include <stdio.h>

int main (void)
{
    int A[3] = {1, 2, 5};
    int B[4] = {1, 5, 9, 10};

    int x, z;

   for( int x = 0; x < 3; x++ )
    for( int z = 0; z < 4; z++ )

}
I have declared the arrays and I am think the rest is correct.

I'm not sure how to find the common numbers or how to associate the loops with the arrays.

New to C and Arrays.
BB89 is offline   Reply With Quote
Old 11-09-2009, 12:28 AM   #2
Registered User
 
Join Date: Oct 2009
Location: While(1)
Posts: 316
What you have to do

Two arrays lets say A and B

Code:
    for i = 0  to n - 1 of A
        for j = 0 to n -1 of B
           if value of A[i] == B[j] then add to answer array
before adding to the answer array just check its already there or not
RockyMarrone is offline   Reply With Quote
Old 11-09-2009, 12:55 PM   #3
Registered User
 
Join Date: Sep 2009
Location: Texas
Posts: 60
Code:
#include <stdio.h>

int main (void)
{
    int A[3] = {1, 2, 5};
    int B[4] = {1, 5, 9, 10};

    int x, z;

   for( int x = 0; x < 3; x++ )
    for( int z = 0; z < 4; z++ )
        if( A[x] == B[z] )
            printf("A \ B = {%d, %d\n}", A[x], B[z] );

    return 0;
}
My output: A \ B = {1,1}
A \ B = {5, 5}

The numbers are correct.

What's wrong.
BB89 is offline   Reply With Quote
Old 11-09-2009, 01:01 PM   #4
Registered User
 
Join Date: Mar 2009
Posts: 22
When
A[x] == B[z]
then
Code:
printf("A \ B = {%d, %d\n}", A[x], B[z] );
A[x] and B[z] are same you need to print any one.

EDIT : You are declaring x and z twice.

Last edited by zalezog; 11-09-2009 at 01:07 PM.
zalezog is offline   Reply With Quote
Old 11-09-2009, 01:34 PM   #5
Registered User
 
Join Date: Sep 2009
Location: Texas
Posts: 60
Ahh, makes since.

So

Code:
 printf("A \ B = {%d}", A[x] );
Why is it printing it twice?

It doing this, I dont understand it.

A \ B = {1,1}
A \ B = {5, 5}

Im wanting jusr A \ B = {1, 5}
BB89 is offline   Reply With Quote
Old 11-10-2009, 02:41 AM   #6
Algorithm Dissector
 
iMalc's Avatar
 
Join Date: Dec 2005
Location: New Zealand
Posts: 2,475
Quote:
Originally Posted by RockyMarrone View Post
What you have to do

Two arrays lets say A and B

Code:
    for i = 0  to n - 1 of A
        for j = 0 to n -1 of B
           if value of A[i] == B[j] then add to answer array
before adding to the answer array just check its already there or not
If A and B each held 10000 numbers or more, would you still use the same algorithm?

The arrays are sorted. If it's a safe assumption that they are always sorted then there's a much quicker approach. Try studying the C++ standard library code for set_intersection.

Can we also assume there are no duplicates?
__________________
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
iMalc is offline   Reply With Quote
Old 11-10-2009, 03:02 AM   #7
Registered User
 
Join Date: Oct 2009
Location: While(1)
Posts: 316
Quote:
Originally Posted by iMalc View Post
If A and B each held 10000 numbers or more, would you still use the same algorithm?

The arrays are sorted. If it's a safe assumption that they are always sorted then there's a much quicker approach. Try studying the C++ standard library code for set_intersection.

Can we also assume there are no duplicates?

Ok iMac Please tell me if the numbers are unsorted then the algo
RockyMarrone is offline   Reply With Quote
Old 11-10-2009, 03:05 AM   #8
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
Quote:
Originally Posted by RockyMarrone
Ok iMac Please tell me if the numbers are unsorted then the algo
... would be to sort them and then use an algorithm that works when they are sorted.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 11-10-2009, 03:09 AM   #9
Registered User
 
Join Date: Oct 2009
Location: While(1)
Posts: 316
Quote:
Originally Posted by laserlight View Post
... would be to sort them and then use an algorithm that works when they are sorted.
So how many compressions will be there less the one above of yours or more ????
And which will be faster
RockyMarrone is offline   Reply With Quote
Old 11-10-2009, 04:10 AM   #10
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
Quote:
Originally Posted by RockyMarrone
So how many compressions will be there less the one above of yours or more ????
And which will be faster
I think you mean "comparisons" instead of "compressions". Unfortunately, I do not really understand your sentence. What algorithms are you trying to compare?
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 11-10-2009, 04:20 AM   #11
Registered User
 
Join Date: Oct 2009
Location: While(1)
Posts: 316
Quote:
Originally Posted by laserlight View Post
I think you mean "comparisons" instead of "compressions". Unfortunately, I do not really understand your sentence. What algorithms are you trying to compare?
Then without knowing the algo how can u write

Quote:

Quote:
Originally Posted by RockyMarrone
Ok iMac Please tell me if the numbers are unsorted then the algo
... would be to sort them and then use an algorithm that works when they are sorted.
RockyMarrone is offline   Reply With Quote
Old 11-10-2009, 05:23 AM   #12
Registered User
 
Join Date: Feb 2009
Posts: 35
Quote:
Originally Posted by BB89 View Post
Ahh, makes since.

So

Code:
 printf("A \ B = {%d}", A[x] );
Why is it printing it twice?

It doing this, I dont understand it.

A \ B = {1,1}
A \ B = {5, 5}

Im wanting jusr A \ B = {1, 5}
you have your printf("a\b...}"); in the middle of a for loop. so everytime you find a matching pair you are printing the entire line. you need to take it out of the for loop and print it only once. you can either stick the statement before the for loops, or after the for loops.

also i cant see an easy way to print matching values inside nested for loops (due to the required format of the output, particularly the comma). you'll need to store the matching values somewhere and print them later, its about 5 extra lines.

(you should also fix up your indenting. either 3 spaces or 4 spaces. pick one and use it all the time)

Last edited by Brain_Child; 11-10-2009 at 05:32 AM.
Brain_Child is offline   Reply With Quote
Old 11-10-2009, 08:07 AM   #13
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
Quote:
Originally Posted by RockyMarrone
Then without knowing the algo how can u write
Ah, but I do know the algorithm that iMalc was talking about
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 11-10-2009, 08:21 AM   #14
Registered User
 
Join Date: Sep 2009
Location: Texas
Posts: 60
I figured it out.

Thanks for the help.
BB89 is offline   Reply With Quote
Old 11-10-2009, 08:22 AM   #15
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
Quote:
Originally Posted by BB89
I figured it out.
Great. What did you use in the end?
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to handle multiple cases which use common code? tmaxx C Programming 3 10-03-2008 07:42 AM
finding most common char in a string scwizzo C++ Programming 6 11-23-2007 01:22 PM
Logical errors with seach function Taka C Programming 4 09-18-2006 05:20 AM
Adding Line numbers in Word Mister C A Brief History of Cprogramming.com 24 06-24-2004 08:45 PM
A (complex) question on numbers Unregistered C++ Programming 8 02-03-2002 06:38 PM


All times are GMT -6. The time now is 07:48 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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