Friday, October 30, 2009

Chap 8[D]a Search quantity of instances in Array

Twenty-five numbers are entered from the keyboard into an array. The number to be searched is entered through the keyboard by the user. Write a program to find if the number to be searched is present in the array and if it is present, display the number of times it appears in the array.
void main()
{
int arr[25];
int a,d,i;

for(i=0;i<25;i++)
{
printf("\nKey %d) value",i);
scanf("%d",&arr[i]);
}

printf("\n25 numbers stored, enter any integer again");
scanf("%d",&d);

for(i=0,a=0;i<25;i++)
{
if(arr[i]==d)
a++;
}

if(a>0)
printf("\nThe integer appeared %d times in the array",a);

else
printf("\nThe integer did not appear in the array");
}

5 comments:

  1. Error
    this program fails

    ReplyDelete
    Replies
    1. #include
      int main(int argc, char ** argv)
      {

      int i,x,a;
      int num[5]; //Array Declaration
      int numtwo[5];

      // Adding values into an array
      for(i=0;i<=4; i++) {
      printf("\n Enter Numbers \n");
      scanf("%d",&num[i]);
      numtwo[i] = num[i];//Store data in an array
      }


      // Displaying values of array stored in another array
      for(i=0;i<=4;i++) {
      printf("The numbers are : %d \n", numtwo[i]);//read data from an array
      }

      // Asking the user to input a number for a search
      printf("\nPlease enter a number to search in an array : \n" );
      scanf("%d\n",&x);
      printf("The number you have entered is: %d",x);


      //Searching that number inside an that array and displaying how many times the number is found
      for(i=0,a=0;i<4;i++)
      {
      if(numtwo[i]==x)
      a++;
      }

      if(a>0) {
      printf("\nThe integer appeared %d times in the array",a);
      }

      else {
      printf("\nThe integer did not appear in the array");
      }


      return 0;
      }

      Delete
  2. http://letuscalllessons.blogspot.in/ visit my blog.

    ReplyDelete