-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Create average.c #1298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create average.c #1298
Conversation
math/average.c
Outdated
result += avg[i]; | ||
} | ||
|
||
return result / size; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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[]) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this 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}; |
There was a problem hiding this comment.
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[]) { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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}; |
There was a problem hiding this comment.
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}; |
There was a problem hiding this comment.
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.
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 |
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 |
I am so excited to having you! Thank you so much! |
adding an average function with a test case
Description of Change
Checklist
Notes: