Finding the specified element

Source: Internet
Author: User

In the known quantity table, find the subscript and pointer of the first element that is equal to the specified value. This example is still simple to compare.

My idea is to first create a struct structure that has two member variables, one for the array element, one for the array element, and the result to save the found results to the structure when the program process looks for it. Here is the implementation section of my program:

#include <stdio.h> #define SIZE -/** * @brief The structure of the element struct * elements, including the element's subscript and the element's pointer */struct element{intsubscript;intP;};/** * @brief Search in an array of arrays, look for the subscript and pointer of the first element equal to key * @param key to find the first element equal to key * @param A Rray the array to be looked for * @param n array with several elements * @return Returns the structure containing the subscript and pointer of the element */struct Element Search (intKeyintArray[],intN) {inti =0; struct element ele; for(i =0; i < n;i++) {if(key = = Array[i])            {ele.subscript = i; ELE.P = &array[i];returnEle }    }returnEle;}/** * Find the subscript * and pointer of the first element equal to the specified value in the known quantity table * @brief main * @return */intMainvoid){intNintIintKeyintArray[size]; printf"Please enter the length of the array:\n"); scanf"%d", &n); printf"Please enter the lements of the array:\n"); for(i =0; i < n;i++) {scanf ("%d", &array[i]); } printf ("Please enter the key for searching:\n"); scanf"%d", &key); struct element searched = Search (key,array,n);//See if a matching element has been found    if(SEARCHED.P = =0) {printf ("There is no element equals to key!! \ n "); }Else{printf ("The subscript of the element is%d.\n", Searched.subscript); printf"The pointer of the element is%d.\n", SEARCHED.P); }return 0;}

Here's what my program is running:

Finding the specified element

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.