-->

WAP to search a number in a list of numbers using array as a parameter.

in turboc++
-----------------------------------------------------------------------------------------
/*WAP to input elements and search a number in that.
Pass Weight as an array as parameter.*/
#include<stdio.h>
#include<conio.h>
int search_in_array(float array[200],int size,int number);//prototype with array and size and number parameters
int main()
{

float arr[200];int k,num;            // array declaration with maximum limit 200
int size,f;               // says about size of array to be entered
printf("enter size of array\n");
scanf("%d",&size);//input of size
printf("enter elements\n");
for(k=0;k<size;k++)//loop execution
{
   scanf("%f",&arr[k]);//data inputs
}
printf("enter number to be searched\n");
scanf("%d",&num);                //number input to be searched
f=search_in_array(arr,size,num);//assigning return value to variable f
if(f==0)                        //testing the value
{
printf("number not found \n");//calling of function
}
else
{
    printf("number found and lcoation=%d",f);
}
getch();
return 0;
}
int search_in_array(float array[200],int size,int number)//body line
{

int k;//declaration
int found;

for(k=0;k<size;k++)//loop execution

{
 if(number==array[k])
        {
            found=0; //assigning 0 on its finding
            break;
        }
        else
        {
            found=1;//else assigning 1
        }
}
if(found==0)//testing the condition
{
    return k;//returning the location
}
    else
{
        return 0;//returning nothing
}
    getch();
}






--------------------------------------------------------------------------------------------
in codeblocks:
--------------------------------------------------------------------------------------------------------------
/*WAP to input elements and search a number in that.
Pass Weight as an array as parameter.*/
#include<stdio.h>
#include<conio.h>
int search_in_array(float array[200],int size,int number);//prototype with array and size and number parameters
int main()
{

float arr[200];int k,num;            // array declaration with maximum limit 200
int size,f;               // says about size of array to be entered
printf("enter size of array\n");
scanf("%d",&size);//input of size
printf("enter elements\n");
for(k=0;k<size;k++)//loop execution
{
   scanf("%f",&arr[k]);//data inputs
}
printf("enter number to be searched\n");
scanf("%d",&num);                //number input to be searched
f=search_in_array(arr,size,num);//assigning return value to variable f
if(f==0)                        //testing the value
{
printf("number not found \n");//calling of function
}
else
{
    printf("number found and lcoation=%d",f);
}
getch();
return 0;
}
int search_in_array(float array[200],int size,int number)//body line
{

int k;//declaration
int found;

for(k=0;k<size;k++)//loop execution

{
 if(number==array[k])
        {
            found=0; //assigning 0 on its finding
            break;
        }
        else
        {
            found=1;//else assigning 1
        }
}
if(found==0)//testing the condition
{
    return k;//returning k value
}
    else
{
        return 0;//returning nothing
}
    getch();
}

No comments:

Post a Comment