Skip to content

Instantly share code, notes, and snippets.

@Lulzx
Created December 21, 2020 09:44
Show Gist options
  • Save Lulzx/98769b3680816447b3cf4dff1afe3089 to your computer and use it in GitHub Desktop.
Save Lulzx/98769b3680816447b3cf4dff1afe3089 to your computer and use it in GitHub Desktop.
c code
#include <stdio.h>
int findMin(int arr[], int n)
{
int min = arr[0];
for (int i = 1; i < n; ++i) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
int findMax(int arr[], int n)
{
int max = arr[0];
for (int i = 1; i < n; ++i) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
int sumArray(int arr[], int n)
{
int sum = 0;
for (int i = 0; i < n; ++i) {
sum += arr[i];
}
return sum;
}
float findAverage(int arr[], int n)
{
float average = sumArray(arr, n) / n;
return average;
}
int main(void)
{
int lower_bound = 0; // minimum
int upper_bound = 10; // maximum
int min = 0, max = 0, n; // initialize variables
printf("how many numbers to enter? ");
scanf("%d", &n);
int arr[n]; // array of inputs stored
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
while (x < lower_bound || x > upper_bound) {
if (x < lower_bound) {
printf("less than lower bound!\n");
}
else if (x > upper_bound) {
printf("more than upper bound!\n");
}
printf("enter correct number %d: ", i);
scanf("%d", &x);
}
arr[i - 1] = x;
}
printf("\nMinimum: %d\n", findMin(arr, n));
printf("Maximum: %d\n", findMax(arr, n));
printf("Average: %.2f\n", findAverage(arr, n));
printf("Sum of array: %d\n", sumArray(arr, n));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment