# Array problems, unfamiliar error messages

• 02-19-2012
Trckster
Array problems, unfamiliar error messages
This is basically my assignment, I have most of it finished but I am new to programming so i'm not really sure what some of the error messages that are coming out mean. any help would be greatly appreciated

The simple algorithm for the program is as follows:
1. Ask user to input numbers and the numbers must be saved in an array.
2. Display the numbers
3. Calculate the sum, mean, and standard deviation of the numbers.
4. Display them
5. Display the differences between the numbers and the mean.
1. Finish the basic requirements in main.
2. Define a function to display numbers in the array.
3. Define a function to calculate the sum of the numbers.
4. Define a function to calculate the sum of the squared numbers.
5. Define a function to calculate the mean of the numbers.
6. Define a function to calculate the standard deviation.
Define a function to display the differences between the numbers and the mean.

Code:

``` #include<math.h>#include<iostream> #include<array> #define MAX_ITEM 5 using namespace std; //Display numbers in the array void display_array(double ar[]) {     for(int i=0; i<MAX_ITEM; i++){             cout << ar[i] << " ";             } } //Calculate the sum of the numbers double cal_sum (double ar[]) {       double sum = 0;         for(int i=0; i< MAX_ITEM; i++){           sum+=ar[i];           }       return sum; } //Calculate the sum of the squared numbers double cal_sum_sqrt (double ar[]) {       double sum_sqrt = 0;       for ( int i = 0; i < MAX_ITEM; i++){                 sum_sqrt+=ar[i]*ar[i];                 }       return sum_sqrt; } //Calculate the mean of the numbers double cal_mean (double sum) {       double mean = 0;       mean = sum/MAX_ITEM;       return mean; } //Calculate the standard deviation of the numbers double cal_st_dev (double mean, double sum_sqrt) {       double st_dev = 0;       st_dev = sqrt(sum_sqrt/MAX_ITEM-mean*mean);       return st_dev; } main() {       double x [MAX_ITEM], sum, sum_sqrt, mean, standard_deviation, square;       for (int i=0; i<MAX_ITEM; i++)       {           cout << "Enter a number " << endl;           cin >> x[i];       }       cout << "The numbers are ";       for (int i=0; i<MAX_ITEM; i++)       {           cout << x[i] << " ";       }       square = sum_sqrt - mean;       sum = cal_sum(i);       sum_sqrt = cal_sum_sqrt(i);       mean = cal_mean(sum);       standard_deviation = cal_st_dev(square, mean);             cout << endl << "Sum of numbers " << sum << endl;       cout << endl << "sum sqared " << sum_sqrt << endl;       cout << endl <<  "Mean of numbers " << mean << endl;       cout << endl << "Standard deviation of numbers " << standard_deviation << endl;             system("pause"); }```
• 02-19-2012
laserlight
What exactly is the problem and what are the error messages?
• 02-19-2012
Trckster
\Data Structures\Assignment2.cpp F:\Data Structures\F array: No such file or directory.
F:\Data Structures\Assignment2.cpp In function `int main()':
67 F:\Data Structures\Assignment2.cpp name lookup of `i' changed for new ISO `for' scoping
62 F:\Data Structures\Assignment2.cpp using obsolete binding at `i'
67 F:\Data Structures\Assignment2.cpp invalid conversion from `int' to `double*'
67 F:\Data Structures\Assignment2.cpp initializing argument 1 of `double cal_sum(double*)'
68 F:\Data Structures\Assignment2.cpp invalid conversion from `int' to `double*'
68 F:\Data Structures\Assignment2.cpp initializing argument 1 of `double cal_sum_sqrt(double*)'
• 02-19-2012
Trckster
sorry bout that forgot to put them in
• 02-19-2012
Elysia
Your errors do not add up to the line numbers in the code. In the future, please post your real code without post editing.
Some errors I notice:
- You are trying to reference i outside the for loop. This is an error because you've declared "i" to online live inside the loop.
- You are trying to pass "i" to functions expect a pointer to double (yes, double[] means double*). You should probably pass your real array.

Some suggestions:
- Don't use #define. Use const type for constants.
- Use std::array or std::vector, not C arrays.
• 02-19-2012
Trckster
[QUOTE=Elysia;1090124]
- You are trying to reference i outside the for loop. This is an error because you've declared "i" to online live inside the loop.
- You are trying to pass "i" to functions expect a pointer to double (yes, double[] means double*). You should probably pass your real array.

what do you mean to online live inside the loop? i'm sorry I just started C++ programming and am not familiar with these terms, i'm actually pretty new to programming
• 02-19-2012
Elysia
I assume you are familiar with scope and global/local variables.
Local variables only live inside the function it is declared inside because it is the scope of the variable.
Likewise, "i" is declared inside the for loop, hence its lifetime is only as far as the for loop goes. Once the for loop ends, the variable is destroyed. You'll have to declare it outside the for loop if you want it to survive afterwards.