Skip to content

Conversation

lukasb1b
Copy link

adding an average function with a test case

Description of Change

Checklist

  • Added description of change
  • Added file name matches File name guidelines
  • Added tests and example, test must pass
  • Relevant documentation/comments is changed or added
  • PR title follows semantic commit guidelines
  • Search previous suggestions before making a new one, as yours may be a duplicate.
  • I acknowledge that all my contributions will be made under the project's license.

Notes:

math/average.c Outdated
result += avg[i];
}

return result / size;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if the size is zero?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets do something like if (size== 0) {
perror("empty array");
}

math/average.c Outdated

//average of an array

double average(int avg[]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why avg? is it the array of avgs? Maybe values? What do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you explain this please

Copy link
Contributor

@rbevin777 rbevin777 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good module, also don't be disheartened by the number of comments either! 😁

math/average.c Outdated
}

int main() {
int test[] = {1,2,3,4,5};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have a look at the contribution guidelines, there needs to be a seperate function to run the test. Have a look at strong_number.c under the math directory for an example of that.

math/average.c Outdated

//average of an array

double average(int avg[]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to add the function header. This should look something like this

/**
 * @brief    This is a brief description of what the function does
 * @param param_name - This is a brief description of the parameter
 * @return return_name - Biref description of the return value itself.
 */

math/average.c Outdated
result += avg[i];
}

return result / size;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be useful to declare a variable like this so that if you or someone else needs to debug the code it is eaiser to trace.

int avg_val = result / size;
return avg_val;

math/average.c Outdated
}

int main() {
int test[] = {1,2,3,4,5};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could add a few different tests to ensure the robustness, so for example using all negative numbers, a mix of negative and positive, you could go for large numbers close to the maximum size of an int and see what happens there (most likely an overflow and the wrong result) which is what you want to see from tests anyway. Play about with testing. It's great at highlighting the faults.

math/average.c Outdated
}

int main() {
int test[] = {1,2,3,4,5};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another comment in regards to testing, there is a common testing documentation format called gherkin The basics of it are

// Given we have an array fo values

// When we try to work out the average value of the array

// Then we should get the expected result

This is a simple example but simple is good.

@lukasb1b
Copy link
Author

I changed all of your requests exept of the last one and the last test is not passing but i dont know why because it prints15.000000-15.000000 and I think that should be 0 any fixes

@lukasb1b
Copy link
Author

I made a workaround at the tests by setting the last value 0 but otherwise i don't think there is a solution to calulate the array length. I just can think of creating a parameter for the length

@Adlinke
Copy link

Adlinke commented Jun 6, 2024

I am so excited to having you! Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants