0% found this document useful (0 votes)
14 views

Arrays

all arrays c programming

Uploaded by

Shaik Masood 246
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Arrays

all arrays c programming

Uploaded by

Shaik Masood 246
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

1) C program to declare, initialize, input and print array elements

#include <stdio.h>

#define MAX_SIZE 1000 // Maximum array size

int main()

int arr[MAX_SIZE]; // Declare an array of MAX_SIZE

int i, N;

/* Input array size */

printf("Enter size of array: ");

scanf("%d", &N);

/* Input elements in array */

printf("Enter %d elements in the array : ", N);

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

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

/*

* Print all elements of array

*/

printf("\nElements in array are: ");

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

printf("%d, ", arr[i]);

return 0;

}
2) C program to print all negative elements in array

#include <stdio.h>

#define MAX_SIZE 100 // Maximum array size

int main()

int arr[MAX_SIZE]; // Declare array of MAX_SIZE

int i, N;

/* Input size of the array */

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

scanf("%d", &N);

/* Input elements in the array */

printf("Enter elements in array : ");

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

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

printf("\nAll negative elements in array are : ");

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

/* If current array element is negative */

if(arr[i] < 0)

printf("%d\t", arr[i]);

return 0;

}
3) C program to find sum of array elements

#include <stdio.h>

#define MAX_SIZE 100

int main()

int arr[MAX_SIZE];

int i, n, sum=0;

/* Input size of the array */

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

scanf("%d", &n);

/* Input elements in array */

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

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

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

// Add each array element to sum

sum += arr[i];

printf("Sum of all elements of array = %d", sum);

return 0;

4) C program to find maximum and minimum element in array

#include <stdio.h>

#define MAX_SIZE 100 // Maximum array size


int main()

int arr[MAX_SIZE];

int i, max, min, size;

/* Input size of the array */

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

scanf("%d", &size);

/* Input array elements */

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

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

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

/* Assume first element as maximum and minimum */

max = arr[0];

min = arr[0];

/*

* Find maximum and minimum in all array elements.

*/

for(i=1; i<size; i++)

/* If current element is greater than max */

if(arr[i] > max)

max = arr[i];

/* If current element is smaller than min */

if(arr[i] < min)


{

min = arr[i];

/* Print maximum and minimum element */

printf("Maximum element = %d\n", max);

printf("Minimum element = %d", min);

return 0;

5) C program to count even and odd elements in array

#include <stdio.h>

#define MAX_SIZE 100 //Maximum size of the array

int main()

int arr[MAX_SIZE];

int i, size, even, odd;

/* Input size of the array */

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

scanf("%d", &size);

/* Input array elements */

printf("Enter %d elements in array: ", size);

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

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

/* Assuming that there are 0 even and odd elements */


even = 0;

odd = 0;

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

/* If the current element of array is even then increment even count */

if(arr[i]%2 == 0)

even++;

else

odd++;

printf("Total even elements: %d\n", even);

printf("Total odd elements: %d", odd);

return 0;

6) C program to find second largest number in array

#include <stdio.h>

#include <limits.h> // For INT_MIN

#define MAX_SIZE 1000 // Maximum array size

int main()

int arr[MAX_SIZE], size, i;

int max1, max2;

/* Input size of the array */


printf("Enter size of the array (1-1000): ");

scanf("%d", &size);

/* Input array elements */

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

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

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

max1 = max2 = INT_MIN;

/*

* Check for first largest and second

*/

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

if(arr[i] > max1)

/*

* If current element of the array is first largest

* then make current max as second max

* and then max as current array element

*/

max2 = max1;

max1 = arr[i];

else if(arr[i] > max2 && arr[i] < max1)

/*

* If current array element is less than first largest

* but is greater than second largest then make it

* second largest
*/

max2 = arr[i];

printf("First largest = %d\n", max1);

printf("Second largest = %d", max2);

return 0;

7) C program to copy all elements of one array to another

#include <stdio.h>

#define MAX_SIZE 100

int main()

int source[MAX_SIZE], dest[MAX_SIZE];

int i, size;

/* Input size of the array */

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

scanf("%d", &size);

/* Input array elements */

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

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

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

/*

* Copy all elements from source array to dest array

*/
for(i=0; i<size; i++)

dest[i] = source[i];

/*

* Print all elements of source array

*/

printf("\nElements of source array are : ");

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

printf("%d\t", source[i]);

/*

* Print all elements of dest array

*/

printf("\nElements of dest array are : ");

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

printf("%d\t", dest[i]);

return 0;

8) C program to insert an element in array

/**

* C program to insert an element in array at specified position

*/

#include <stdio.h>

#define MAX_SIZE 100

int main()
{

int arr[MAX_SIZE];

int i, size, num, pos;

/* Input size of the array */

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

scanf("%d", &size);

/* Input elements in array */

printf("Enter elements in array : ");

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

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

/* Input new element and position to insert */

printf("Enter element to insert : ");

scanf("%d", &num);

printf("Enter the element position : ");

scanf("%d", &pos);

/* If position of element is not valid */

if(pos > size+1 || pos <= 0)

printf("Invalid position! Please enter position between 1 to %d", size);

else

/* Make room for new array element by shifting to right */

for(i=size; i>=pos; i--)

arr[i] = arr[i-1];

}
/* Insert new element at given position and increment size */

arr[pos-1] = num;

size++;

/* Print array after insert operation */

printf("Array elements after insertion : ");

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

