c++ - Error when passing array to function -
i'm new c++ , writing program supposed following:
- fill array of integers based on user-defined constant.
- pass array in step # 1 function computes mean of integers in array.
- pass array in step # 2 function computes standard deviation of integers in array.
here's code:
#include "stdafx.h" #include <iostream> #include <cmath> using namespace std; const int size_of_array = 100; int custarray[]; void fillarray(int size_of_array); double standarddeviation(double[], int); double mean(double[], int); double arithmeticaverage; int _tmain(int argc, _tchar* argv[]) { int i; fillarray(size_of_array); cout << "\n"; cout << "the mean is: " << mean(custarray[], i); cout << endl; cout << "the standard deviation is: " << standarddeviation(custarray[], i); return 0; } void fillarray(int size_of_array) { int = 0; custarray[0] = { 1 }; (int = 0; < size_of_array; i++) custarray[i] = + 1; return; } double mean(double custarray[], int size_of_array) { double sumofelements = 0; int i; (i = 0; < size_of_array; i++) { sumofelements += custarray[i]; } arithmeticaverage = sumofelements / i; return (arithmeticaverage); } double standarddeviation(double custarray[], int size_of_array) { double standarddeviation; double tempsum = 0; (int = 0; < size_of_array; i++) { tempsum += pow((custarray[i] - arithmeticaverage), 2); } standarddeviation = sqrt(tempsum / (size_of_array)); return (standarddeviation); }
i'm getting error in following lines of code:
cout << "the mean is: " << mean(custarray[], i);
and
cout << "the standard deviation is: " << standarddeviation(custarray[], i);
that reads: "syntax error: ']'
why?
there's lot of problems in program. however, did fix them , compiles without errors.
first of all, dont pass arrays functuin
[]
after name; type variable name.second,
mean()
,standarddeviation()
functions both had array declareddouble
.third, passed variable
i
main()
not initialized. think meant passsize_of_array
.- fourth, need create main function this:
int main()
. original program hadint _tmain(int argc, _tchar* argv[])
microsoft specific standard.
#include <iostream> #include <cmath> using namespace std; const int size_of_array = 100; int custarray[size_of_array]; void fillarray(int size_of_array); double standarddeviation(int[], int); double mean(int[], int); double arithmeticaverage; int main() { //int i; no need fillarray(size_of_array); cout << "\n"; cout << "the mean is: " << mean(custarray, size_of_array); cout << endl; cout << "the standard deviation is: " << standarddeviation(custarray, size_of_array); return 0; } void fillarray(int size_of_array) { int = 0; custarray[0] = { 1 }; (int = 0; < size_of_array; i++) custarray[i] = + 1; return; } double mean(int custarray[], int size_of_array) { double sumofelements = 0; int i; (i = 0; < size_of_array; i++) { sumofelements += custarray[i]; } arithmeticaverage = sumofelements / i; return (arithmeticaverage); } double standarddeviation(int custarray[], int size_of_array) { double standarddeviation; double tempsum = 0; (int = 0; < size_of_array; i++) { tempsum += pow((custarray[i] - arithmeticaverage), 2); } standarddeviation = sqrt(tempsum / (size_of_array)); return (standarddeviation); }
Comments
Post a Comment