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

Sum of Array Elements

Uploaded by

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

Sum of Array Elements

Uploaded by

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

Sum of Array Elements Reverse an Array

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


int main() int main() {
int arr[] = {1, 2, 3, 4, 5};
{
int n = 5;
int arr[] = {1, 2, 3, 4, 5}; printf("Original array: ");
int sum = 0; for(int i = 0; i < n; i++) {
for(int i = 0; i < 5; i++) { printf("%d ", arr[i]);
sum += arr[i]; }
} printf("\n");
printf("Sum of array elements: %d\n", sum); printf("Reversed array: ");
for(int i = n - 1; i >= 0; i--) {
return 0;
printf("%d ", arr[i]);
} }
printf("\n");
Find Maximum Element return 0;
#include <stdio.h> }
int main() {
int arr[] = {10, 23, 5, 8, 45}; Calculate Average
int max = arr[0]; #include <stdio.h>
for(int i = 1; i < 5; i++) { int main() {
if(arr[i] > max) { int arr[] = {10, 20, 30, 40, 50};
max = arr[i]; int n = 5;
} float sum = 0;
} for(int i = 0; i < n; i++) {
printf("Maximum element: %d\n", max); sum += arr[i];
return 0; }
} float average = sum / n;
printf("Average: %.2f\n", average);
Linear Search return 0;
#include <stdio.h> }
int main() {
int arr[] = {10, 23, 5, 8, 45}; Copy Array
int target = 8; #include <stdio.h>
int found = 0; int main() {
for(int i = 0; i < 5; i++) { int arr1[] = {1, 2, 3, 4, 5};
if(arr[i] == target) { int arr2[5];
found = 1; for(int i = 0; i < 5; i++) {
break; arr2[i] = arr1[i];
} }
} printf("Copied array: ");
if(found) { for(int i = 0; i < 5; i++) {
printf("Element found at index\n"); printf("%d ", arr2[i]);
} else { }
printf("Element not found\n"); printf("\n");
} return 0;
return 0; }
}
Merge Arrays arr[j + 1] = temp;
#include <stdio.h> }
int main() { }
int arr1[] = {1, 2, 3}; }
int arr2[] = {4, 5, 6}; printf("Sorted array: ");
int merged[6]; for(int i = 0; i < n; i++) {
for(int i = 0; i < 3; i++) { printf("%d ", arr[i]);
merged[i] = arr1[i]; }
} printf("\n");
for(int i = 0; i < 3; i++) { return 0;
merged[i + 3] = arr2[i]; }
}
printf("Merged array: "); Transpose Matrix
for(int i = 0; i < 6; i++) { #include <stdio.h>
printf("%d ", merged[i]); int main() {
} int matrix[3][3] = {
printf("\n"); {1, 2, 3},
return 0; {4, 5, 6},
} {7, 8, 9}
};
Check Palindrome int transpose[3][3];
#include <stdio.h> for(int i = 0; i < 3; i++) {
int main() { for(int j = 0; j < 3; j++) {
int arr[] = {1, 2, 3, 2, 1}; transpose[j][i] = matrix[i][j];
int n = 5; }
int isPalindrome = 1; }
for(int i = 0; i < n / 2; i++) { printf("Original matrix:\n");
if(arr[i] != arr[n - 1 - i]) { for(int i = 0; i < 3; i++) {
isPalindrome = 0; for(int j = 0; j < 3; j++) {
break; printf("%d ", matrix[i][j]);
} }
} printf("\n");
if(isPalindrome) { }
printf("Array is a palindrome\n"); printf("Transpose matrix:\n");
} else { for(int i = 0; i < 3; i++) {
printf("Array is not a palindrome\n"); for(int j = 0; j < 3; j++) {
} printf("%d ", transpose[i][j]);
return 0; }
} printf("\n");
}
Sort Array return 0;
#include <stdio.h> }
int main() {
int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5};
int n = 9;
// Bubble sort
for(int i = 0; i < n - 1; i++) {
for(int j = 0; j < n - i - 1; j++) {
if(arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy