Wednesday, 3 September 2014

Test Class

//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");
}
}

15 comments:

  1. whr is ur final
    l output....??

    ReplyDelete
  2. CHECK AGAIN, IS YOUR OWN PROGRAM RUNNING...!!!

    ReplyDelete
    Replies
    1. Run my program on your machine. lab machines aren't properly configured.

      Delete
  3. Look for your outpur:-
    Enter 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.

    ReplyDelete
    Replies
    1. 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
      Here 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.

      Delete
  4. void main() or int main()???n no return 0 or no getch()...is ur progrm running??

    ReplyDelete
    Replies
    1. void 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.
      The program will exit only when you press a character.

      Delete
  5. Deepa + Piyush, Check your compiler settings and configuration (Directory Path).
    DEV 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

    ReplyDelete
  6. is your program dealing with duplicate elements???

    ReplyDelete
    Replies
    1. If there is duplicacy then the program will give the first duplicate element's address not the last one.

      Delete
  7. Enter the size of list: 5
    Enter 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

    ReplyDelete
    Replies
    1. "position of element 2 is 1,3"
      but the first position of 2 is 0...

      Delete

Note: only a member of this blog may post a comment.