![]() |
| | #1 |
| Registered User Join Date: Sep 2009 Location: Dallas, Texas
Posts: 70
| finding common numbers 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'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 | |
| | #2 |
| Registered User Join Date: Oct 2009 Location: While(1)
Posts: 368
| 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
|
| RockyMarrone is offline | |
| | #3 |
| Registered User Join Date: Sep 2009 Location: Dallas, Texas
Posts: 70
| 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;
}
A \ B = {5, 5} The numbers are correct. What's wrong. |
| BB89 is offline | |
| | #4 |
| Registered User Join Date: Mar 2009
Posts: 38
| When A[x] == B[z] then Code: printf("A \ B = {%d, %d\n}", A[x], B[z] );
EDIT : You are declaring x and z twice. Last edited by zalezog; 11-09-2009 at 01:07 PM. |
| zalezog is offline | |
| | #5 |
| Registered User Join Date: Sep 2009 Location: Dallas, Texas
Posts: 70
| Ahh, makes since. So Code:
printf("A \ B = {%d}", A[x] );
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 | |
| | #6 | |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,733
| Quote:
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 | |
| | #7 | |
| Registered User Join Date: Oct 2009 Location: While(1)
Posts: 368
| Quote:
Ok iMac Please tell me if the numbers are unsorted then the algo | |
| RockyMarrone is offline | |
| | #8 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,338
| Quote:
__________________ 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 | |
| | #9 |
| Registered User Join Date: Oct 2009 Location: While(1)
Posts: 368
| |
| RockyMarrone is offline | |
| | #10 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,338
| Quote:
__________________ 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 | |
| | #11 | ||
| Registered User Join Date: Oct 2009 Location: While(1)
Posts: 368
| Quote:
Quote:
| ||
| RockyMarrone is offline | |
| | #12 | |
| Registered User Join Date: Feb 2009
Posts: 35
| Quote:
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 | |
| | #13 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,338
| Quote:
__________________ 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 | |
| | #14 |
| Registered User Join Date: Sep 2009 Location: Dallas, Texas
Posts: 70
| I figured it out. Thanks for the help. |
| BB89 is offline | |
| | #15 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,338
| Quote:
__________________ 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |