Thread: Array problems, unfamiliar error messages

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    9

    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");
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What exactly is the problem and what are the error messages?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    9
    \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*)'

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    9
    sorry bout that forgot to put them in

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    9
    [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

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error messages
    By lyojarhhs in forum C Programming
    Replies: 5
    Last Post: 03-27-2007, 02:31 AM
  2. What does the mean of error messages?
    By Mathsniper in forum C Programming
    Replies: 1
    Last Post: 10-04-2005, 10:48 AM
  3. XP error messages
    By chris1985 in forum Windows Programming
    Replies: 2
    Last Post: 01-12-2005, 02:37 PM
  4. Help with error messages
    By chris1985 in forum Windows Programming
    Replies: 1
    Last Post: 01-11-2005, 03:39 PM
  5. problems with too many warning messages?
    By Isometric in forum C Programming
    Replies: 9
    Last Post: 11-25-2001, 01:23 AM