当前位置:网站首页>Find a specific number in an ordered array
Find a specific number in an ordered array
2022-07-27 04:44: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 .
边栏推荐
- Maximum value, minimum value, bubble sort in the array
- Use unity to build a WordArt system
- 题目:有一个已经按升序排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
- JS three methods of traversing arrays: map, foreach, filter
- 第六章:云数据库
- Wechat applet editor Avatar
- 有趣的C语言
- GenericServlet为什么有两个init方法
- Introduction to regular expressions of shell, general matching, special characters: ^, $,., * Character range (brackets): [], special characters: \, matching mobile phone number
- Ribbon load balancing strategy and configuration, lazy loading and hungry loading of ribbon
猜你喜欢

Chapter 6: cloud database

Deep analysis - dynamic memory management

Structural mode - decorator mode
![[final review of software engineering] knowledge points + detailed explanation of major problems (E-R diagram, data flow diagram, N-S box diagram, state diagram, activity diagram, use case diagram...)](/img/f4/70634556c4ae8fc3b087084e1e27b3.png)
[final review of software engineering] knowledge points + detailed explanation of major problems (E-R diagram, data flow diagram, N-S box diagram, state diagram, activity diagram, use case diagram...)

Grid layout

好用的shell快捷键

Unity:Resource Merging、Static Batching、Dynamic Batching、GPU Instancing

第4章 Bean对象的作用域以及生命周期

Overview of communication protocols

Yolov4网络详解
随机推荐
Using webmvcconfigurer to intercept interface requests is being enhanced (with source code)
How to set user-defined display for Jiaming Watch
P1438 boring sequence line segment tree + difference
JMeter learning notes 004-csv file line number control cycle times
【AtCoder Beginner Contest 260 (A·B·C)】
华为入局商用市场:趋势使然,挑战颇多
JS第二天(变量、变量的使用、命名规则、语法扩展)
Cloudcompare & PCL match point distance suppression
The project parameters are made into configurable items, and the @configurationproperties annotation is used
The price reduction of iphone13 is just a show. Consumers are waiting for iphone14
0动态规划中等 LeetCode467. 环绕字符串中唯一的子字符串
在有序数组找具体某个数字
深度学习领域图像分割FCN(Fully Convolutional Networks for Semantic Segmentation)
Redis interview question (2022)
F - Pre-order and In-order(Atcoder 255)
e. The difference between target and e.currenttarget
Is the e-commerce billing system important? How should the platform choose billing service providers?
Structural mode - decorator mode
打开编程的大门
The data in echart histogram is displayed at the top of the chart