I’ve got a quick and I am assuming question but I have not been able to find anything online.
How to calculates the average of elements in an unsigned char array?
Or more like it, perform operations on an unsigned char?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Arithmetic operations work just fine on
unsigned char, although you may occasionally be surprised by the fact that arithmetic in C always promotes toint.In C++’s Standard Template Library,
To calculate the sum of
unsigned char arr[], you may useaccumulate(arr, arr + sizeof(arr) / sizeof(arr[0]), 0). (0 is aninthere. You may find it more appropriate to use a different type.)Without STL, this is trivially computed with a loop.
The average is the sum divided by the length (
sizeof(arr) / sizeof(arr[0])).