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

C Coding Arrays Examples

The document provides examples of C programs to perform various operations on arrays: 1. It shows programs to sort numeric and string arrays. 2. A program to check if an array contains a specific value. 3. A program to find the index of an element in an array. 4. A program to remove a specific element from an array. 5. A program to insert an element into an array at a given position. 6. A program to reverse the elements of an integer array. 7. A program to find the common elements between two integer arrays. 8. The document begins providing an example to remove duplicate elements from an array.

Uploaded by

editsgamer
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)
28 views

C Coding Arrays Examples

The document provides examples of C programs to perform various operations on arrays: 1. It shows programs to sort numeric and string arrays. 2. A program to check if an array contains a specific value. 3. A program to find the index of an element in an array. 4. A program to remove a specific element from an array. 5. A program to insert an element into an array at a given position. 6. A program to reverse the elements of an integer array. 7. A program to find the common elements between two integer arrays. 8. The document begins providing an example to remove duplicate elements from an array.

Uploaded by

editsgamer
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/ 16

C homeworks - Arrays

1. Write a C program to sort a numeric array and a string array.

#include <stdio.h>
#include <string.h>

int main() {
int numar[] = {12, 5, 23, 8, 41, 17};
int numsiz = sizeof(numar) / sizeof(numar[0]);

char strar[][50] = {"apple", "orange", "banana", "grape", "kiwi"};


int strsiz = sizeof(strar) / sizeof(strar[0]);

for (int i = 0; i < numsiz - 1; i++) {


for (int j = 0; j < numsiz - i - 1; j++) {
if (numar[j] > numar[j + 1]) {

int temp = numar[j];


numar[j] = numar[j + 1];
numar[j + 1] = temp;
}
}
}

for (int i = 0; i < strsiz - 1; i++) {


for (int j = 0; j < strsiz - i - 1; j++) {
if (strcmp(strar[j], strar[j + 1]) > 0) {

char temp[50];
strcpy(temp, strar[j]);
strcpy(strar[j], strar[j + 1]);
strcpy(strar[j + 1], temp);
}
}
}

printf("Sorted Numeric Array: ");


for (int i = 0; i < numsiz; i++) {
printf("%d ", numar[i]);
}

printf("\nSorted String Array: ");


for (int i = 0; i < strsiz; i++) {
printf("%s ", strar[i]);
}

C homeworks - Arrays 1
return 0;
}

2. Write a C program to test if an array contains a specific value.

#include <stdio.h>

int main() {
int arr[7] = {12, 11, 45, 6, 5, 9, 8};
int s, found=0;

printf("Enter the value to search: ");


scanf("%d", &s);

for (int i = 0; i < 7; i++) {


if (arr[i] == s) {
found = 1;
break;
}
}
if (found) {
printf("The array contains the value %d.\n", s);
} else {
printf("The array does not contain the value %d.\n", s);
}

return 0;
}

3. Write a C program to find the index of an array element.

#include <stdio.h>

int main() {
int x[5] = {10, 20, 30, 40, 50};
int target, index = -1;

for(int j=0;j<5;j++){
printf("%d ", x[j]);
}
c
printf("\nWrite the target.\n");
scanf("%d", &target);

for (int i = 0; i < sizeof(x) / sizeof(x[0]); i++) {


if (x[i] == target) {

C homeworks - Arrays 2
index = i;
break;
}
}
if (index != -1) {
printf("Element %d found at index %d\n", target, index);
} else {
printf("Element %d not found in the array\n", target);
}
return 0;
}

4. Write a C program to remove a specific element from an array.

#include <stdio.h>
#include <string.h>

int main() {
int x[100],y,z,b=0;
printf("Please enter how many integer do you want to enter:\n");
scanf("%d", &y);

printf("Please enter %d value in an order:\n",y);


for(int i=0;i<y;i++){
scanf("%d", &x[i]);
}
for(int i=0;i<y;i++){
printf("%d ",x[i]);
}
printf("\nWhich character do you want to remove:\n");
scanf("%d",&z);
for(int j=0;j<y;j++){
if(x[j]!=z){
b++;
}
else{break;}
}

printf("Your new array is -> ");


for(int i=0;i<b;i++){
printf("%d ",x[i]);
}
for(b;b<y-1;b++){
printf("%d ",x[b+1]);
}
return 0;
}

C homeworks - Arrays 3
5. Write a C program to insert an element (specific position) into an array.

#include <stdio.h>

#define maxs 100

int main() {
int size, position, value;

printf("Enter the array size (max %d): ", maxs);


scanf("%d", &size);

if (size <= 0 || size > maxs) {


printf("Invalid array size. Exiting program.\n");
return 1;
}

int arr[maxs];

printf("Enter %d elements for the array:\n", size);


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

printf("Enter the position to insert (0 to %d): ", size);


scanf("%d", &position);

if (position < 0 || position > size) {


printf("Invalid position. Exiting program.\n");
return 1;
}

printf("Enter the value to insert: ");


scanf("%d", &value);

for (int i = size; i > position; i--) {


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

arr[position] = value;
size++;

printf("Array after insertion:\n");


for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}

return 0;
}

C homeworks - Arrays 4
6. Write a C program to reverse an array of integer values.

#include <stdio.h>

int main() {
int arr[] = {12, 11, 12, 21, 41, 43, 21};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Tersten yazdirilmis array: ");


for (int i = n - 1; i >= 0; i--) {
printf("%d ", arr[i]); }

return 0;
}

7. Write a C program to find the common elements between two arrays of integers.

#include <stdio.h>

int main() {
int s1, s2;

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


scanf("%d", &s1);

int ar1[s1];

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


for (int i = 0; i < s1; i++) {
scanf("%d", &ar1[i]);
}

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


scanf("%d", &s2);

int ar2[s2];

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


for (int i = 0; i < s2; i++) {
scanf("%d", &ar2[i]);
}

printf("Common Elements: ");


for (int i = 0; i < s1; i++) {
for (int j = 0; j < s2; j++) {
if (ar1[i] == ar2[j]) {
// Kontrol için bir değişken

C homeworks - Arrays 5
int isCommon = 0;
for (int k = 0; k < i; k++) {
if (ar1[i] == ar1[k]) {
isCommon = 1;
break;
}
}

if (!isCommon) {
printf("%d ", ar1[i]);
}
break;
}
}
}

printf("\n");

return 0;
}

8. Write a C program to remove duplicate elements from an array.

#include <stdio.h>

int main() {
int arr[7] = {12, 11, 12, 21, 41, 43, 21};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

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


for (int j = i + 1; j < n;) {
if (arr[i] == arr[j]) {
for (int k = j; k < n - 1; k++) {
arr[k] = arr[k + 1];
}
n--;
} else {
j++;
}
}
}

printf("\n\n\nArray after the duplicates removed: ");


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

C homeworks - Arrays 6
printf("%d ", arr[i]);
}

return 0;
}

9. Write a C program to test the equality of two arrays.

#include <stdio.h>

int main() {
int array1[] = {1, 2, 3, 4, 5};
int array2[] = {1, 2, 3, 4, 3};
int equal = 1;

for (int i = 0; i < sizeof(array1) / sizeof(array1[0]); ++i) {


if (array1[i] != array2[i]) {
equal = 0;
break;
}
}
if (equal) {
printf("Arrays are equal.\n");
} else {
printf("Arrays are not equal.\n");
}

return 0;
}

10. Write a C program to separate even and odd numbers of an given array of integers.
Put all even numbers first, and then odd numbers.

#include <stdio.h>

#define N 10

