Array Chapter Homework Answers
1. Average Marks of 10 Students
#include <stdio.h>
int main() {
int marks[10];
int sum = 0;
float average;
printf("Enter marks of 10 students:\n");
for(int i = 0; i < 10; i++) {
scanf("%d", &marks[i]);
sum += marks[i];
}
average = sum / 10.0;
printf("Average marks = %.2f\n", average);
return 0;
}
2. Function to Find Maximum Value in Array
#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 to Call Maximum Function
#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 numbers[] = {12, 45, 7, 89, 34};
int length = 5;
int result = findMax(numbers, length);
printf("Maximum value = %d\n", result);
return 0;
}
4. Bubble Sort Program
#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 - 1 - i; j++) {
if(arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printArray(int arr[], int n) {
for(int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {64, 34, 25, 12, 22};
int n = 5;
printf("Before sorting:\n");
printArray(arr, n);
bubbleSort(arr, n);
printf("After sorting:\n");
printArray(arr, n);
return 0;
}
5. Student Score Statistics (2D Arrays)
#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("Total score of student %d = %d\n", i + 1, sum(score, i));
}
return 0;
}
6. String in C (Optional Question)
#include <stdio.h>
int main() {
char str[100];
int count = 0;
printf("Enter a string:\n");
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("Number of English letters = %d\n", count);
return 0;
}