October 4th, 2002, 12:33 AM
Calculate Average Prob
Hi, I am making a program that will find the average of what ever you enter, but i am having trouble with a part in my program. My program asks the user how many numbers they are going to enter. Then I store that number in unsigned int choice. Now, I have another function that I need to use the value in int choice. I have tried Making int choice global, and have tried pointers (not sure if i was doing it right though..)
I am getting errors such as
c:\Documents and Settings\default\My Documents\Visual Studio Projects\Average Finder\Average.cpp(54): error C2057: expected constant expression
and other errors due to it cant read the choice value. Here is some of my code to better understand my situation..
Right now i have choice as global variable.
If someone could help me it would be greatly appreciated
using namespace std;
unsigned int choice = 1; //Is used to hold the numbers wanted to average
double* nums = NULL; //This will hold the average array
cout << endl << endl << "\t\t\tAverage Finder v1\n\n\n"; //Prints average findeer
cout << "How many numbers do you want to find the average of?: ";
cin >> choice;
int avg[choice]; // im trying to use the value in choice to set the arrays size
October 4th, 2002, 12:23 PM
You should use the new keyword instead of declaring:
Try rewriting the above line as:
int *avg = new int[choice];
Also don't forget to do:
delete  avg;
at the end of your average function to free up the memory! / Hope this helps!
October 4th, 2002, 05:28 PM
Thanks a lot for your help!
October 4th, 2002, 08:00 PM
Ok, i have one more question about my program..
Alright I have all my numbers that are being entered go to an array.
Is there a way to compare the array with the other values in the same array and find which one is the smallest and which one is second smallest and so on.. and then re-assign the least to index 0 second least to index 1 and so forth?
Thanks to any help I can get
October 4th, 2002, 08:08 PM
You can search for bubble sort or bubblesort in google if you like, for the simplest of sorting algorithms. If you want to look like a seasoned professional, you'd probably want to use a quicksort algorithm instead (which is *quite a bit* faster than a bubblesort). There's already an ANSI function defined, to help with quicksorting any array and it's called qsort(). Check out the documentation in MSDN for it http://msdn.microsoft.com/library/de..._crt_qsort.asp . You'll also find some sample code for using qsort in that link. Hope this helps!
October 4th, 2002, 08:25 PM
Wow, Thanks for the fast reply scorpian. I really appreciate your help.
Alright, I get the Bubble sort algorithm, but the qsort is kind of confusing. Sorry if i sound newb coz i kinda am But for the qsort, there is ...
void main( int argc, char **argv )
and then a lil later in the code there is..
what does that do?
October 4th, 2002, 10:13 PM
>> Sorry if i sound newb coz i kinda am
We all have to start somewhere
Basically, if you run the program from the command line, argc would contain the number of arguments that were passed on the command line and argv contains the actual arguments. For example, if you ran the program from the command prompt like this:
c:\>qsort foo bar baz quux
then argc would be set to 5 because there are 5 arguments on the command line(including the program name itself). Also, the argv array would be filled up as follows - argv = qsort, argv = foo, argv = bar etc. Now the purpose of the program is to sort the command line arguments (excluding the program name itself). When the program first runs, argv points to argv (i.e.) the program name itself and we don't want to include that to be sorted. So, we need to increment it by 1 to point it to the first argument (i.e. foo), which is what argv++ does. Since, we're omitting the first member of the array, we also need to decrease the array count by 1, which is why we do an argc--. You don't really need to do an argc--, instead you could call qsort like this:
qsort( (void *)argv, (size_t)argc - 1, sizeof( char * ), compare );
Since you need to let qsort know how large your array is and that's what argc contains, you can do it either way.
By the way, all of this argv++ business is pointer arithmetic. If you don't understand pointers the first time around, relax. Just keep working with them and you'll get the hang of it eventually . If you feel more comfortable with the bubble sort method, then just use it for now and come back to quicksort later. I just let you know about quicksort since bubble sort is rather inefficient for larger number of elements.
Hope this helps!
October 4th, 2002, 11:54 PM
Ok thanks a lot It did help