#define NUMMAX 7
void main()
{
float num[NUMMAX];
double temp = 0;
printf("Plz input %d numbers, each of which ends with enter:\n", NUMMAX);
/* Input the numbers */
for(int i = 0; i < NUMMAX; i++)
{
scanf("%f", num + i); //The 2nd parameter of scanf is actually the address of variable you want to store
}
printf("%s\n", "***The initial arrays***");
for(int i = 0; i < NUMMAX; i++)
{
printf("%g ", num[i]);
}
printf("\n");
/* Sort the arrays by bubble */
for(int j = 0; j < NUMMAX; j++)
{
for(int i = NUMMAX - 1; i > 0; i--)
{
if(num[i] < num[i - 1])
{
temp = num[i];
num[i] = num[i - 1];
num[i - 1] = temp;
}
}
}
printf("%s\n", "***The bubble-sorted arrays***");
for(int i = 0; i < NUMMAX; i++)
{
printf("%g ", num[i]);
}
printf("\n");
float target = 0;
printf("%s\n", "Plz input the number you'd like to find: ");
scanf("%f", &target);
/* The bubble sort process */
int low = 0, high = NUMMAX - 1;
int i = 0;
do{
i = (low + high) / 2;
if(num[i] < target)
{
low = i + 1;
}
if(num[i] > target)
{
high = i - 1;
}
if(num[i] == target)
{
break;
}
}while(i != (low + high) / 2);
if(num[i] != target)
printf("%s\n", "Not exit");
else
printf("Got it, the position is %d\n", i);
}
Since its the first time I've ever use the C language, I tried to look up a lot of books and articles to make the grammar correct.
All the referances is paced embedded the codes and the notes are as follows:
1. For the function scanf, the 2nd parameter of scanf is actually the address of variable you want to store.
2. Scanf and printf almost have the same parameter, Where specifier is the most significant one and defines the type and the interpretation of the value of the coresponding argument.
3. Pay close attention to the process of Binary search. Many mistakes may occur in the tens lines of codes.
2 int low = 0, high = NUMMAX - 1;
3 int i = 0;
4
5 do{
6 i = (low + high) / 2;
7 if(num[i] < target)
8 {
9 low = i + 1;
10 }
11 if(num[i] > target)
12 {
13 high = i - 1;
14 }
15 if(num[i] == target)
16 {
17 break;
18 }
19 }while(i != (low + high) / 2);
20
21 if(num[i] != target)
22 printf("%s\n", "Not exit");
23 else
24 printf("Got it, the position is %d\n", i);
3.1 In each time of loop, low is iterated by i + 1, not i, or the search won't get the last element of your array. The same to high.
3.2 Simple as it may seem, let me analyse the whole structure in detail. It may have something to do with the knowledge of Discrete Mathmatics.
The simple block indicates a general principle for iterative process, initialization, saving and terminal.
Initialization: Iterative process begins after the invariant (or assertion) is initialized to be true, often by give a initial value to the argument or so.
Saving: In the iterative process, we must make sure the inviariant always be correct. So it can be proved by Mathematical Induction that both before and after the iteration, the assertion is always correct.
Terminal: Wherever the loop ends, the invariant is always correct.
All the 3 steps above prove the truth that the loop can be properly ended with the correction of invariant. To do this, let us take a look at the validation of any function.
To validate whether a funtion or assertion is correct or not, the most popular way is Syllogism.