C++: getting errors with some code -
i kind of stuck code im doing. have in comments.
//write function computes average value of array of floating-point data: //double average(double* a, int size) //in function, use pointer variable, not integer index, traverse array //elements. #include <iostream> using namespace std; double average(double* a, int size) { double total = 0; double* p = a; // p starts @ beginning of array (int = 0; < size; i++) { total = total + *p; // add value p points p++; // advance p next array element } return total / size; }
to start not running. , 2ndly, going doing problem correctly? tried following book go through elements , average out after... have strong feeling im missing something.
sorry if seems obvious of guys. still pretty new of , teacher doesn't exactly... doesn't teach coding aspect of c++. read 5 out of 200+ slides , hand tracing(not pseudo code) , throws wolves randomly picking coding assignment. way teaches if know how code, of , of us(like me) seeing first time.
she hasnt taught how use compiler, learning of ourselves. arg, sorry went on rant there. anyway can this?
your function correct.
here is, running, , resulting in correct output.
in case problem not aware (though seems unlikely), had add main
function , provide data function work on:
int main() { double array[5] = {1,2,3,4,5}; std::cout << average(array, 5); }
but that's all had do.
Comments
Post a Comment