jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

This doesn't magically assign that function to sinAngle

Indeed it does :) - Salem

Clarification:
What I meant was that OP couldn't say double sinAngle = sin(angle); and then have the function sin(angle) (in the abstract) substitute back in for the value sinAngle down below like the OP wanted it to.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Do you understand it now (is the important question)?

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Step through the code to see:

int main()
{
	double angle=0;       //angle = 0
	double sinAngle = sin(angle);  //sinAngle =  0 at this point

	for (angle=0; angle<=90; angle++)  
		cout << sinAngle << " " << endl;  //sinAngle hasn't changed
	                                       //and won't change so the value ^^^
                                              //is printed 91 times
	return 0;
}

Compare with (skip the other two variables):

for (double angle=0; angle<=90; angle++)  //since you're going by 1 anyway you could use int
		cout << sin(angle) << " " << endl;  //angle is changing each 
                                                                          //pass through

P.S. the math library uses radians so you need to go from 0 to Pi/2 if you want 0-90 in degrees

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

You only did the calculation once:

double angle=0;
double sinAngle = sin(angle);

This doesn't magically assign that function to sinAngle, it evaluates it at angle 0 and prints it over and over again in your loop. Leave the declaration double sinAngle; outside and move the sinAngle = sin(angle); inside your loop.

Salem commented: Indeed it does :) +19
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

thought it was something simple

No. It should be but it's one of those things.
Look around at other integration programs that you might run across on the net and see how they do it. It's something people used to get around with #defines I think.
Otherwise for the time being just have f_int as

double f_int(double x)
{
     return x;
}

for when you need f(x) = x and (replace it completely with)

double f_int(double x)
{
     return x*x;
}

when you need x^2, etc.

Hopefully someone more steeped than myself in numerical methods will have a better suggestion for you.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

You dont use metric for road-related stuff apart from petrol..

I know percents are unitless, I was just having fun. That number does seem a little high but I'll take your word for it.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Firstly f_int takes a double for a parameter and x is a character.

Beyond that, though, when you read the value into the function f_int doesn't suddenly become that function. Each time you use that function name in I_simpson that method will be called again and you will be reprompted. The '\n' left over in the stream from x + [enter] cascades through all of the prompts since they all are supposed to take a double too.

All that being said, this is not a trivial thing to do. One way to do it would be to hard code your (mathematical) function into a (C++) function which I think is sort of what you trying to do above. You can also look into function pointers but you'd still have to worry about translating your input into something the program can evaluate (if you want anything beyond a polynomial). If all you need is polys then look around on this site for stuff on Horner's method you could incorporate that pretty easily.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

That must be a percentage measured in metric. Just kidding. :)

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

"total_Items = (int)(Console.Read());" blows up on my machine.

I looked into that a little. Console.Read() is meant to get a character and turn it into an int (for its ASCII representation) so if you type in 2 you'll end up sending 50 back (it's supposed to be the getchar() for the .NET world I think). Also, it blocks so I believe if your prompt hasn't cleared the buffer it's going to suppress it. All around not at all the right method to use.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

maxValue is still fine. See for example: the 5th number the series is 3. What does 3 have to do with the number of iterations? Nothing. Incrementing the fib value doesn't make much more sense. So create something named count and compare if it's less than maxNum.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Good luck with it. There were a couple of false starts there on my part but it should be fine now. Post back if you have probs.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Gee where did line 49 come from? Your middle statement there is just going to evaluate to true or false and not do anything anyway.

I think you're confusing yourself a bit because you're using iterations and max value interchangeably but they are very different. In other words "fib" should not factor into your loop condition at all. Create a counter (just an int) to use for this purpose as you go through each iteration. Hint: you have to initialize your counter to 2 so that you account for the first two terms you have found outside of the loop.

Once you get the do/while loop done the while loop will be a slam dunk.

Curiosity question : why don't you ask for the iterations right after the user selects a choice (you can do it around line 24 or so) since you are holding onto the choice value anyway. That way you don't have to duplicate the code 3 times.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Since your numbers are characters all must be enclosed in single quotes

