You must Sign In to post a response.
  • Category: Programming

    Program to search a number from an array using pointer to array in C++.


    Preparing for C Programming exams? Have a query about writing a program to search a number from an array using pointer to an array? Find program solutions on this Ask Expert page which will help you to prepare for your exams.

    Following is a question that was asked in the MSBTE (Maharashtra State Board of Technical Education, Mumbai) Diploma in Computer Engineering question paper for Winter 2018 examination.

    This question is asked for 04 marks and hence requires some explanation along with the program code. Please let me know the solution.

    Q) Write a program to search a number from an array using pointer to array. [4 Marks]
  • Answers

    1 Answers found.
  • It is possible to assign pointer to an array. Basically array name indicates address of zeroth location of array. So when we mention array name ultimately we get address of zeroth location of an array. Then we can easily hold this address in appropriate pointer. Thus in this way we can assign pointer to array.
    When we increment the pointer by one, it is actually incremented by the size of data type it is pointing to. In our case when we increment the pointer by one, it points to next location in the array. So using pointer to array we can access every element in the array.
    Below is the program which is used to find required number within an array using pointer to array.
    Please include stdio.h and conio.h header files before main function.

    //PROGRAM TO FIND NUMBER FROM ARRAY USING POINTER TO ARRAY.
    #include
    #include
    void main(void)
    {
    int c=0,arr[10],i,find,*p;
    clrscr();
    printf("Enter any ten integers\n");
    for(p=arr;p {
    scanf("%d",p);
    }
    printf("Enter the number to find from array\n");
    scanf("%d",&find);
    printf("\nOriginal array elements are as follows\n");
    for(p=arr;p {
    printf("%d ",*p);
    }
    printf("\n\n");
    for(p=arr;p {
    c++;
    if(*p==find)
    {

    printf("\nNumber %d is present at %d position in array",find,c);
    break;
    }
    }
    if(c==0)
    {
    printf("\nNumber %d is not present in the given array",find);
    }
    getch();
    }


  • Sign In to post your comments