C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 4th, 2002, 12:33 AM
Optix Optix is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 36 Optix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 11
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

Reply With Quote
  #2  
Old October 4th, 2002, 12:23 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,385 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 30 m 25 sec
Reputation Power: 4080
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!

Reply With Quote
  #3  
Old October 4th, 2002, 05:28 PM
Optix Optix is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 36 Optix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 11
Ok

Thanks a lot for your help!

Reply With Quote
  #4  
Old October 4th, 2002, 08:00 PM
Optix Optix is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 36 Optix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 11
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

Reply With Quote
  #5  
Old October 4th, 2002, 08:08 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,385 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 30 m 25 sec
Reputation Power: 4080
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!

Reply With Quote
  #6  
Old October 4th, 2002, 08:25 PM
Optix Optix is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 36 Optix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 11
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

Reply With Quote
  #7  
Old October 4th, 2002, 10:13 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,385 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 30 m 25 sec
Reputation Power: 4080
>> 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!

Reply With Quote
  #8  
Old October 4th, 2002, 11:54 PM
Optix Optix is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 36 Optix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 11
Ok thanks a lot It did help

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Calculate Average Prob

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap