# C language learning review

Keywords: C Algorithm

This is a review of the C language online course in station B

The learning contents are simple circular statements and three algorithm problems of while and for

First, list the three exercises directly:

1. Find a number in an ordered array;

2. Write code to demonstrate that multiple characters move from both ends and converge to the middle

3. Write code to simulate the user login scenario, and can only log in three times. If the three times are wrong, exit the program

The first problem uses a variety of solutions in learning. After the teacher optimizes it, a very typical "half search method" is finally obtained

```#include<stdio.h>

int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int k = 7;
int sz = sizeof(arr) / sizeof(arr);
int left = 0;
int right = sz - 1;
//Note here that if mid is set outside the while loop, an dead loop will occur
//int mid =(left + right ) /2 ;
while (left < right)
{
int mid = (left + right) / 2;
if (arr[mid] < k)
{
left = mid + 1;
}
else if (arr[mid) > k)
{
right = mid - 1;
}
else
{
printf("find it ,the number is :%d"\n",mid);
break;
}
}
if (left > right)
{
printf("can not find your aim number in here!\n");
}
return 0;
}```

This is the classic dichotomy algorithm in data structure. It is the first "possibly more advanced" (I don't know whether such words are accurate) algorithm that we beginners can contact after learning C language.

By introducing the concept of left and right subscripts, the search value is gradually approximated in an ordered sequence. It is worth mentioning that, just as the judgment condition in the while statement: the left < right loop can be executed all the time for gradual approximation.

It is not difficult to observe the code blocks in the loop. We just need to determine the size of arr[mid] and k, that is, the number to be found, change the value of the left or right subscript one by one, and finally approach until arr[mid] == k. in this case, the loop ends.

In order to facilitate you to review this article, analyze the inner side of the cyclic code block while it is hot:

```if ( arr[mid] > k )
{
right = mid - 1 ;
}```

If the number indicated by the middle mark is larger than the number to be found, subtract one from the middle value and assign it to the new right subscript. Through this figure, we can more intuitively understand the meaning of the above three lines of code, so when arr [mid] < K can also be introduced. I won't repeat it here.

In addition, here I introduce my first impression solution to this problem, which may be called "bubble sorting"? I don't quite understand the professional name:

```int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int k = 17;
int i = 0;
// Write a code, in the arr array (ordered), find 7
int sz = sizeof(arr) / sizeof(arr); // sz is the length of the array
for (i = 0; i < sz; i++)
{
if (k == arr[i])
{
printf("find it , the number is :%d\n", i);
break;
}
}
if (i == sz)
{
printf("can't find it ");
}
return 0;
}```

The idea of this one-to-one comparison to finally determine the number to be found is very simple. For beginners like us, it may be difficult to translate the idea into code. I hope I can learn more and knock more and encourage them!

The second question is an animation demonstration, which uses a loop to play a game:

```#include<stdio.h>
#include<string.h>
#include<Windows.h>
#include<stdlib.h>
int main()
{
//welcome to here!!!!!!!!!!
//#########################
//w#######################!
//we#####################!!
//wel###################!!!
//....
//....
//welcome to here!!!!!!!!!!
char arr1[] = "welcome to here !!!!!!!";
char arr2[] = "#######################";
int left = 0;
// error:
// int right = sizeof(arr1)/sizeof(arr1) -1
// for example here:
// char arr[] = " abc ";
// [a b c \0 ]
//  1 2 3  4
// 4-1 only can catch the wrong number
// we should use 4-2 instead of 4-1
int right = strlen(arr1) - 1; // or use the strlen
// tips: if you want to use the function "strlen" you should include the function library: <string.h>
while (left <= right)
{
arr2[left] = arr1[left];
arr2[right] = arr1[right];
printf("%s\n", arr2);
//Take one second off every time you print
Sleep(1000);
system("cls");// A function to execute system commands - cls -- clear the screen
left++;
right--;
}

printf("%s\n", arr2);
return 0;
}```

You can copy this code to see the effect. It's very interesting

This method is briefly introduced. It also uses the processing of left and right subscripts. Each hop pops up the leftmost (or rightmost) characters that do not appear. The expected implementation effect is like the previous comments in the main function.

It is worth mentioning that how do we determine the right subscript of a string?

The right in the first question is completed by determining the total length of the sequence and subtracting one. However, in the string, all strings naturally contain '\ 0'. If only sizeof minus one, the real right subscript cannot be determined. Here are two solutions

1,sizeof - 2

2,strlen - 1

In the code, I gave an example, [a, b, c], for reference!

In the while loop, assign the left and right subscripts of arr1 to arr2 each time, and scrape it slowly like hanging coating. Scrape the left subscript + 1 and the right subscript - 1 at the end of the loop for the next time.

In order to increase the interest of the code, Sleep(1000) is added; And system("cls"); It means to cycle every second and clear the screen.

Note that you must add the corresponding function library to use these two lines of code.

The last code is very simple. Don't review it. If beginners like me see this problem, they can try to change the content of the code and do their own diy!

```#include<string.h>
int main()
{
char password = { 0 };
int i = 0;
for (i = 0; i < 3; i++)
{
if (strcmp(password, "123456") == 0) // Cannot compare two strings with = =, only with the library function strcmp
{
break;
}
else