|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
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. Code:
#include <iostream>
#include <stdio.h>
#include <windows.h>
using namespace std;
int average();
unsigned int choice = 1; //Is used to hold the numbers wanted to average
int main()
{
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;
.........
average();
return 0;
}
int average()
{
int avg[choice]; // im trying to use the value in choice to set the arrays size
.......
}
If someone could help me it would be greatly appreciated Thanks! -Optix |
|
#2
|
||||
|
||||
|
You should use the new keyword instead of declaring:
int avg[choice]; 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! |
|
#3
|
|||
|
|||
|
Ok
Thanks a lot for your help! ![]() |
|
#4
|
|||
|
|||
|
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 -Optix |
|
#5
|
||||
|
||||
|
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/d.../_crt_qsort.asp . You'll also find some sample code for using qsort in that link. Hope this helps!
![]() |
|
#6
|
|||
|
|||
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.. argv++; argc--; what does that do? Thanks again, -Optix |
|
#7
|
||||
|
||||
|
>> Sorry if i sound newb coz i kinda am
We all have to start somewhere ![]() >>argv++; >>argc--; 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[0] = qsort, argv[1] = foo, argv[2] = 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[0] (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! ![]() |
|
#8
|
|||
|
|||
|
Ok thanks a lot
It did help ![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Calculate Average Prob |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|