#include <stdio.h> long linear_search(long [], long, long); int main() { long array[100], search, c, n, position; printf("Enter number of elements in array\n"); scanf("%ld", &n); printf("Enter %d numbers\n", n); for (c = 0; c < n; c++) scanf("%ld", &array[c]); printf("Enter number to search\n"); scanf("%ld",&search); position = linear_search(array, n, search); if (position == -1) printf("%d is not present in array.\n", search); else printf("%d is present at location %d.\n", search, position+1); return 0; } long linear_search(long a[], long n, long find) { long c; for (c = 0 ;c < n ; c++ ) { if (a[c] == find) return c; } return -1; }
C Programming By Vijay Jadhav
Tuesday, 24 March 2015
C program for linear search using function
C Program for Linear search
#include <stdio.h> int main() { int array[100], search, c, n; printf("Enter the number of elements in array\n"); scanf("%d",&n); printf("Enter %d integer(s)\n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); printf("Enter the number to search\n"); scanf("%d", &search); for (c = 0; c < n; c++) { if (array[c] == search) /* if required element found */ { printf("%d is present at location %d.\n", search, c+1); break; } } if (c == n) printf("%d is not present in array.\n", search); return 0; }
Output of program:
C Program of Linear search for multiple occurrences
#include <stdio.h> int main() { int array[100], search, c, n, count = 0; printf("Enter the number of elements in array\n"); scanf("%d", &n); printf("Enter %d numbers\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); printf("Enter the number to search\n"); scanf("%d", &search); for (c = 0; c < n; c++) { if (array[c] == search) { printf("%d is present at location %d.\n", search, c+1); count++; } } if (count == 0) printf("%d is not present in array.\n", search); else printf("%d is present %d times in array.\n", search, count); return 0; }
Output of code:
Monday, 23 March 2015
C program to find minimum element in array using pointers
#include <stdio.h> int main() { int array[100], *minimum, size, c, location = 1; printf("Enter the number of elements in array\n"); scanf("%d",&size); printf("Enter %d integers\n", size); for ( c = 0 ; c < size ; c++ ) scanf("%d", &array[c]); minimum = array; *minimum = *array; for ( c = 1 ; c < size ; c++ ) { if ( *(array+c) < *minimum ) { *minimum = *(array+c); location = c+1; } } printf("Minimum element found at location %d and it's value is %d.\n",
location, *minimum); return 0;
}
Output of program:
C program to find minimum element in array using function
#include <stdio.h> int find_minimum(int[], int); int main() { int c, array[100], size, location, minimum; printf("Enter the number of elements in array\n"); scanf("%d", &size); printf("Input %d integers\n", size); for (c = 0; c < size; c++) scanf("%d", &array[c]); location = find_minimum(array, size); minimum = array[location]; printf("Minimum element location = %d and value = %d.\n", location + 1, minimum); return 0; } int find_minimum(int a[], int n) { int c, min, index; min = a[0]; index = 0; for (c = 1; c < n; c++) { if (a[c] < min) { index = c; min = a[c]; } } return index; }
Output of program:
C program to find minimum element in array
#include <stdio.h> int main() { int array[100], minimum, size, c, location = 1; printf("Enter the number of elements in array\n"); scanf("%d",&size); printf("Enter %d integers\n", size); for ( c = 0 ; c < size ; c++ ) scanf("%d", &array[c]); minimum = array[0]; for ( c = 1 ; c < size ; c++ ) { if ( array[c] < minimum ) { minimum = array[c]; location = c+1; } } printf("Minimum element is present at location %d and it's value is %d.\n" ,
location, minimum); return 0; }
Output of program:
Wednesday, 18 March 2015
C program to find maximum element in array using function
#include <stdio.h> int find_maximum(int[], int); int main() { int c, array[100], size, location, maximum; printf("Input number of elements in array\n"); scanf("%d", &size); printf("Enter %d integers\n", size); for (c = 0; c < size; c++) scanf("%d", &array[c]); location = find_maximum(array, size); maximum = array[location]; printf("Maximum element location = %d and value = %d.\n", location + 1, maximum); return 0; } int find_maximum(int a[], int n) { int c, max, index; max = a[0]; index = 0; for (c = 1; c < n; c++) { if (a[c] > max) { index = c; max = a[c]; } } return index; }
Output of program:
Input number of elements in array
5
Enter 5 integers
8
6
9
3
2
Maximum element location = 3 and value = 9
Subscribe to:
Posts (Atom)