Finding a Number Within Array?

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

Finding a Number Within Array?

Postposted on Wed Apr 02, 2003 8:49 pm

I have problem concerning an array. To sum it up quickly the user will input a number then I need to find out if that number is a number within the array. Problem is I never came across anything like this before so I don’t know the syntax. Can anyone help?

A quick sample script is below:
int array[15] ={78, 34, 56, 1, 23, 8, 75, 12, 45, 33, 85, 90, 29, 32, 56};
malebolgia
Gerbil Elite
 
Posts: 973
Joined: Fri Apr 05, 2002 7:00 pm
Location: New Mexico, USA

Postposted on Wed Apr 02, 2003 8:58 pm

I'm not quite sure if this is what you are talking about, and C(++) isn't quite my strong point (yet :-), but here goes:
Code: Select all
int array[15]  ={78, 34, 56, 1, 23, 8, 75, 12, 45, 33, 85, 90, 29, 32, 56};
int number;
cin >> number;
for(int x=0;x < 15;x++)
 if (array[x] == number) break;
if (x < 15)
 cout << "Number found in position " << x << ".\n";
else
 cout << "Number not found.\n";

of course the forum engine probably raped my spacing... :roll:
SuperSpy
Gerbil Jedi
Gold subscriber
 
 
Posts: 1583
Joined: Thu Sep 12, 2002 9:34 pm
Location: TR Forums

Postposted on Wed Apr 02, 2003 8:58 pm

you can use some kind of for loop to go through the array one by one and if a match is found the shoot out some output.

something like this...not exact syntax by any means since I dont even know which language... c?

for (x=1 x>=15 x+)
if x = inputnumber then
whatever output
Your bargaining posture is highly dubious.
Coldfirex
Graphmaster Gerbil
 
Posts: 1109
Joined: Wed Dec 26, 2001 7:00 pm
Location: College Station, TX

Postposted on Wed Apr 02, 2003 8:59 pm

SuperSpy00bob beat me to it, and did a much better job at what I was trying to say :D
Your bargaining posture is highly dubious.
Coldfirex
Graphmaster Gerbil
 
Posts: 1109
Joined: Wed Dec 26, 2001 7:00 pm
Location: College Station, TX

Postposted on Wed Apr 02, 2003 9:59 pm

Well this gives a little bit of a better start, but I'm still kind of stuck. And SuperSpy00bob thanks for the code, but I need to do this in C, and since I don't know C++ I can't really do anything with it. Below is the code I have done so far. This part of the program as you can see basically searches and then prints out whether or not it's within the array. I'm pretty sure I'm suppose to put the last two printf statements in a loop, but then again I'm not really sure.

#include<stdio.h>

main()
{
int array[15] = {78, 23, 67, 31, 79, 3, 25, 9, 65, 3, 18, 80, 16, 2, 90};
int N, x;


printf("Enter N: ");
scanf("%d", &N);

for (x=0 x>=15 x++)
x=N;

printf("%d does appear in the array", N);

printf("%d does NOT appear in the array", N);
} //end main
malebolgia
Gerbil Elite
 
Posts: 973
Joined: Fri Apr 05, 2002 7:00 pm
Location: New Mexico, USA

Postposted on Wed Apr 02, 2003 10:08 pm

you dont have any if statements in there to check for anything. SuperSpy00bob is using C, just substitute the cout and cin for the printf and scanf...both are in C.
Your bargaining posture is highly dubious.
Coldfirex
Graphmaster Gerbil
 
Posts: 1109
Joined: Wed Dec 26, 2001 7:00 pm
Location: College Station, TX

Postposted on Wed Apr 02, 2003 10:36 pm

Do you mean like this?

#include<stdio.h>

main()
{
int array[15] ={78, 34, 56, 1, 23, 8, 75, 12, 45, 33, 85, 90, 29, 32, 56};

int number,x;

printf("Enter number: ");
scanf("&d",number);

for(x=0;x < 15;x++)
if (array[x] == number) break;
else if (x < 15)
printf("Number found in position");
else
printf("Number not found \n");
}
malebolgia
Gerbil Elite
 
Posts: 973
Joined: Fri Apr 05, 2002 7:00 pm
Location: New Mexico, USA

Postposted on Wed Apr 02, 2003 10:36 pm

Actually try this:


#include<stdio.h>

#define NUM_PTS 15

const int array[NUM_PTS] = {78, 23, 67, 31, 79, 3, 25, 9, 65, 3, 18, 80, 16, 2, 90};