printf("%d\t", arr[i]);

return 0;

9) C program to delete element from an array

#include <stdio.h>

#define MAX_SIZE 100

int main()

int arr[MAX_SIZE];

int i, size, pos;

/* Input size and element in array */

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

scanf("%d", &size);

printf("Enter elements in array : ");

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

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

/* Input element position to delete */


printf("Enter the element position to delete : ");

scanf("%d", &pos);

/* Invalid delete position */

if(pos < 0 || pos > size)

printf("Invalid position! Please enter position between 1 to %d", size);

else

/* Copy next element value to current element */

for(i=pos-1; i<size-1; i++)

arr[i] = arr[i + 1];

/* Decrement array size by 1 */

size--;

/* Print array after deletion */

printf("\nElements of array after delete are : ");

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

printf("%d\t", arr[i]);

return 0;

}
10) C program count total duplicate elements in array

#include <stdio.h>

#define MAX_SIZE 100 // Maximum array size

int main()

int arr[MAX_SIZE];

int i, j, size, count = 0;

/* Input size of array */

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

scanf("%d", &size);

/* Input elements in array */

printf("Enter elements in array : ");

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

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

/*

* Find all duplicate elements in array

*/

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

for(j=i+1; j<size; j++)

/* If duplicate found then increment count by 1 */

if(arr[i] == arr[j])

count++;

break;
}

printf("\nTotal number of duplicate elements found in array = %d", count);

return 0;

11) C program to delete duplicate elements from array

/**

* C program to delete all duplicate elements from array

*/

#include <stdio.h>

#define MAX_SIZE 100 // Maximum size of the array

int main()

int arr[MAX_SIZE]; // Declares an array of size 100

int size; // Total number of elements in array

int i, j, k; // Loop control variables

/* Input size of the array */

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

scanf("%d", &size);

/* Input elements in the array */

printf("Enter elements in array : ");

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

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

}
/*

* Find duplicate elements in array

*/

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

for(j=i+1; j<size; j++)

/* If any duplicate found */

if(arr[i] == arr[j])

/* Delete the current duplicate element */

for(k=j; k < size - 1; k++)

arr[k] = arr[k + 1];

/* Decrement size after removing duplicate element */

size--;

/* If shifting of elements occur then don't increment j */

j--;

/*

* Print array after deleting duplicate elements

*/

printf("\nArray elements after deleting duplicates : ");

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

printf("%d\t", arr[i]);
}

return 0;

12) C program to merge two sorted array

/**

* C program to merge two sorted array in ascending order

*/

#include <stdio.h>

#define MAX_SIZE 100 // Maximum size of the array

int main()

int arr1[MAX_SIZE], arr2[MAX_SIZE], mergeArray[MAX_SIZE * 2];

int size1, size2, mergeSize;

int index1, index2, mergeIndex;

int i;

/* Input size of first array */

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

scanf("%d", &size1);

/* Input elements in first array */

printf("Enter elements in first array : ");

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

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

/* Input size of second array */

printf("\nEnter the size of second array : ");

scanf("%d", &size2);
/* Input elements in second array */

printf("Enter elements in second array : ");

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

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

mergeSize = size1 + size2;

/*

* Merge two array in ascending order

*/

index1 = 0;

index2 = 0;

for(mergeIndex=0; mergeIndex < mergeSize; mergeIndex++)

/*

* If all elements of one array

* is merged to final array

*/

if(index1 >= size1 || index2 >= size2)

break;

if(arr1[index1] < arr2[index2])

mergeArray[mergeIndex] = arr1[index1];

index1++;

else
{

mergeArray[mergeIndex] = arr2[index2];

index2++;

/*

* Merge remaining array elements

*/

while(index1 < size1)

mergeArray[mergeIndex] = arr1[index1];

mergeIndex++;

index1++;

while(index2 < size2)

mergeArray[mergeIndex] = arr2[index2];

mergeIndex++;

index2++;

/*

* Print merged array

*/

printf("\nArray merged in ascending order : ");

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

printf("%d\t", mergeArray[i]);

return 0;

}
13) C program to find reverse of array

/**

* C program to print array in reverse order

*/

#include <stdio.h>

#define MAX_SIZE 100 // Defines maximum size of array

int main()

int arr[MAX_SIZE];

int size, i;

/* Input size of array */

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

scanf("%d", &size);

/* Input array elements */

printf("Enter elements in array: ");

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

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

/*

* Print array in reversed order

*/

printf("\nArray in reverse order: ");

for(i = size-1; i>=0; i--)

printf("%d\t", arr[i]);

return 0;
}

14) C program to search element in an array

/**

* C program to search element in array

*/

#include <stdio.h>

#define MAX_SIZE 100 // Maximum array size

int main()

int arr[MAX_SIZE];

int size, i, toSearch, found;

/* Input size of array */

printf("Enter size of array: ");

scanf("%d", &size);

/* Input elements of array */

printf("Enter elements in array: ");

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

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

printf("\nEnter element to search: ");

scanf("%d", &toSearch);

/* Assume that element does not exists in array */

found = 0;

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

{
/*

* If element is found in array then raise found flag

* and terminate from loop.

*/

if(arr[i] == toSearch)

found = 1;

break;

/*

* If element is not found in array

*/

if(found == 1)

printf("\n%d is found at position %d", toSearch, i + 1);

else

printf("\n%d is not found in the array", toSearch);

return 0;

You might also like