int main() {
int n = N;
int array[N] = {12, 7, 45, 23, 56, 89, 78, 34, 67, 90};

int even[N], odd[N];


int evenCount = 0, oddCount = 0;

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


if (array[i] % 2 == 0) {

C homeworks - Arrays 7
even[evenCount++] = array[i];
} else {
odd[oddCount++] = array[i];
}
}

printf("Even numbers: ");


for (int i = 0; i < evenCount; ++i) {
printf("%d ", even[i]);
}
printf("\n");

printf("Odd numbers: ");


for (int i = 0; i < oddCount; ++i) {
printf("%d ", odd[i]);
}
printf("\n");

return 0;
}

11. Write a C program to find the k largest elements in a given array. Elements in the
array can be in any order.

#include <stdio.h>

int main() {
int n, k;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);

if (n <= 0) {
printf("Array size should be greater than 0.\n");
return 1;
}

int ar1[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &ar1[i]);
}

while (1) {
printf("Enter the value of k: ");
scanf("%d", &k);

if (k > 0 && k <= n) {


break;
} else {

C homeworks - Arrays 8
printf("Invalid value of k. It should be between 1 and %d.\n", n);
}
}

printf("The %d largest elements in the array are:\n", k);


for (int i = 0; i < k; i++) {
int max = ar1[0];
int idx = 0;
for (int j = 1; j < n; j++) {
if (ar1[j] > max) {
max = ar1[j];
idx = j;
}
}
printf("%d ", max);
ar1[idx] = -2147483648;
}

return 0;
}

12. Write a C program to find the numbers greater than the average of the numbers of
a given array.

#include <stdio.h>

int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);

int ar1[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &ar1[i]);
}

int sum = 0;
for (int i = 0; i < n; i++) {
sum += ar1[i];
}

int avg = sum / n;

printf("Numbers greater than the average are:\n");


for (int i = 0; i < n; i++) {
if (ar1[i] > avg) {
printf("%d ", ar1[i]);
}

C homeworks - Arrays 9
}

return 0;
}

13. Write a C program to find the length of the longest consecutive sequence of a
given array of integers

#include <stdio.h>

int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);

int ar1[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &ar1[i]);
}

int max_len = 1;
int current_len = 1;

for (int i = 1; i < n; i++) {


if (ar1[i] == ar1[i - 1] + 1) {
current_len++;
} else {
if (current_len > max_len) {
max_len = current_len;
}
current_len = 1;
}
}

if (current_len > max_len) {


max_len = current_len;
}

printf("Length of the longest consecutive sequence: %d\n", max_len);

return 0;
}

14. Write a C program to divide a given array of integers into given k non-empty
subsets whose sums are all equal. Return true if all sums are equal otherwise return

C homeworks - Arrays 10
false

#include <stdio.h>
// BİTMEDİ. TAMAMLAMADAN PDF YE KOYMA
int main() {
int n, k, sum = 0;
scanf("%d %d", &n, &k);

int ar1[100];
for (int i = 0; i < n; ++i) {
scanf("%d", &ar1[i]);
sum += ar1[i];
}

int visited = 0;
int subsetSum = sum / k;

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


int mask = 1 << i;
if (!(visited & mask) && ar1[i] == subsetSum) {
visited |= mask;
int currentSum = ar1[i];
for (int j = 0; j < n; ++j) {
if (!(visited & (1 << j)) && currentSum + ar1[j] <= subsetSum) {
visited |= (1 << j);
currentSum += ar1[j];
}
}
}
}

if (visited == (1 << n) - 1) {
printf("true");
} else {
printf("false");
}

return 0;
}

15. Write a C program to multiply corresponding elements of two arrays of integers

#include <stdio.h>

int main() {
int n;
printf("Enter the size of the arrays: ");
scanf("%d", &n);

C homeworks - Arrays 11
int ar1[100], ar2[100];

printf("Enter elements of the first array:\n");


for (int i = 0; i < n; ++i) {
scanf("%d", &ar1[i]);
}

printf("Enter elements of the second array:\n");


for (int i = 0; i < n; ++i) {
scanf("%d", &ar2[i]);
}

printf("Result after multiplying corresponding elements:\n");


for (int i = 0; i < n; ++i) {
printf("%d ", ar1[i] * ar2[i]);
}

return 0;
}