int firstChoice()
{
    char choice;
    cin >> choice;

    if(isdigit(choice))
    {
        if (choice=='1' || choice=='0')
        {
            return choice;
        }
        while(choice!='0' && choice !='1')

is what I meant. Goes for anytime you are comparing characters.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Yes, sorry, I missed the real problem. See my edit above. (choice == '1' etc.)

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

You need to check versus '1' etc. (with the single quotation marks). Ignore any of my previous edits.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Yes, that would work. Eventually you might want to put that if else sequence into a while (or do/while) loop so that you can get the user to re-enter.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Do you actually need the numbers for calculation within your method? If not just take them in as char and check versus '1' (etc.) in lines 10-17.
If you really need the numbers themselves, take them in as char and convert to the numerical value by subtracting '0'

e.g.,

char one = '1';
int ione = one - '0';  //a char is really a 1 byte int anyway

Check an ASCII table to see why this works.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Line 25 is incorrect.
It should be: while (salesPerson != -1); what you were doing was assigning not comparing. I'm not sure how you were getting output at all with that code.
Within your while you might want to enclose lines 19-24 in an if statement with if(salesPerson != -1) so that you can skip over the data entry if the user has opted to exit.
See if these two help towards the other problem but there's probably still some inconsistencies there.

EDIT: note the != I was mixed up.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Change your while loop to a do/while loop with the same loop condition. Move your prompt for the salesperson number into the loop. That way it's called the first time through and any subsequent times.

Line 20 has the incorrect format specifier for a double, it should be %lf
instead of %d.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

This goes very much in depth but seems to have some good background in the beginning. Lex is a specific lexer but the concepts are the same.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

So move line 38 back into the for loop then.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

I think that block can be drastically reduced into a couple of separate passes where the base price is calculated and the extras are added on with another if for each. That was going to be in phase three :)

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Use ./ that will go element by element (I'm assuming that's what you are looking for). If you're looking for matrix inversion times another matrix than for inv(A)*B you need A\B.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Cool. I thought maybe it had gotten lost in the communication before. Even though you are using the older compiler you should still be very much aware of what has been standardized since then. In addition to what JH pointed out, you should use #include <iostream> instead of iostream.h to get the standard header. Same goes for string.h which has become #include<cstring> with the standard. Just pointing it out so you don't get a culture shock should you have to change.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

You must have, this was the change that I had recommended.

string & operator+(const string &s,const string &t)
{
	string* temp = new string();

for posterity: string was a class built using cstring functions not the std::string (changing the post from snippet to regular separated the code somehow)

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

You need to instantiate your temp variable. Change line 17 to complex *temp = new complex(); otherwise you're writing to an uninitialized pointer.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

This is one of those times that you have to trace the program by hand because you're missing virtually everything we talked about before (see below).
You're just outputting everything and not actually doing the operations with the variables.
When you write: cout << fib1 << " + " << fib2 << " = " << (fib1 + fib2) << endl; it's only printing out fib1+fib2 and not actually storing anything.
Reference this:

fib = fib1 + fib2;
fib1 = fib2;  (My other statement was incorrect)
fib2 = fib;
(output fib)

from post #4. These are the actual statements you need to use in the code.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Use some of the methods from iomanip (#include <iomanip>). Pay particular attention to "fixed" and "setprecision," they'll give you what you need.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Hint: shipping type is a character, it's not likely to be equal to 1 in this context. In a similar vein, Console.Read() reads in a character and returns the code for that character (so if you put in 10 it would return the ascii code for 1 which is 49). Use the Int32.Parse() just like you did the others. Put those changes in and see where you are.

if (shipping_Type == 1)
            {
                Console.WriteLine("How many items are to be shipped?");
                total_Items = (int)(Console.Read());
            }
            else if (shipping_Type == 2)
                Console.WriteLine("How heavy is your package?(In pounds)");

EDIT: Also, another hint, beware of the fact that I can get your code to skip over all of those if elses in the middle just by entering in A or B or Y or N.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

What doesn't work about it?

Also please enclose your code in code tags:

[code]

//code goes here

[/code]

There's still a few minutes left on your editing time for it.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

I overlooked that show was a friend function and not a member but that's not where the error is.

In performing the + operation you need to return a reference to the calling object otherwise you're getting no result back so it's left with junk in the value on the other side of the equal sign.

Use this as the beginning of your method:

string & operator+(const string &s,const string &t)
{
	string* temp = new string();

you'll need to make the changes to account for the fact that temp is a pointer (hint: use the -> operator for all of temp's members) and at the end return *temp; so that you return the proper reference.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

You don't need to pass in another string to your show() method as you already have access to the one within the object. It should just be cout <<p; or you can #include <cstdio> and use puts() P.S. When you are asking for help in the forums post it as a question not as a code snippet please.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Create your own class with + - * / overloaded.

This is an example for matrices but it's similar (complex will be less complicated).

Give it a go and post back when you get stuck (or if you have code already post it and someone can look it over).

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

It's going to be tricky because we don't have access to your libraries. I'm sure they are wrappers for the standard functions but that's not a given.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

FWIW, nobody's mentioned it yet -- but check out http://gmplib.org/

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Welcome. What do you have so far? I'm assuming you've done some work with OOP concepts beforehand so which do you think would be most appropriate?

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

I think I misinterpreted your requirement. You could either go back a step or you can still use what we were just doing.
From your code before:

int usednumbers[365];
int filledcount = 0;
int count = 0;
for(i=0;i < numStudents ;i++)
{
      tmp=array[i];
      for(int j=i+1;j<numStudents;j++)
      {
         if(array[j]==tmp )
        {
           if (filledcount >0)
           {   
                 [check and see if the number is already in the array and if not add it in and up the count of dates with multiple bdays.  ]
           }
           else
                usednumbers[fillcount] = array[j];
 }
  double frequency = (double) count /numStudents;
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Yet another way to keep the count:

Have an array of 366, maybe call it bdays[366] (one extra to skip day 0)
In your code, array[] keeps track of which b'day each person has...

So, if array[0] has a b'day on 122, increment it: bdays[array[0]]++; You've just counted that b'day.
Next person's b'day on 68 - bdays[array[1]]++; etc.

When done, if bdays[5] == 3 then 3 people had b'day on day #3.

I think what WaltP was saying and what I was saying has gotten jumbled together. You have to adopt something like the following. Let's assume we have an array, 366 elements long called bdays that goes from day number 1 in slot one (skipping 0) to day 365 in slot 365.
You find a birthday on the 22nd day of the year. The 22 goes into the first array called array. You go to the int value in slot 22 of bdays and you increment it(array[j] is 22, do bdays[array[j]]++;) so we're building up these counts.
Then if we encounter a birthday on the 126 day of the year, array[j] = 126 then we go to bdays[array[j]]++; and increment that one.
Initially I was saying line them up so if the first element of the days array was 126 then we'd keep the count for array[j] = 126 in the first slot of the bdays array instead. So the setup can be either

|126|73|24|9|239| in the "array" array and
|count126|count73|count24|count9|count239| in the …
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

You never call setPercent for any of the objects so that value is always zero in the output.

Also, you don't have a constructor for juice to pass in the value for the ounces.
In your drink constructor you just take the value and reduce it to zero again so it doesn't show up either.

So make sure you're getting the values to their proper spot and calling everything that needs to be called. I would definitely change that drink constructor and perhaps integrate that into another method called empty or something.

A couple of general things. If your methods (e.g., getName or consume) are virtually (no pun intended) identical to each other why not just put them in the parent class as such. You've defeated the purpose of having a class to inherit from as you did twice as much work.
Also, they'll probably push you into this eventually but you should think about dividing your code up into headers for your declarations, implementation files (.cpp) for your method definitions and perhaps a separate .cpp for your main file (that way you include all the headers in main() and in their (respective) .cpp files and compile the whole shebang at once). It's more convenient to have it in one file now but in terms of maintenance and keeping it organized (and particularly for reuse) you'll benefit from doing it in the long run.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

for(int j=1;j<numStudents;j++) We've already told you that this is not correct... bdays[array[0]]++; You're only updating the count for the day number that is associated with the first element of the array.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

The Soda class should include the pure virtual function while the Juice class should include the other virtual function.

My stuff is a bit rusty so bear with me if I'm missing something obvious but aside from varying the access specifiers (pub,priv,prot) of the two methods and using a different {public, protected,private} inheritance model for each derived class I believe they are both going to be there regardless.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Did you preload number[0] and number[1] with 0 and 1 respectively? Remember by your requirements you are doing it by terms so you should start out at 2 and run to < i

If not put up the full code and I'll take a look at it.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

EOF is not a string it's normally a negative integer. Use your getline function to drive a while loop. I would suggest the following though someone probably has a better solution which doesn't require the user to press enter twice (as it's just not clicking with me today).

while(getline(cin,str,'\n')) //getline defaults to '\n' anyway
	{
		if(str != "")
		  s_v.push_back(str);
		else
		   break;
	}

getline reads in the '\n' but subsequently gets rid of it, it doesn't end up in the string.

EDIT: Curses foiled by niek_e. I tried that solution initially but it just sits there once you are done...

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

It's just magic. The little gnomes inside your CPU like it that way.

It's seeding it based upon the clock time. If you seed it once it will go through it's predefined sequence (see Narue's tutorial) and generate a series of numbers but what you were doing was (re)seeding it with each dice roll and if they happened too close in time you'd end up starting the sequence over again with (roughly) the same seed.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Which part?
Well the counts one you should be filling in your code where you were proposing to add cnt++ before (you need to keep track of which date is which in the end so just keeping one number will only get you the total which you can derive from the other array anyway).

So you have an array (you can safely call it of dimension 365 even though you won't need it). Make a second array that has 365 elements to hold the counts.
You find a birthday on the 26.
|26|
You find a birthday on the 122 day
|26|122|
You find a second birthday on the 122, so in your other array you have
|1|2|
Find a birthday on the 200 day
|26|122|200|
other array
|1|2|1|

So now we want to find the probabilities. Probability of the 26 is 1/(1+2+1) = 1/4. Probability of the 122 day is 2/(1+2+1) = 1/2

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

I didn't compile and run your program yet, but try putting the srand() call out in main() so it's only done once.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Just match them up:

Keep the dates in one |26|5|140|156|10| Counts in the other |1|3|2|6|4| So the (approximation to the) probability of someone having a birthday on 26th day of the year is 1/(1+3+2+6+4) (but use the 1.0 etc when you enter it)

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Make yet another array of ints that runs parallel to your duplicate values matrix and increment a particular entry when it comes up more than once. I know how much you enjoy parallel arrays from your other post. The asterisk part should be straightforward from there.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

The problem is that I believe that a majority of folks on this board run the VC# IDE. Without loading it up and running it we can't tell what's the normal setup for Mono. A suggestion I've given to other users with that IDE is to tag your posts with Mono so that other users searching the site might find your post and be able to help you out.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Everything you always wanted to know but were afraid to ask: http://oreilly.com/catalog/make3/book/index.csp