Thread: Assignment working but have questions

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

    Assignment working but have questions

    Hello, everyone my brother was given an assignment to do in C, I know C++ and C# so I write it out in C++ to get a better understanding of what the brief wants us to do.

    I have the program working, but with modifications to the brief.

    Brief

    Write a program that calculates the Root-Mean-Square (RMS) of a
    sequence of numbers whose length and content are determined by the
    user. The RMS calculation should be done in a function of prototype
    double RMS(double *x, int N), where N is the number of elements
    in the array x. Demonstrate its use in a main function which prompts
    the user for the length of the sequence, and then the values of this
    sequence (e.g. if the user gives 3 as a sequence length, he/she should
    thereafter be asked for 3 values).

    This is what I wrote
    Code:
    #include <iostream>
    #include <math.h>
    
    double RMS(double x, int N);
    using namespace std;
    
    int main()
    {
        int arraysize(0);
        double total(0);
        double content(0);
       cout << "How many number's should there be." << endl;
       cin >> arraysize;
     double array[arraysize];
    
    
       for(int i = 0; i < arraysize; ++i)
       {
        cout << "Enter number " << i << endl;
        cin >> content;
        array[i] += content;
        total += content;
    
       }
    
        cout << RMS(total, arraysize);
      return 0;
    }
    
    double RMS(double x, int N)
    {
     double result(0);
     result = x * x / N;
     sqrt(result);
     return result;
    
    }
    What Im not sure of is the prototype in the brief.
    double RMS(double *x, int N)
    Is double *x a pointer or does he just mean array because they are similar but not the same.

    I did it all from memory so go easy on me if there is some noobish mistakes the last time I used C++ was a long time ago.
    Last edited by Dynamic; 11-02-2011 at 03:58 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Dynamic
    What Im not sure of is the prototype in the brief.
    double RMS(double *x, int N)
    Is double *x a pointer or does he just mean array because they are similar but not the same.
    It means that the parameter x is a pointer to double. However, you could pass an array of doubles as the first argument and it will be converted to a pointer to its first element.
    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
    10
    Thanks for that got it working

    I always hated pointers but they are so important.

    Code:
    #include <iostream>
    #include <math.h>
    
    double RMS(double *x, int N);
    using namespace std;
    
    int main()
    {
        double * p;
        int arraysize(0);
        double total(0);
       cout << "How many number's should there be." << endl;
       cin >> arraysize;
     double array[arraysize];
    
    
       for(int i = 0; i < arraysize; ++i)
       {
        cout << "Enter number: ";
        cin >> array[i];
        total += array[i];
    
       }
       p = &total;
       total = *p;
    
    cout << RMS(p, arraysize);
      return 0;
    
    }
    
    double RMS(double *x, int N)
    {
     double result(0);
     result = *x * *x / N;
     sqrt(result);
     return result;
    
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This declares a variable length array:
    Code:
    double array[arraysize];
    Variable length arrays are not part of standard C++.

    An appropriate solution here is to use a std::vector<double> instead.

    Also, where you use the using namespace std; directive, avoid naming variables array because there is a standard library component named array.
    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

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Also, where you use the using namespace std; directive, avoid naming variables array because there is a standard library component named array.
    Which is a perfect example of why you really shouldn't do "using namespace std;". C++ standard library is huge. If you import all that crap into your global namespace, you'll run into name collisions sooner or later (very soon in this case).

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    10
    Quote Originally Posted by laserlight View Post
    This declares a variable length array:
    Code:
    double array[arraysize];
    Variable length arrays are not part of standard C++.

    An appropriate solution here is to use a std::vector<double> instead.

    Also, where you use the using namespace std; directive, avoid naming variables array because there is a standard library component named array.
    Thank you both of you, my brothers assignments have motivated me to go back to learning C++, I know it's a bad habbit I will stop

    :O Just noticed I forgot to Initalize the pointer to Nullptr! Just out of interest has anyone here actually overwrote a part of memory by not pointing to NULL
    Last edited by Dynamic; 11-03-2011 at 11:28 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Dynamic
    Just noticed I forgot to Initalize the pointer to Nullptr!
    Declare variables near first use instead of initialising them long before first use with a default value that will get overwritten anyway.
    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

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    10
    Your last point about not using namespace std, so would doing this be more practical.
    Code:
    using std::cout;
    using std::cin;
    using std::string;
    Or should I just do it the long way like say
    Code:
    std::cout << "blah blah";

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Dynamic View Post
    Your last point about not using namespace std, so would doing this be more practical.
    Code:
    using std::cout;
    using std::cin;
    using std::string;
    Or should I just do it the long way like say
    Code:
    std::cout << "blah blah";
    Well, I declare everything written by me in a separate namespace ...and use namespace std globally.
    (Because I use the standard library *much* more frequently than totally homebrew code...)

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by manasij7479 View Post
    Well, I declare everything written by me in a separate namespace ...and use namespace std globally.
    (Because I use the standard library *much* more frequently than totally homebrew code...)
    This is still not very good. If you a function or variable named "array", it will hide the one from the standard library.

    Using namespace statements are nice, but they have their drawbacks. Simple using statements have the same drawbacks, but they affect lesser scopes.
    There is nothing wrong with using them, as long as you know what they do and what problems they might cause. It is up to you to decide whether that is acceptable or not.
    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.

  11. #11
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elysia View Post
    This is still not very good. If you a function or variable named "array", it will hide the one from the standard library.
    Why ?
    If I have my array inside namespace foo,
    I can use my array as foo::array outside it, and when I need std::array inside namespace foo, I would specify it as such.

  12. #12
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Quote Originally Posted by Dynamic View Post
    Your last point about not using namespace std, so would doing this be more practical.
    Code:
    using std::cout;
    using std::cin;
    using std::string;
    ...
    That is my personal preference.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by manasij7479 View Post
    Why ?
    If I have my array inside namespace foo,
    I can use my array as foo::array outside it, and when I need std::array inside namespace foo, I would specify it as such.
    Imagine that someday, in your code, you want to use std::array. Since you typically use "using namespace std," you simply type "array."
    But since you are in the namespace foo, you get foo::array instead, and if the classes are not dissimilar, it might just compile and you will get incorrect code!
    Hence why it might not be so good.
    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.

  14. #14
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elysia View Post
    Imagine that someday, in your code, you want to use std::array. Since you typically use "using namespace std," you simply type "array."
    But since you are in the namespace foo, you get foo::array instead, and if the classes are not dissimilar, it might just compile and you will get incorrect code!
    Hence why it might not be so good.
    I don't use namespace std in the implementation files related to stuff within namespace foo.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ItemArray assignment not working as expected
    By Dragoon_42 in forum C# Programming
    Replies: 1
    Last Post: 01-28-2010, 03:11 PM
  2. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  3. Replies: 9
    Last Post: 03-30-2009, 04:09 AM
  4. Pointer assignment not working
    By coderCPP1981 in forum C Programming
    Replies: 6
    Last Post: 06-20-2008, 06:59 AM
  5. three questions about hw assignment
    By jlmac2001 in forum C++ Programming
    Replies: 8
    Last Post: 09-11-2003, 01:55 PM