I started learning c++ about 3 weeks ago after 2 years of java. It seems so different but im getting there. my lecturer is a lovely guy but any time i ask a question as to why something is that way or this way. he just responds "because it is".
Theres lots of comments in the code below with a few random questions, but the main problem is im getting two build errors, one says the arraytotal has not been initialized (even though i found a value for it) and the other says an external reference in main.
would anyone mind reading the code and answering a few comments within, and maybe the overall problem im having?
#include<string>
#include<fstream>
#include<ostream>
using namespace std;
//double decimals[5] ={2,4,6,8,10};
const int arraySize = 5;
// does an arraySize have to be const always? is it so it doesnt channge after the array has been created?
//double decimals[arraySize];
/*
this array is being created in the function averageN() but why?
cant i just create it up top and reference it in?
*/
// why do you have to write the name of the function up here before you even create it?
double averageN();
int main()
{
averageN();
return 0;
}
// why does the array have to be created here?
double averageN(double decimals[arraySize])
{
double average;
double arrayTotal;
for (int i = 0; i<5;i++)
{
// fills with random numbers from 0 - 10
decimals[i] = (0+(rand()%10));
}
// find the total of all the elements in the array
for (int i = 0; i < arraySize;i++)
{
double currentElement = decimals[i];
arrayTotal = (currentElement+arrayTotal);
//arrayTotal +=decimals[i]) ;
}
// return the average
average = (arrayTotal/arraySize);
return 0.0;
}
// does an arraySize have to be const always? is it so it doesnt channge after the array has been created? Yes, it has to be const, moreover, it must be a constant expression, which means its size must be known at compile-time (not at runtime). If you want to resize arrays, then the best is to use the standard container std::vector. Or use dynamically allocated arrays if you want a fixed-size array, but the size is not known until runtime
/* this array is being created in the function averageN() but why? cant i just create it up top and reference it in? */ if you speak about decimals, then no, it is a global variable, you can use it from anywhere.
// why do you have to write the name of the function up here before you even create it? You must declare any name in C++ prior to its use. Since you call this function in main, it must be at least declared beforehand. You can also provide the definition (body) before main.
// why does the array have to be created here?
Oops, it appears that there's a big mixup in your code. As a matter of fact, you have 2 functions named averageN, one is averageN that takes no parameters, other is AveraeN taking an array of double.You never defined the first one, just declared.
Errors:
doubleTotal is not initialized. Well it is not
double arrayTotal;
change to
double arrayTotal = 0.0;
unresolved extenal in main - that's the AverageN function in main you are caling. You never wrote a body for it. You created a function that takes an array, which wasn't useful since your array is global. Just delete the array parameter from AverageN definition.
HTH
P.S. Read S. Lippmann's C++ Primer. It's the best book for beginners ther is for C++. IMO :)
const gives the compiler a clue that the item should not be changed and if the code attempts it then the compiler can flag an error.
the function name is mentioned before the actual declaration the main() function needs to reference it before the compiler has actually come to compile it (as it later on in the code file). You can move the whole function before the main() to avoid this.
double averageN(double decimals[arraySize]) is saying this function takes an array. It doesn't say that it create the array. If you look in the function, it takes the array, and adds calculated values into it (decimals[i] = (0+(rand()%10))
). This function also calculates an average over the array and returns that as a double.
So to answer your big question what's wrong - read the last point and look at the call you are making -averageN();
- can you see how this is not the correct call?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With