Count positive and negative values in an array of 10 integers.
Arrays, traversal, counters, and comparisons.
An array stores multiple values in a single variable name. We read 10 integers into an array and then loop through it. For each element:
```c
#include
int main() { int arr[10]; int i, positive = 0, negative = 0;
printf("Enter 10 integers:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < 10; i++) {
if (arr[i] > 0) {
positive++;
} else if (arr[i] < 0) {
negative++;
}
}
printf("Positive count = %d\n", positive);
printf("Negative count = %d\n", negative);
return 0; } ``` ### Sample run ```text Input: 5 9 -4 -10 64 -7 13 -58 94 27 Output: Positive count = 6 Negative count = 4 ```
Read an array and print its elements in reverse order.
Array indexing and reverse traversal.
We store the numbers in an array, then loop from the last index to the first index. Each element is printed in the reverse order. This is done using the index i from n - 1 down to 0.
#include <stdio.h>
int main() {
int arr[10], n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Reversed array: ");
for (i = n - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Input: 1 2 3 4
Output: 4 3 2 1
Calculate the sum of all array elements.
Arrays, traversal, and accumulators.
We store numbers in an array and use a loop to visit each element. A variable called sum starts at 0 and adds each array value to it. After the loop, we print the total.
#include <stdio.h>
int main() {
int arr[10], n, i, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < n; i++) {
sum += arr[i];
}
printf("Sum = %d\n", sum);
return 0;
}
Input: 1 2 3
Output: 6
Calculate the average of the array elements.
Array traversal, sums, and floating-point division.
First, we compute the total sum of all array elements. Then we divide the sum by the number of elements. Since division of integers can lose decimals, we use a floating-point variable and cast or use float for the average.
#include <stdio.h>
int main() {
int arr[10], n, i;
float sum = 0, average;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < n; i++) {
sum += arr[i];
}
average = sum / n;
printf("Average = %.2f\n", average);
return 0;
}
Input: 2 4 6
Output: 4.00