Arrays C Example Programs
Arrays C Example Programs
/*
* C program to accept numbers as an input from user
* and to sort them in ascending order.
*/
#include <stdio.h>
sort_numbers_ascending(number, count);
}
Output:
Enter the numbers one by one:99
6
-99
8
0
34
1
Numbers in ascending order:
-99
0
1
6
8
34
99
________________________________________________
//C Program to find largest element of an Array
#include <stdio.h>
/* This is our function to find the largest
* element in the array arr[]
*/
int largest_element(int arr[], int num)
{
int i, max_element;
return max_element;
}
int main()
{
int arr[] = {1, 24, 145, 20, 8, -101, 300};
int n = sizeof(arr)/sizeof(arr[0]);
printf("Largest element of array is %d", largest_element(arr, n));
return 0;
}
Output:
Largest element of array is 300
--------------------------------
Process exited after 0.01048 seconds with return value 0
Press any key to continue . . .
________________________________________________
//Program to find sum of array elements using loops
#include <stdio.h>
int main()
{
int arr[100],size,sum=0,i;
return 0;
}
Output:
Enter size of the array: 5
Enter the elements of the array: 6
-9
78
54
94
Sum of array elements is: 223
______________________________________________
// Sum of array elements using Recursion
#include <stdio.h>
int sum_array_elements( int arr[], int n ) {
if (n < 0) {
//base case:
return 0;
} else{
//Recursion: calling itself
return arr[n] + sum_array_elements(arr, n-1);
}
}
int main()
{
int array[] = {1,2,3,4,5,6,7};
int sum;
sum = sum_array_elements(array,6);
printf("Sum of array elements is: %d",sum);
return 0;
}
Output:
Sum of array elements is: 28
--------------------------------
_________________________________________________
// Sum of array elements using scanf() and Recursion function
#include <stdio.h>
int sum_array_elements( int arr[], int n ) {
if (n < 0) {
//base case:
return 0;
} else{
//Recursion: calling itself
return arr[n] + sum_array_elements(arr, n-1);
}
}
int main()
{
int array[100];
//int array[] = {1,2,3,4,5,6,7};
int sum,n,i;
printf("Enter the size of the array:\n");
scanf("%d",&n);
printf("Enter the elements of the array one by one:\n");
for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}
sum = sum_array_elements(array,n);
printf("Sum of array elements is: %d",sum);
return 0;
}
Output:
Enter the size of the array:
5
Enter the elements of the array one by one:
5
6
7
8
9
Sum of array elements is: 35
--------------------------------
___________________________________________________
//C Program to Find the Number of Elements in an Array
//Program to find the size of an array
#include <stdio.h>
int main()
{
double arr[] = {11, 22, 33, 44, 55, 66};
int n;
Output:
Size of the array is: 6
--------------------------------
______________________________________________________
// display triangle
for (i = 0; i< 10; i++) {
for (j = 0; j <= i; j++) {
printf("%d ", triangle[i][j]);
}
printf("\n");
}
return 0;
}
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
____________________________________________
//C program to Insert an element in an Array
// C Program to Insert an element
// at a specific position in an Array
#include <stdio.h>
int main()
{
int arr[100] = { 0 };
int i, x, pos, n = 10;
// element to be inserted
x = 50;
// insert x at pos
arr[pos - 1] = x;
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
1 2 3 4 50 5 6 7 8 9 10
_____________________________________
//Remove an element from an array in C
/* program to remove the specific elements from an array in C. */
#include <stdio.h>
#include <conio.h>
int main ()
{
// declaration of the int type variable
int arr[50];
int pos, i, num; // declare int type variable
printf (" \n Enter the number of elements in an array: \n ");
scanf (" %d", &num);
Output:
Enter 5 elements in array:
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
Define the position of the array element where you want to delete:
4
Output:
Sorted array: 11 12 22 25 34 64 90
__________________________________________
//// Linear Search in C
// Driver code
int main()
{
int arr[10] = { 3, 4, 1, 7, 5, 8, 11, 42, 3, 13 };
int size = sizeof(arr) / sizeof(arr[0]);
int key = 4;
// calling linearSearch
int index = linearSearch(arr, size, key);
return 0;
}
Output:
The element is present at arr[1].
__________________________________________
// C Program to implement linear search using recursion
#include <stdio.h>
// Driver code
int main()
{
int arr[5] = { 6, 7, 9, 1, 5, 11, 8, 12, 2, 3 };
int size = sizeof(arr) / sizeof(int);
int key = 4;
if (index == -1) {
printf("The element is not present in the list.");
}
else {
printf("The element is present at arr[%d].", index);
}
return 0;
}
Output:
The element is not present in the list.
_________________________________________________
// Binary Search in C
#include <stdio.h>
if (array[mid] == x)
return mid;
if (array[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main(void) {
int array[] = {3, 4, 5, 6, 7, 8, 9};
int n = sizeof(array) / sizeof(array[0]);
int x = 4;
int result = binarySearch(array, x, 0, n - 1);
if (result == -1)
printf("Not found");
else
printf("Element is found at index %d", result);
return 0;
}
Output:
Element is found at index 1
________________________________________________
//C Program To Remove Duplicates From Sorted Array
// C program to remove duplicates
#include <stdio.h>
int temp[n];
return j;
}
// Driver code
int main()
{ int i;
int arr[] = {1, 2, 2, 3, 4,
4, 4, 5, 5};
int n = sizeof(arr) / sizeof(arr[0]);