|
| Binary Search
|
#include<stdio.h>
void main() { int mark[16] = {10, 11, 15, 18, 19, 27, 38, 39, 51, 55, 57, 61, 66, 82, 83, 95}; int low = 0, // lowest index in the range that is being searched mid, // index of the middle in the range high = 15, // index of the highest in the range answer = -1, // this will be the index if lookup is found lookup; // this is what we are searching for in mark[ ] // firstly, get the number to be searched. printf("==Playing with binary Search==\n"); printf("\nEnter a number to be searched: "); scanf_s("%d", &lookup); // start the binary search for(; high - low > 1; ) { // find the middle of the range printf("Dividing the array into two part, lower and top part\n"); mid = (high - low) / 2 + low; // if the number is at the midpoint, set the answer // and break out of loop if(mark[mid] == lookup) { printf("Found in the middle...\n"); answer = mid; break; } // if the number is not at the midpoint, adjust // either high or low if(mark[mid] > lookup) { high = mid; printf("Go and check the top half.\n"); } else { low = mid; printf("Go and check the lower half.\n"); } }
// if the number at high or low, set the answer if(answer == -1) if(mark[high] == lookup) { printf("Found in the top half.\n"); answer = high; } else if(mark[low] == lookup) { printf("Found in the lower half.\n"); answer = low; } // if answer is still unchanged, the number was not found in the array if(answer == -1) printf("\n%d not found. It is not in the array!\n", lookup); else printf("\n%d was found at index number %d.\n", lookup, answer); } | A sample input and output.
---------------------------------------------
|
| |
More Practice
1. Answer the following question.
int a[5] = {3, 7, 4, 9, 6};
Here we have an array with 5 elements. The name of the array is a. It is an array because it is defined with a set of (square) brackets. The 5 inside the brackets indicates that it has 5 elements numbered from 0 to 4. The keyword,int means that each of these 5 indexes holds an integer. a[0] is initialized to3,a[1] is initialized to7 and so on.
|
|
2. Index/subscript is the term used to refer to a slot number. Answer the following questions based on the previous question.
|
|
3. Refer to the following array and answer the questions.
int a[5] = {3, 7, 4, 9, 6};
|
|
4. Use the following code and answer the questions.
int a[5] = {3, 7, 4, 9, 6}; for(i = 0; i <= 4; i = i + 1} printf("%d\t", a[4 - i]);
|
|
for( __________ ; __________ ; ___________ ) printf("%d\t", a[i]); | for( i=4; i>=0; i=i-1) printf("%d\t", a[i]); |
|
|
|
|
| #include <stdio.h>
void main() { int count = 0, a[5] = {3, 7, 4, 9, 6}, i; for(i = 0; i <= 4; i = i + 1) if(a[i] > 5) { count = count + 1; printf("%d ", a[i]); } printf("\ncount = %d\n", count); }
|
| #include <stdio.h>
void main() { int count = 0, a[5] = {3, 7, 4, 9, 6}, i, sum=0; for(i = 0; i <= 4; i = i + 1) if(a[i] > 5) sum = sum + a[i]; printf("sum = %d\n", sum); }
|
| #include <stdio.h>
void main() { int a[5] = {3, 7, 4, 9, 6}, i, sum=0, count=0; for(i = 0; i <= 4; i = i + 1) { if( a[i] > 5 ) sum = sum + a[i]; if( a[i] <= 5 ) count = count + 1; } printf("sum is = %d\n", sum); printf("count = %d\n", count); }
|
| |
| #include <stdio.h>
void main() { int a[5] = {3, 7, 4, 9, 6}, i, largest = a[0]; for(i = 1; i <= 4; i = i + 1) if(a[i] > largest) largest = a[i]; printf("The largest element is: %d\n", largest); }
|
| #include <stdio.h>
void main() { int a[5] = {3, 7, 4, 9, 6}, i, largest = a[0], item_idx=0; for(i = 1; i <= 4; i = i + 1) if(a[i] > largest) { largest = a[i]; item_idx = i; } printf("The index of the largest is: %d\n", item_idx); }
|
#include<stdio.h>
void main() { ... ... for(i = 0; i <= 3; i = i + 1) scanf_s("%d %d", &a[i], &b[i]); ... ... ... }
Hint: The contents of a[ ] starting at index 0 are “3 2 1 7”. The contents ofb[ ] starting at index0 are “8 9 5 6”. | #include <stdio.h>
void main() { int i, a[5], b[5]; for(i = 0; i <= 3; i = i + 1) { scanf_s("%d %d", &a[i], &b[i], sizeof(int), sizeof(int)); } printf("\na[i] = "); for(i = 0; i <= 3; i = i + 1) printf("%d ", a[i]); printf("\nb[i] = "); for(i = 0; i <= 3; i = i + 1) printf("%d ", b[i]); printf("\n"); }
When i is 0, a[0] = 3 and b[0] = 8. |
#include<stdio.h>
void main() { ... ... for(i = 0; i <= 3; i = i + 1) scanf_s("%d", &a[i]); for(i = 0; i <= 3; i = i + 1) scanf_s("%d", &b[i]); ... ... ... } | #include <stdio.h>
void main() { int i, a[5], b[5]; for(i = 0; i <= 3; i = i + 1) scanf_s("%d", &a[i]); for(i = 0; i <= 3; i = i + 1) scanf_s("%d", &b[i]); printf("a[i]= "); for(i = 0; i <= 3; i = i + 1) printf("%d ", a[i]); printf("\nb[i]= "); for(i = 0; i <= 3; i = i + 1) printf("%d ", b[i]); printf("\n"); }
|
#include<stdio.h>
void main() { ... scanf_s("%d", &k); for(i = 0; k > 1; i = i + 1) { a[i] = k; scanf_s("%d", &b[i]); scanf_s("%d", &k); } ... ... ... } | #include <stdio.h>
void main() { int i, a[5], b[5], k;
printf("Enter a sample input:\n"); scanf_s("%d", &k, sizeof(int)); for(i = 0; k > 1; i = i + 1) { a[i] = k; scanf_s("%d", &b[i], sizeof(int)); scanf_s("%d", &k, sizeof(int)); printf("a[%d]=%d ", i, a[i]); printf("b[%d]=%d ", i, b[i]); } printf("\n"); }
|
#include<stdio.h>
void main() { ... scanf_s("%d", &k); for(i = 0; i <= 2; i = i + 1) { scanf_s("%d", &a[k]); scanf_s("%d", &k); } ... ... ... } |
The content of a[4] is rubbish.
|
#include<stdio.h>
void main() { ... ... float fastest = b[0]; // loop to find the fastest in team b for(i = 1; i <= 4; i = i + 1) _________________________ _________________________ // loop to count the slower ones in a for(i = 0; i <= 4; i = i + 1) _________________________ _________________________ printf("The fastest b runner could beat %d of the a runner\n", count); } | #include <stdio.h>
void main() { // the f & F modifiers used to indicate that // the type is float, by default it is treated // as double for floating point. without the // modifier compiler will generate warnings... float a[5] = {7.2F, 3.4F, 4.9F, 2.2F, 8.1F}, b[5]= {5.6f, 12.8f, 4.7f, 6.2f, 7.3f}, fastest = b[0]; int i, count = 0; // loop to find the fastest in team b for(i = 1; i <= 4; i = i + 1) { if(b[i] < fastest) fastest = b[i]; } printf("The fastest 'b' runner is at %.2f\n", fastest); // loop to count the slower ones in a for(i = 0; i <= 4; i = i + 1) { if(a[i] > 4.7) count = count +1; } printf("The fastest 'b' runner could beat\n %d of the 'a' runner(s).\n", count); }
|
--------------------------End of 1D Array---------------------