Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,60 @@
void inputNumbers(int arr[], int *n)
{
printf("How many numbers (max %d)? ", MAX_NUMBERS);
// Note: It's good practice to check the return value of scanf for robustness,
// but keeping it simple for this fix.
scanf("%d", n);
if (*n > MAX_NUMBERS || *n < 1)
{
printf("Invalid count!\n");
printf("Invalid count! Please enter a number between 1 and %d.\n", MAX_NUMBERS);
*n = 0;
return;
}
printf("Enter %d numbers:\n", *n);
for (int i = 0; i < *n; i++)
{
// Another good practice: check scanf return value here too.
scanf("%d", &arr[i]);
}
}

/**
* Fix 1: The loop condition was 'i > n', which is false initially (i=0, n>0),
* so the loop never executed. Changed to 'i < n'.
*/
int sumArray(int arr[], int n)
{
int sum = 0;
for (int i = 0; i > n; i++)
for (int i = 0; i < n; i++)
{
sum += arr[i];
}
return sum;
}

/**
* Fix 2: The division 'sum / n' performs integer division since both 'sum' (int)
* and 'n' (int) are integers. To get a floating-point average, one of them
* must be explicitly cast to a float before division.
*/
float averageArray(int arr[], int n)
{
// Re-calculating sum here for simplicity, but calling sumArray is also fine.
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += arr[i];
}
return sum / n;
// Cast 'sum' to float to ensure floating-point division
return (float)sum / n;
}

int findMin(int arr[], int n)
{
// Add check for n > 0 to prevent accessing arr[0] on an empty array,
// although inputNumbers handles n=0 and main() checks for it.
if (n <= 0) return 0; // Or some error indicator

int min = arr[0];
for (int i = 1; i < n; i++)
{
Expand All @@ -55,6 +73,9 @@ int findMin(int arr[], int n)

int findMax(int arr[], int n)
{
// Add check for n > 0
if (n <= 0) return 0; // Or some error indicator

int max = arr[0];
for (int i = 1; i < n; i++)
{
Expand All @@ -74,9 +95,11 @@ int main()
inputNumbers(numbers, &n);
if (n == 0)
{
printf("No valid numbers entered. Exiting.\n");
return 1;
}

// Now uses the corrected sumArray and averageArray functions
printf("Sum: %d\n", sumArray(numbers, n));
printf("Average: %.2f\n", averageArray(numbers, n));
printf("Minimum: %d\n", findMin(numbers, n));
Expand Down