0% found this document useful (0 votes)
2 views3 pages

Array Homework Simple

The document contains several C programming examples focusing on arrays, including calculating the average marks of students, finding the maximum value in an array, implementing bubble sort, and summing student scores. Each example includes code snippets and a brief explanation of its functionality. Additionally, there is an example demonstrating how to count alphabetic characters in a string.

Uploaded by

temuxoxo
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)
2 views3 pages

Array Homework Simple

The document contains several C programming examples focusing on arrays, including calculating the average marks of students, finding the maximum value in an array, implementing bubble sort, and summing student scores. Each example includes code snippets and a brief explanation of its functionality. Additionally, there is an example demonstrating how to count alphabetic characters in a string.

Uploaded by

temuxoxo
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

Array Homework Answers

1. Average Marks of 10 Students

#include <stdio.h>

int main() {
int marks[10];
int sum = 0;
float average;

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


scanf("%d", &marks[i]);
sum += marks[i];
}

average = sum / 10.0;

printf("Average = %.2f\n", average);

return 0;
}

2. Find Maximum Value

#include <stdio.h>

int findMax(int arr[], int length) {


int max = arr[0];

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


if(arr[i] > max) {
max = arr[i];
}
}

return max;
}

3. Main Function for Maximum

#include <stdio.h>

int findMax(int arr[], int length) {


int max = arr[0];

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


if(arr[i] > max) {
max = arr[i];
}
}
return max;
}

int main() {
int arr[] = {12, 45, 7, 89, 34};

printf("Max = %d\n", findMax(arr, 5));

return 0;
}

4. Bubble Sort

#include <stdio.h>

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


int temp;

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


for(int j = 0; j < n - i - 1; j++) {
if(arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main() {
int arr[] = {64, 34, 25, 12, 22};

bubbleSort(arr, 5);

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


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

return 0;
}

5. Student Score Statistics

#include <stdio.h>

int sum(int score[][4], int row) {


int total = 0;

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


total += score[row][i];
}

return total;
}

int main() {
int score[3][4] = {
{80, 75, 90, 85},
{70, 88, 92, 78},
{95, 91, 89, 93}
};

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


printf("%d\n", sum(score, i));
}

return 0;
}

6. String in C

#include <stdio.h>

int main() {
char str[100];
int count = 0;

scanf("%s", str);

for(int i = 0; str[i] != '\0'; i++) {


if((str[i] >= 'a' && str[i] <= 'z') ||
(str[i] >= 'A' && str[i] <= 'Z')) {
count++;
}
}

printf("%d\n", count);

return 0;
}

You might also like