LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-02-2002, 09:59 PM   #1
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Rep: Reputation: 30
Is there a better way to do the following?


Hi. New to C and loving it. I'm mostly learning from a few books and there's alot of cool websites on Beginning C. I just began learning about arrays and was wondering if the following was a good way to check for matches between two arrays? Or is there some easier or better way to do it?

/* compare users input to int array; print results to standard out */

for(i=0; i<=4; i++)
{
while(a[j] != b[i] )
{
j++;
}
printf("b[%d] matches a[%] they both contain %d\n",i, j, b[i] );

/* set variable j back to 0 */
j = 0;

}

i have an array (a[10] containing 10 ints from 1-10 )
then the user enters 5 ints into b[5] and the above searches thru the array and prints out the matches

thanks for your time
New to C and Linux (RedHat 7.2)
 
Old 07-03-2002, 01:29 AM   #2
tundra
Member
 
Registered: Jun 2002
Location: Koom Valley
Distribution: rh8
Posts: 528

Rep: Reputation: 31
depends on what you mean by good

actually, as long as it gets the job done, don't worry about it too much. you'll find ways to improve whatever code you're doing later. especially if you have nothing else much to do . bask in your new found achievement and when you've had enough, move on to other topics on C!
 
Old 07-03-2002, 07:25 AM   #3
biosx
Member
 
Registered: Jul 2002
Location: Chicagoland
Distribution: Gentoo, Ubuntu
Posts: 63

Rep: Reputation: 15
I would rather do it this way, it makes more sense if you think about it:

Code:
for(i = 0; i < 5; i++) 
    for(j=0; j < 5; j++) 
        if( a[i] == b[j] )
            printf("a[%d] and b[%d] both contain %d\n", i, j, a[i]);
 
Old 07-03-2002, 10:50 AM   #4
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
thanks I didnt know you could put for loops inside each other like that. Cool!
 
Old 07-04-2002, 03:09 AM   #5
tundra
Member
 
Registered: Jun 2002
Location: Koom Valley
Distribution: rh8
Posts: 528

Rep: Reputation: 31
you could include error checking...
 
Old 07-07-2002, 05:29 PM   #6
kervin
Member
 
Registered: Jan 2002
Location: Melbourne, FL.
Distribution: redhat
Posts: 168

Rep: Reputation: 31
I know you're just starting off, but get a book on data structures and algorithms, if you plan to eventually use this code in something serious.

A "good" solution to your search problem is going to depend on the type of data you are search for ( integers?, strings?), the size of the array, are you looking for space ( memory ) or time ( CPU ) optimizations.

One integer approach is to make the array index the number. This would work if you have a bounded range or do not mind reallocating member. Therefore if the user enters '5' you do,
arrayOne[5]=1;
etc., That way your search loop becomes
for(int i=0;i<userInputSize;i++)
{
currentNumber = arrayTwo[i];
if(arrayOne[currentNumber]==1)
/* We have a match. Do stuff here */
}

The above can have *big* advantages if you make arrayOne a bitfield ( ie. an array of 'bits' instead of integers which would be 32 bits instead of 1 ).

But more importantly the algorithm is of order N instead of N^2 as the one you posted was. With your algorithm, the time it takes to execute will grow expontentially. The code will be twice as slower for every additional element in the array.

The second algorithm should grow linear.

So check out algorithms and data structures. Interesting stuff
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration