#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
void averageAndCountEven(int arr[], int len, double *average, int *count);
void app_main() {
// Declaring and initializing an integer array
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Calculating the length of the array
int len = sizeof(arr) / sizeof(arr[0]); //Division was reversed. (JI)
// Declaring average and count variables
double average;
int count;
/* Calling averageAndCountEven(), passing the 'arr' array, length of the array'len',
references to the variables 'average' and 'count', and printing the results */
averageAndCountEven(arr, len, &average, &count); //There weren't any parameters being passed. Average and count have a dereference because the function has a reference in the parameters of those variables. (JI)
printf("The average of array elements is %.2f\n", average); //Format specifier was an integer instead of double. Added %.2 so that the number is limited to 2 decimal spots. (JI)
printf("The count of even numbers in the array is %d\n", count);
//Removed return 0 because main function is void. (JI)
}
/************************
* Function - averageAndCountEven
* Description - calculates the average and count of even numbers in an integer array
* Parameters - arr[] - an integer array
* len - length of the array arr[] (number of elements in the array)
* average - a pointer to store the average
* count - a pointer to store the count
* Return type - none
************************/
void averageAndCountEven(int arr[], int len, double *average, int *count) { //Average and count variable need to match the function declaration. (JI)
//initializing the pointers to 0
*average = 0;
*count = 0;
//loop through the arr[] using 'len' variable as the limit of the integer 'i'
for (int i = 0; i < len; i++) { //For-loop was incomplete. (JI)
//calculating the total sum and store it in the pointer 'average'
*average += arr[i];
if (arr[i] % 2 == 0) {
(*count)++;
}
}
//calculate the average and store it in the pointer 'average'
*average /= len; //Missing the divisor. (JI)
}
Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1