0% found this document useful (0 votes)
64 views11 pages

Arrays C Example Programs

The documents contain several C programs demonstrating common array operations and sorting algorithms: 1) The programs show how to sort arrays in ascending order, find the largest/sum of elements, insert/remove elements, and use linear/bubble sorting. 2) Array operations demonstrated include initialization, traversing with for loops, and calculating size with sizeof. 3) Sorting algorithms like bubble sort are implemented by swapping elements based on comparisons until fully sorted.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
64 views11 pages

Arrays C Example Programs

The documents contain several C programs demonstrating common array operations and sorting algorithms: 1) The programs show how to sort arrays in ascending order, find the largest/sum of elements, insert/remove elements, and use linear/bubble sorting. 2) Array operations demonstrated include initialization, traversing with for loops, and calculating size with sizeof. 3) Sorting algorithms like bubble sort are implemented by swapping elements based on comparisons until fully sorted.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 11

//C Program to arrange numbers in ascending order

/*
* C program to accept numbers as an input from user
* and to sort them in ascending order.
*/
#include <stdio.h>

void sort_numbers_ascending(int number[], int count)


{
int temp, i, j, k;
for (j = 0; j < count; ++j)
{
for (k = j + 1; k < count; ++k)
{
if (number[j] > number[k])
{
temp = number[j];
number[j] = number[k];
number[k] = temp;
}
}
}
printf("Numbers in ascending order:\n");
for (i=0;i<count;++i)
printf("%d\n",number[i]);
}
void main()
{
int i, count, number[20];
printf("How many numbers we are gonna enter:\n");
scanf("%d",&count);
printf("\nEnter the numbers one by one:\n");

for (i = 0; i < count; ++i)


scanf("%d", &number[i]);

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;

// Initialization to the first array element


max_element = arr[0];

/* Here we are comparing max_element with


* all other elements of array to store the
* largest element in the max_element variable
*/
for (i = 1; i < num; i++)
if (arr[i] > max_element)
max_element = arr[i];

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;

printf("Enter size of the array: ");


scanf("%d",&size);

printf("Enter the elements of the array: ");


for(i=0; i<size; i++)
{
scanf("%d",&arr[i]);
}
//calculating sum of entered array elements
for(i=0; i<size; i++)
{
sum+=arr[i];
}
printf("Sum of array elements is: %d",sum);

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;

/* Calculating the size of the array with this formula.


* n = sizeof(array_name) / sizeof(array_name[0])
* This is a universal formula to find number of elements in
* an array, which means it will work for arrays of all data
* types such as int, char, float etc.
*/
n = sizeof(arr) / sizeof(arr[0]);
printf("Size of the array is: %d\n", n);
return 0;
}

Output:
Size of the array is: 6

--------------------------------
______________________________________________________

//Pascal Triangle using C Program


#include <stdio.h>
int main() {
int triangle[10][10],i,j;

// initialize first row to 1's


for (i = 0; i< 10; i++) {
triangle[0][i] = 1;
}

// compute subsequent rows


for (i = 1; i< 10; i++) {
for (j = 0; j <= i; j++) {
if (j == 0 || j == i) {
triangle[i][j] = 1;
} else {
triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j];
}
}
}

// 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;

// initial array of size 10


for (i = 0; i < 10; i++)
arr[i] = i + 1;

// print the original array


for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");

// element to be inserted
x = 50;

// position at which element


// is to be inserted
pos = 5;

// increase the size by 1


n++;

// shift elements forward


for (i = n - 1; i >= pos; i--)
arr[i] = arr[i - 1];

// insert x at pos
arr[pos - 1] = x;

// print the updated array


for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");

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);

printf (" \n Enter %d elements in array: \n ", num);

// use for loop to insert elements one by one in array


for (i = 0; i < num; i++ )
{ printf (" arr[%d] = ", i);
scanf (" %d", &arr[i]);
}

// enter the position of the element to be deleted


printf( " Define the position of the array element where you want to delete: \n
");
scanf (" %d", &pos);

// check whether the deletion is possible or not


if (pos >= num+1)
{
printf (" \n Deletion is not possible in the array.");
}
else
{
// use for loop to delete the element and update the index
for (i = pos - 1; i < num -1; i++)
{
arr[i] = arr[i+1]; // assign arr[i+1] to arr[i]
}

printf (" \n The resultant array is: \n");

// display the final array


for (i = 0; i< num - 1; i++)
{
printf (" arr[%d] = ", i);
printf (" %d \n", arr[i]);
}
}
return 0;
}

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

The resultant array is:


arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 5
____________________________________
//Bubble sort program in C Program
#include <stdio.h>

void bubble_sort(int arr[], int n) {


int i, j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90},i;
int n = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, n);
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}

Output:
Sorted array: 11 12 22 25 34 64 90
__________________________________________
//// Linear Search in C

// C program to implement linear search using loops


#include <stdio.h>
// linear search function that searches the key in arr
int linearSearch(int* arr, int size, int key)
{
int i;
// starting traversal
for ( i = 0; i < size; i++) {
// checking condition
if (arr[i] == key) {
return i;
}
}
return -1;
}

// 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);

// printing result based on value returned by


// linearSearch()
if (index == -1) {
printf("The element is not present in the arr.");
}
else {
printf("The element is present at arr[%d].", index);
}

return 0;
}

Output:
The element is present at arr[1].
__________________________________________
// C Program to implement linear search using recursion
#include <stdio.h>

// Function to perform linear search


int linearSearch(int* arr, int size, int key)
{
// if there are no elements, return -1
if (size == 0)
return -1;

// if the element at (size - 1) index is equal to key,


// return (size - 1)
if (arr[size - 1] == key) {
return size - 1;
}

// if not, call linear seach for same array arr but


// reducing the size by a single element
return linearSearch(arr, size - 1, key);
}

// 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;

// calling linearSearch function


int index = linearSearch(arr, size, key);

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>

int binarySearch(int array[], int x, int low, int high) {


// Repeat until the pointers low and high meet each other
while (low <= high) {
int mid = low + (high - low) / 2;

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>

// Function to remove duplicate


// elements This function returns
// new size of modified array.
int removeDuplicates(int arr[], int n)
{
// Return, if array is empty or
// contains a single element
if (n == 0 || n == 1)
return n;

int temp[n];

// Start traversing elements


int i,j = 0;

// If current element is not equal


// to next element then store that
// current element
for (i = 0; i < n - 1; i++)
if (arr[i] != arr[i + 1])
temp[j++] = arr[i];

// Store the last element as whether


// it is unique or repeated, it hasn't
// stored previously
temp[j++] = arr[n - 1];

// Modify original array


for (i = 0; i < j; i++)
arr[i] = temp[i];

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]);

// removeDuplicates() returns new


// size of array.
n = removeDuplicates(arr, n);

// Print updated array


for (i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Output:
1 2 3 4 5
--------------------------------
____________________________________________

You might also like