//This code is to find the position of element according to index value of the element in the array.
#include<stdio.h>
void main()
{
int list[100],n,i,x,t,p; //Declaration of variable;
printf("Enter the size of list: ");
scanf("%d",&n); //Entering the size of array 'list'
printf("Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&list[i]); //Insertion of elements in array 'list'
}
p=0; //Assiging a value to p
printf("Enter the element to be here: ");
scanf("%d",&x); //Entering the element whose index has to be find
for(i=0;i<n;i++)
{
if(list[i]==x) //Mactching each element of array 'list' with the entered element
{
p=i; //Assinging the index value to variable p if the match success
break; //Break operation stops the searching operation if the element found
}
}
if(p>=0)
{
printf("\nposition of element %d is %d",x,p); //Position on element i.e. index value in array 'list'
}
else
{
printf("Try again");
}
}
#include<stdio.h>
void main()
{
int list[100],n,i,x,t,p; //Declaration of variable;
printf("Enter the size of list: ");
scanf("%d",&n); //Entering the size of array 'list'
printf("Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&list[i]); //Insertion of elements in array 'list'
}
p=0; //Assiging a value to p
printf("Enter the element to be here: ");
scanf("%d",&x); //Entering the element whose index has to be find
for(i=0;i<n;i++)
{
if(list[i]==x) //Mactching each element of array 'list' with the entered element
{
p=i; //Assinging the index value to variable p if the match success
break; //Break operation stops the searching operation if the element found
}
}
if(p>=0)
{
printf("\nposition of element %d is %d",x,p); //Position on element i.e. index value in array 'list'
}
else
{
printf("Try again");
}
}
OUTPUT???
ReplyDeleteYour compiler error....:D
Deletewhr is ur final
ReplyDeletel output....??
Reinstall DEV C++ compiler....:D
DeleteCHECK AGAIN, IS YOUR OWN PROGRAM RUNNING...!!!
ReplyDeleteRun my program on your machine. lab machines aren't properly configured.
DeleteLook for your outpur:-
ReplyDeleteEnter the size of list: 4
Enter the elements: 2
2
2
3
Enter the element to be here: 2
position of the element is 0
Correct it.
The program is not for the duplicacy but if duplicay occur in input then the output should give the first position of the element not the last. :D
DeleteHere size is 4 n elements are {2,2,2,3} with relative index positions {0,1,2,3}. Element has to be here is '2' and it has found at position '0'. But it is also at the other positions too but '0' is the first position so that is the output.
void main() or int main()???n no return 0 or no getch()...is ur progrm running??
ReplyDeletevoid main() means there will no return value of any type. getch() function is used to take input from the use through keyboard but doesn't print it. It just used for freezing the output screen.
DeleteThe program will exit only when you press a character.
Deepa + Piyush, Check your compiler settings and configuration (Directory Path).
ReplyDeleteDEV C++ doesn't require additional header file to freeze the output screen if the settings are correct. Do check the settings if you are using DEV C++ compiler.
In Turbo C++ you need to include header file and the function getch() to freeze the output. Otherwise you will see the current program's output in next program compilation.
:P :P
is your program dealing with duplicate elements???
ReplyDeleteIf there is duplicacy then the program will give the first duplicate element's address not the last one.
DeleteEnter the size of list: 5
ReplyDeleteEnter the elements: 1
2
1
2
3
Enter the element to be here: 2
position of element 2 is 1
the output should be like position of element 2 is 1 ,3
"position of element 2 is 1,3"
Deletebut the first position of 2 is 0...