main()
{
int N, x;

printf("Enter N: ");
scanf("%d", &N);

for (x=0; x>=NUM_PTS; x++) {
if (N == array[x]) {
printf("%d is in array position %d", N, x);
break;
}
}

if (x == NUM_PTS)
printf("%d does NOT appear in the array", N);

} //end main


Why do I move the array outside of the main routine? That is to prevent too much use of stack space, but it could have been declared within main too, it's scope would only be within main however. Why do I const it? That way the array is populated and created at compile time rather than empty memory being created on the stack, and then populated with values from an invisible const source the compiler creates at compile time. This means you would have both a variable array and a const array using up memory, and there has to be invisible code to copy these constants to this new memory location on the stack. My method will use only one const source, wont have to initialize data, and run faster.

Also why do I use #define NUM_PTS 15? Since 15 was used more than once and refers to an array size, it should be written this way NO MATTER WHAT! Get used to this practice. If not you will wind up increasing the size of your array and forgetting to change at least one corresponding value in code.

If you removed break from my code, it would be able to report all instances of a match and it's position, but then the "NOT appear" would always report as TRUE. You would have to make some small additional changes to add that support, like a flag.

Probably more than you wanted, and normally I don't do student's homework for them, but people already gave it away.

-LS
liquidsquid
Minister of Gerbil Affairs
 
Posts: 2447
Joined: Wed May 29, 2002 10:49 am
Location: New York

Postposted on Wed Apr 02, 2003 11:13 pm

lol malebolgia are you going to get us to do your homework all the time or just this once :lol:

1 thing i noticed: shouldn't your array be 14 and not 15?
sativa
Grand Gerbil Poohbah
 
Posts: 3045
Joined: Sun Apr 14, 2002 7:22 pm
Location: lafayette, la

Postposted on Thu Apr 03, 2003 3:52 am

Thanks for all the help everyone. To answer sativa this is homework, but only a small fraction of the complete program I have to do.
malebolgia
Gerbil Elite
 
Posts: 973
Joined: Fri Apr 05, 2002 7:00 pm
Location: New Mexico, USA

Postposted on Thu Apr 03, 2003 6:07 am

You could use pointers, as they usualy work faster in array notation (don't know what happens with optimiztional compilers, they could do the stuff for you).
a[10]=>a is address of begining of array, *a-value of 1st element, *(a+1) - value of second element.
Code: Select all
for(;;)
if ((*(a++)==number)|(x++==14))
  cout<<"In array";

or something like that.
Madman
Minister of Gerbil Affairs
 
Posts: 2317
Joined: Tue Apr 01, 2003 4:55 am
Location: Latvia

Ugh.

Postposted on Thu Apr 03, 2003 7:03 am

Ahh, pointer arithmetic, reason if one were needed why C shouldn't be taught to new Computer Science students....
Pauly
Gerbil
 
Posts: 40
Joined: Thu Jan 17, 2002 7:00 pm
Location: Manchester - UK

Postposted on Thu Apr 03, 2003 6:34 pm

yeah madman, if he doesn't know how to parse arrays, let alone search them, i doubt he is doing pointers yet.
sativa
Grand Gerbil Poohbah
 
Posts: 3045
Joined: Sun Apr 14, 2002 7:22 pm
Location: lafayette, la

Postposted on Thu Apr 03, 2003 6:56 pm

If pointer arithmetic was used, it might compile faster, but on most modern optimising compilers the run time would be the same.
Yahoolian
Grand Gerbil Poohbah
 
Posts: 3576
Joined: Sun Feb 16, 2003 3:43 pm
Location: MD

Postposted on Sat Apr 05, 2003 9:31 pm

Leave pointers out of this wil ya?

You will just confuse him even more.

Jus use the loop function that everyone has suggested.
Windows XP - The 64-bit wannabe with a 32-bit graphics interface for 16-bit extensions to a 8-bit patch on a 4-bit operating system designed to run on a 2-bit processor by a company that can't stand 1-bit of competition
fc34
Minister of Gerbil Affairs
 
Posts: 2816
Joined: Wed May 08, 2002 8:39 am
Location: Somewhere

Postposted on Sat Apr 05, 2003 10:27 pm

Am I losing it, or shouldn't it be "<=" in the for loop? I mean, it suppose to continue while the statement is true, right?

for(x = 0; x <= NUM_PTS; x++)


Typo? Or me too tired?

:)
Amiga 1200, 68020@28MHz, 4MB+2MB RAM, Conner 80MB harddrive
FubbHead
Grand Gerbil Poohbah
 
Posts: 3483
Joined: Mon Apr 08, 2002 9:04 pm
Location: Borås, Sweden


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 1 guest