16. Write a C program to merge two given sorted array of integers and create a new
sorted array

#include <stdio.h>

int main() {
int n, m;
printf("Enter the size of the first array: ");
scanf("%d", &n);

int ar1[100];
printf("Enter elements of the first array (sorted):\n");
for (int i = 0; i < n; ++i) {
scanf("%d", &ar1[i]);
}

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


scanf("%d", &m);

int ar2[100];
printf("Enter elements of the second array (sorted):\n");
for (int i = 0; i < m; ++i) {
scanf("%d", &ar2[i]);
}

int merged[200];
int i = 0, j = 0, k = 0;

C homeworks - Arrays 12
while (i < n && j < m) {
if (ar1[i] < ar2[j]) {
merged[k++] = ar1[i++];
} else {
merged[k++] = ar2[j++];
}
}

while (i < n) {
merged[k++] = ar1[i++];
}

while (j < m) {
merged[k++] = ar2[j++];
}

printf("Merged sorted array:\n");


for (int l = 0; l < n + m; ++l) {
printf("%d ", merged[l]);
}

return 0;
}

17. Write a C program to calculate the median of an given unsorted array of integers

#include <stdio.h>

int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);

int ar[100];
printf("Enter elements of the array:\n");
for (int i = 0; i < n; ++i) {
scanf("%d", &ar[i]);
}
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - i - 1; ++j) {
if (ar[j] > ar[j + 1]) {
// Burada swap atiyom
int temp = ar[j];
ar[j] = ar[j + 1];
ar[j + 1] = temp;
}
}
}
double median;

C homeworks - Arrays 13
if (n % 2 == 0) {
median = (ar[n / 2 - 1] + ar[n / 2]) / 2.0;
} else {
median = ar[n / 2];
}
printf("Median of the array: %lf\n", median);

return 0;
}

18. Write a C program to find a number that appears only once in a given array of
integers, all numbers occur twice

#include <stdio.h>

int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);

int ar[100];
printf("Enter elements of the array (each number occurs twice, except one):\n");
for (int i = 0; i < n; ++i) {
scanf("%d", &ar[i]);
}

int xorResult = 0;

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


xorResult ^= ar[i];
}

printf("Number that appears only once: %d\n", xorResult);

return 0;
}

19. Write a C program to remove all occurrences of a specified value in a given array
of integers and return the new length of the array

#include <stdio.h>

int main() {
int n, rRem;
printf("Enter the size of the array: ");
scanf("%d", &n);

C homeworks - Arrays 14
int ar1[100];
printf("Enter elements of the array:\n");
for (int i = 0; i < n; ++i) {
scanf("%d", &ar1[i]);
}

printf("Enter the value to remove: ");


scanf("%d", &rRem);

int newLength = n;

for (int i = 0; i < newLength; ++i) {


if (ar1[i] == rRem) {
for (int j = i; j < newLength - 1; ++j) {
ar1[j] = ar1[j + 1];
}
--newLength;
--i; // Check the same index again as the array shifted
}
}

printf("New length of the array: %d\n", newLength);

return 0;
}

20. Write a C program to find the number of elements that is higher than the average
of given array of integers

#include <stdio.h>

int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);

int ar1[100];
printf("Enter elements of the array:\n");
for (int i = 0; i < n; ++i) {
scanf("%d", &ar1[i]);
}

int sum = 0;
for (int i = 0; i < n; ++i) {
sum += ar1[i];
}

int average = sum / n;

C homeworks - Arrays 15
int count = 0;

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


if (ar1[i] > average) {
++count;
}
}

printf("Number of elements higher than the average: %d\n", count);

return 0;
}

C homeworks - Arrays 16

You might also like