# Assignment working but have questions

• 11-02-2011
Dynamic
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

Quote:

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.
• 11-02-2011
laserlight
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.
• 11-02-2011
Dynamic
Thanks for that got it working :D

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; }```
• 11-02-2011
laserlight
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.
• 11-02-2011
cyberfish
Quote:

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).
• 11-03-2011
Dynamic
Quote:

Originally Posted by laserlight
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 :D

: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
• 11-03-2011
laserlight
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.
• 11-03-2011
Dynamic
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";`
• 11-03-2011
manasij7479
Quote:

Originally Posted by Dynamic
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...)
• 11-04-2011
Elysia
Quote:

Originally Posted by manasij7479
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.
• 11-04-2011
manasij7479
Quote:

Originally Posted by Elysia
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.
• 11-04-2011
cyberfish
Quote:

Originally Posted by Dynamic
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.
• 11-04-2011
Elysia
Quote:

Originally Posted by manasij7479
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.
• 11-04-2011
manasij7479
Quote:

Originally Posted by Elysia
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.