Like Tree2Likes
  • 1 Post By laserlight
  • 1 Post By cyberfish

Assignment working but have questions

This is a discussion on Assignment working but have questions within the C++ Programming forums, part of the General Programming Boards category; Hello, everyone my brother was given an assignment to do in C, I know C++ and C# so I write ...

  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
    21,625
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,625
    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.
    Elysia likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,181
    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).
    Elysia likes this.

  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
    21,625
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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...)
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  12. #12
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,181
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21