# To Search a number in an Array

Page 1 of 1

## 5 Replies - 1368 Views - Last Post: 14 May 2009 - 03:00 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=105081&amp;s=20d6b7efdf3429baa98c76410b03db76&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jokertin92

Reputation: -2
• Posts: 39
• Joined: 26-March 09

# To Search a number in an Array

Posted 14 May 2009 - 12:21 AM

[indent]
```HI all, this is a prgram to search a number in an array.
Please point me out errors in this Program and help
me how to declare n number of arrays instead of a[40].

/* TO SEARCH A NUMBER IN AN ARRAY*/
#include<stdio.h>
main()
{
int a[40],m,n,j=0;
int i;
clrscr();
printf("Enter the Array size : ");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("Enter the array element a[%d] :",i);
scanf("%d",&a[i]);
}
printf("\nEnter the number to be Searched in the Array : ");
scanf("%d",&n);
for(i=0;i<m;i++)
if(a[i]==n)
j=j+1;
printf("\nThe number %d is repeated %d times...",n,j);
getch();
}

[b]Output:[/b][u]

Enter the Array size : 5
Enter the array element a[0] :2
Enter the array element a[1] :4
Enter the array element a[2] :5
Enter the array element a[3] :2
Enter the array element a[4] :1

Enter the number to be Searched in the Array : 2

The number 2 is repeated 2 times...

```

This post has been edited by jokertin92: 14 May 2009 - 02:38 AM

Is This A Good Question/Topic? 0

## Replies To: To Search a number in an Array

### #2 galorin

Reputation: 4
• Posts: 86
• Joined: 17-February 09

## Re: To Search a number in an Array

Posted 14 May 2009 - 01:47 AM

jokertin92, on 13 May, 2009 - 11:21 PM, said:

HI all, this is a prgram to search a number in an array. Please point me out errors in this Program and help me how to declare n number of arrays instead of a[40].

Does it compile? Does it return warnings or errors? Can you please use the code markup when posting code?

You can create the array of size n-1 after the user inputs how many numbers there are. Create another function that takes in the number of elements in, and either does your processing, or returns a pointer to an array. I also noticed that main() doesn't return anything, or take in any arguments. It should be
```int main ( int argc, char *argv[] )
```

so that it has an exit status, and knows how many arguments were passed to it on the command line (int argc) and what those commands are (char * argv[]).

Great thing about C is there are a million was to accomplish the same thing, and all of them are standard. I came from VBA (yick) where there are a million ways to do something, and not a single one is standard.

### #3 amin_mubarak

Reputation: 0
• Posts: 3
• Joined: 31-March 08

## Re: To Search a number in an Array

Posted 14 May 2009 - 01:57 AM

hello >>>
you must include conio.h for getch() function
to make dynamic matrix in runtime you must use pointer and malloc function
see the code
```#include<stdio.h>
#include <conio.h>
#include <malloc.h>
int main()
{
int* a,m,n,j=0;
int i;
printf("Enter the Array size : ");
scanf("%d",&m);
a=(int*) malloc(m* sizeof(int));
for(i=0;i<m;i++)
{
printf("Enter the array element a[%d] :",i);
scanf("%d",&a[i]);
}
printf("\nEnter the number to be Searched in the Array : ");
scanf("%d",&n);
for(i=0;i<m;i++)
if(a[i]==n)
j=j+1;
printf("\nThe number %d is repeated %d times...\n",n,j);
free(a);
getch();
return 0;
}
```

bye

This post has been edited by amin_mubarak: 14 May 2009 - 01:58 AM

### #4 jokertin92

Reputation: -2
• Posts: 39
• Joined: 26-March 09

## Re: To Search a number in an Array

Posted 14 May 2009 - 02:39 AM

galorin, on 14 May, 2009 - 12:47 AM, said:

jokertin92, on 13 May, 2009 - 11:21 PM, said:

HI all, this is a prgram to search a number in an array. Please point me out errors in this Program and help me how to declare n number of arrays instead of a[40].

Does it compile? Does it return warnings or errors? Can you please use the code markup when posting code?

You can create the array of size n-1 after the user inputs how many numbers there are. Create another function that takes in the number of elements in, and either does your processing, or returns a pointer to an array. I also noticed that main() doesn't return anything, or take in any arguments. It should be
```int main ( int argc, char *argv[] )
```

so that it has an exit status, and knows how many arguments were passed to it on the command line (int argc) and what those commands are (char * argv[]).

Great thing about C is there are a million was to accomplish the same thing, and all of them are standard. I came from VBA (yick) where there are a million ways to do something, and not a single one is standard.

Yes boss! see the output, I am getting what we expect.

This post has been edited by jokertin92: 14 May 2009 - 02:42 AM

### #5 ashishshevale

Reputation: 3
• Posts: 92
• Joined: 14-April 09

## Re: To Search a number in an Array

Posted 14 May 2009 - 02:41 AM

hey code should be like this
``` #include<stdio.h>
main()
{
int a[40],m,n,j=0;
int i;
clrscr();
printf("Enter the Array size : ");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("Enter the array element a[%d] :",i);
scanf("%d",&a[i]);
}
printf("\nEnter the number to be Searched in the Array : ");
scanf("%d",&n);
for(i=0;i<m;i++)
{
if(a[i]==n)
{
j=j+1;
}
}
printf("\nThe number %d is repeated %d times...",n,j);
getch();
}
```

This post has been edited by ashishshevale: 14 May 2009 - 02:42 AM

### #6 jokertin92

Reputation: -2
• Posts: 39
• Joined: 26-March 09

## Re: To Search a number in an Array

Posted 14 May 2009 - 03:00 AM

ashishshevale, on 14 May, 2009 - 01:41 AM, said:

hey code should be like this
``` #include<stdio.h>
main()
{
int a[40],m,n,j=0;
int i;
clrscr();
printf("Enter the Array size : ");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("Enter the array element a[%d] :",i);
scanf("%d",&a[i]);
}
printf("\nEnter the number to be Searched in the Array : ");
scanf("%d",&n);
for(i=0;i<m;i++)
{
if(a[i]==n)
{
j=j+1;
}
}
printf("\nThe number %d is repeated %d times...",n,j);
getch();
}
```

Hey! i got someother way, it executes well...

```		 /* TO SEARCH A NUMBER IN AN ARRAY*/
#include<stdio.h>
main()
{
int *a,m,n,j=0;
int i;
a=&a[0];
clrscr();
printf("Enter the Array size : ");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("Enter the array element a[%d] :",i);
scanf("%d",&a[i]);
}
printf("\nEnter the number to be Searched in the Array : ");
scanf("%d",&n);
for(i=0;i<m;i++)
if(a[i]==n)
j=j+1;
printf("\nThe number %d is repeated %d times...",n,j);
getch();
}

```