当前位置:网站首页>Find a specific number in an ordered array
Find a specific number in an ordered array
2022-07-27 02:23:00 【GD_ small_ bit】
Hello everyone , I'm still the blogger who likes to share programming knowledge , At the same time, I will also share some interesting C Code of language applet , Let you experience happiness in the process of learning programming , And I'm committed to embodying programming ideas in the article , Focus a little , The next issue is more exciting .
Let's get down to business , today , I bring you to find a specific number in an ordered array and print its subscript , The difficulty is easy , People who just learn programming can learn to knock . Since we want to find relevant numbers in the array , And print it out , So we need to quote input , Output the header file and create an ordered array . as follows :
#include<stdio.h>
int main ()
{
int arr[]={
1,2,3,4,5,6,7,8,9,10};
return 0;
}
Last time 《C The fun of language 》 In the article , I have mentioned the knowledge of subscript of array and the element of array can be found by subscript , So here , We still use subscripts to find . Since we use subscripts to find , We need a variable to start from zero , And you should add one every time to ensure that you can find every element in the array , So as to judge whether the search requirements are met , So we introduced a Variable i and for loop To meet the above conditions .
Of course , stay for loop Under the circumstances , We also need to add a qualification . Since we are looking for the desired array in the array , and Variable i Is used to find array elements , Then we must limit Variable i To ensure that the elements to be searched are in the array , By the way for loop The limiting conditions in . That limit Variable i What are you talking about ? We know that the subscript of the array is from 0 At the beginning , This causes the subscript of the last element of the array to be equal to the number of array elements minus one , Then we can calculate the size of the number of array elements minus one , And let Variable i Less than or equal to this element , This makes our Variable i The element you are looking for is in the array .
Next , Is to think about how to find the number of elements , We can use sizeof Find the size of the entire array , And remove the use sizeof Find the data of the size of a single array element , So as to get the number of array elements . Sum up , as follows :
#include<stdio.h>
int main ()
{
int i = 0;
int arr [] = {
1,2,3,4,5,6,7,8,9,10};
int sz = sizeof(arr)/sizeof(arr[0])-1;
for(i=0;i<=sz;i++)
{
}
return 0;
}
Next , We can set another Variable k, This Variable k Used to assign the element we are looking for , Here we assume that the element we are looking for is 7, use Variable k With our arr[i] That is, the elements of the array are judged one by one , Until we find the array element we want . Then there is the problem of judgment , We can use if sentence Judge , that if sentence Where should I put it ? As mentioned above , We are for loop To find the elements in the array , So we have to for loop To judge . Determine what the execution statement is after success , We need to add print function , Print the subscript of the element we found , And join break Statement out of loop .
Of course , We are for loop After that, we still haven't found the element we want , We should tell them that the element was not found , So we should be in the whole for loop Add the print function later , Tell them that the element was not found . Sum up , as follows :
#include<stdio.h>
int main ()
{
int i = 0;
int arr [] = {
1,2,3,4,5,6,7,8,9,10};
int sz = sizeof(arr)/sizeof(arr[0])-1;
int k = 7;
for(i=0;i<=sz;i++)
{
if(k==arr[i])
{
printf(" eureka , The subscript is :%d\n",i);
break;
}
}
printf(" Can't find .\n");
return 0;
}
Here is the code in VS2010 The result of running in version environment .

Careful friends will know , The above operation results are somewhat unsatisfactory , It has been found , But also printed Can't find Sentences , Then what's wrong ? The result itself comes from the second print function , First, follow me to analyze , Come to the second printing function, there are several cases ? Two kinds of . When we find the element we want , stay break Statement , Then jump out of the loop , Here comes the second print function , This is the first case . When our for loop At the end, we will also come to the second printing function , This is the second case . And what we want , No more than is for loop At the end , That is, when we don't find the element we want , Perform the second print function . Then it is necessary for us to make a judgment when executing the second print function , And we still consider the conditions Variable i, When the last time I entered for loop when , Variable i The size of is equal to Variable sz Size , When out of circulation , Variable i Add one , Now Variable i Your stool and urine are Variable sz Add one . let me put it another way , When for At the end of the loop , We Variable i The size is Variable sz Add one . Sum up , The code is as follows :
```c
#include<stdio.h>
int main ()
{
int i = 0;
int arr [] = {
1,2,3,4,5,6,7,8,9,10};
int sz = sizeof(arr)/sizeof(arr[0])-1;
int k = 7;
for(i=0;i<=sz;i++)
{
if(k==arr[i])
{
printf(" eureka , The subscript is :%d\n",i);
break;
}
}
if(i==sz+1)
{
printf(" Can't find .\n");
}
return 0;
}
Our violent search number code is completed , Of course , This code can be optimized , That is, the method of binary search . Just imagine , When we look for a job , The interviewer after you write the code , Ask you , Can this program be optimized , As one can imagine , How important it is to keep learning optimization ideas . Next stage , I will bring you two point search , Focus a little , Prevent loss .
边栏推荐
猜你喜欢

C language - assignment operator, compound assignment operator, self increasing and self decreasing operator, comma operator, conditional operator, goto statement, comment

MySQL course 1. simple command line -- simple record welcome to supplement and correct errors

Lora communication application development

TCP的三次握手与四次断开

Codeforces Round #807 (Div. 2), problem: (C) Mark and His Unfinished Essay

HCIP-第四天-OSPF路由协议

静态路由综合实验

最新京东短信登录+傻妞机器人保姆级部署教程(2022/7/24)

JUC并发编程

C语言——第一个程序、打印、变量和常量
随机推荐
RISC-V工具链编译笔记
Lora gateway node converges sensor data
Use of golang - sync package (waitgroup, once, mutex, rwmutex, cond, pool, map)
WAN technology experiment
First knowledge of C language (2)
Codeforces Round #810 (Div. 2), problem: (B) Party
Codeforces Round #810 (Div. 2), problem: (B) Party
入住博客第一天【冲八万】!
怎么判断一个数是奇数还是偶数?
微信小程序:用户微信登录流程(附:流程图+源码)
Static routing default routing VLAN experiment
HCIP-第四天-OSPF路由协议
npm报错, Error: EPERM: operation not permitted, mkdir
7.13 Weilai approved the written examination in advance
Can bus communication application
Golang中的错误处理
The latest C language introduction and advanced - the most complete and detailed C language tutorial in history!! Section 1 - overview of C language
Golang implements TCP chat room
Republishing and routing strategy of OSPF
在有序数组找具体某个数字