5 Replies - 969 Views - Last Post: 14 May 2009 - 03:00 AM Rate Topic: -----

#1 jokertin92  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • 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  Icon User is offline

  • D.I.C Head

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

Re: To Search a number in an Array

Posted 14 May 2009 - 01:47 AM

View Postjokertin92, 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.
Was This Post Helpful? 0
  • +
  • -

#3 amin_mubarak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 :D

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

Was This Post Helpful? 0
  • +
  • -

#4 jokertin92  Icon User is offline

  • New D.I.C Head

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

Re: To Search a number in an Array

Posted 14 May 2009 - 02:39 AM

View Postgalorin, on 14 May, 2009 - 12:47 AM, said:

View Postjokertin92, 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

Was This Post Helpful? 0
  • +
  • -

#5 ashishshevale  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 jokertin92  Icon User is offline

  • New D.I.C Head

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

Re: To Search a number in an Array

Posted 14 May 2009 - 03:00 AM

View Postashishshevale, 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();
	 }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1