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