jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Your temparray is exactly that. It's created within the local block of the function and destroyed when the function exits, unfortunately.

Pass in an array by pointer so that your function signature looks like

void arrayfunction(float arr[],float flt1,float flt2,float flt3)
{
   //load up the array here

   //still exists on exit

}

I suspect since you are using opengl that your function is really more complicated than this.

What you have above could work with some modifications, but I think the above way is much more straightforward.

scar164 commented: Thanks for your help! +1
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Are you drawing numbers in between 0 and 1 or just 0 and 1 themselves? I'm not sure about biasing the selection. I suppose,for example, you could generate a random floating point number between 0 and 1 and if it's greater than .33 output a 1 and less than that make it a zero. That would give you roughly twice the number of 1s.

However, I'm certain there's someone that has dealt with this more than I have and so I don't guarantee this to be an ideal solution and there's definitely a better way.

In the meantime check out Narue's great tutorial on the subject: http://www.eternallyconfuzzled.com/arts/jsw_art_rand.aspx

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Why not copy it all over to a char array, substituting the '*' for and integer value of 1 and converting all other integer values to characters (assuming they are all one digit)?

I mean technically chars are integers so you could store you asterisks as 42 in the int array but then your 1 values would have to be 49 to remain consistent. If you were to try to output the array by casting it your 1's would end up as a non-printing character.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

What are the types of both arrays?

Also, the way you have it laid out if you hit a 1 on the first pass through the while you'll get stuck there (unless you are incrementing row and col somewhere else).

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

See if the content of this thread http://www.daniweb.com/forums/thread27905.html will help you. Basically, you can use a string stream with getline using a delimiter (analogous to using getline with a delimiter on an input stream).

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Okay. I'm not well versed in that but hopefully but there are a few folks around that will be able to. Apologies that I couldn't help!

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

What are you using for a windowing library ("...in two text box")? (e.g., Win32 API, MFC, CLR-Winforms, Qt)

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Yes, but even in that same handout they have you include

#include <iostream>
#include <string>  // != string.h (I'm not sure how this is resolved in pre standard compilers)
#include "genlib.h" //which must be a custom header to go with their course

If you're just starting out and you're not attached to VC++ 6 due to legacy code or needing MFC, grab the VC++ 2010 Express Edition compiler from M$. It's free and is infinitely more standard compliant than v.6.

you’ll need to include genlib.h to make the short name
string visible instead of requiring the cumbersome std::string.

All this means is that the header probably has using namespace std; (or using std::string by itself) so that you don't have to qualify the name (typing std::string instead of typing string, just as you would have to for std::cout, etc) it doesn't have any bearing on anything else.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

You want to include <string> instead of <string.h> to declare it as a std::string which has a length() method (really just an alias for the size() method). strlen() comes in <string.h> (or <cstring> in a compiler that conforms closely to the standard (which VC++ 6 does not)) and is used on null terminated C style strings. How is str_name defined?

That's rather terse but post back for clarification or google for std::string versus C style strings and there should be a ton of hits.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Unless you have to use it for some reason (it's nearly 20 years old!!) go for Visual C++ Express Edition or Code::Blocks

If you're stuck with it, search in this forum or the C forum for directions on getting it to work (the C forum posts will be related to Turbo C but it's the same concerns). Example: http://www.daniweb.com/forums/post1281354.html#post1281354

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

I would make 2 arrays, one holding numbers and one holding counts corresponding to the numbers. If you want to process the number at each entry, check and see if the number exists in numbers array. If it does, increment its corresponding count, if not add it to the numbers array.

Changing it to read all the numbers in at once and then process them wouldn't be too difficult.

EDIT: Agni nudges me out by a hair

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

You need the arrow -> and not the . on lines 15 and 16 because theTime is a pointer.

shaynerossum commented: Thank you very simple solution. +2
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

I think the OP is getting at being able to tell if the user has entered a number or not.
One way to do that would be to use the .fail() method of the input stream. You could also probably use stringstreams.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

For example,

if(x >=1 && x <=5)

will work

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Can you use CMake to generate a Visual Studio project for it? (does it have a CMakeLists.txt in the main directory)

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

This site is indispensable: http://www.cplusplus.com/reference/ (their tuts are good too but it's nice to be able to pull up a function and get a quick snippet to go with it)

http://web.cs.mun.ca/~michael/c/ascii-table.html is a nice plain ol' ASCII table.

For more advanced stuff check out http://www.eternallyconfuzzled.com/. It's very well written (by DaniWeb's Narue).

I'm sure there are hundreds of thousands but those are the ones I use most frequently.

Lusiphur commented: Thanks jonsca, I knew I'd get a reason to give you rep eventually!! :) +1
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

TryParse returns a bool as to whether or not the conversion was successful it might look something like this

using namespace System; //up at the top
if (Double::TryParse(textBox1->Text,value) && Double::TryParse(textBox2->Text, rate))
{
  result = (1/rate) * value;
  textBox3->Text = "$"+result.ToString();
}
else
  textBox3->Text = "Unable to convert parameters -- ERROR";

Or something similar to that (e.g. instead of the error message, set value to 0 and rate to 1 and print a message out in the debug)

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

You're very welcome.

You don't need the TextChanged handlers for the 3 textboxes as they are not doing anything at present. TextChanged means the method will run with each character you are typing for example (or when you backspace, etc).

Have value be either a private member variable of your class -or- a local variable to your method, but not both. If you need "value" elsewhere in your class keep the private member variable. If not, just have it be local like rate and result.

To place the dollar sign in front just do textBox3->Text = "$" + result.ToString(); The only other thing is Parse, but I explained that one already. If you type bob into the textbox the program is going to throw an exception.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

I have heard (I have not read it) that Ivor Horton's Beginning Visual C++ 2010 (or the 2008 edition) has a fair amount of C++/CLI. I've never found anything satisfactory in the tutorial department for CLR Winforms (I've adapted my knowledge from C#).

One thing I see in the above code is that total is undefined when you get to the switch statment.

You don't need to put "value" as public (and that version would be masked by the one in the click handler anyway) unless you need it elsewhere in your code, in which case only declare it as a variable of the Form1 class. The TryParse should be called right in the click handler too. You don't need to calculate result within each case of the switch statement, you can postpone it until the end.

A consideration: I would use all doubles instead of making the other two floats. You run into danger of losing some precision in the conversion.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Thank you jonsca for your insightful response! I researched the Double::TryParse and I believe I understand how it works. Although I am unable to figure out how to implement it?

Double::Parse will throw an exception if there is no valid conversion between the string and a double. TryParse returns true if the value converts. I misspoke earlier calling the parameter an out variable (which is what it is considered in C#). Basically create a double variable and pass that in as the second parameter: Double::TryParse(stringtoconvert,doubletoacceptvalue) and you can put it in an if statement to make sure you are really getting your double.

So in your case the string to convert will be textBoxWhatever.Text

I should have also stated this was is my attempt at writing a "Forms App" and it's confusing the heck out of me. :)

As for your question on how I would signal the conversion to begin; I assumed I could use the "Convert" button to call the event?
Thank you again for your assistance!

No worries. It takes some getting used to. Luckily a lot of the dirty work is taken care of behind the scenes.

Yes, you're correct, so your formula will go into that ConvertButton_click event (or whatever the name of your button is).

Anatake commented: Thank you for helping a newcomer! +0
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

so you need to remove
the comments to have the book_list declared.

mit, It wouldn't let me +rep you (since I did yesterday) but yeah.

Yeesh (slaps my forehead), sorry Bobbie Jean.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

You need to make sure that you have all the files in the same project, or if you are compiling on the command line put BookRecord.cpp first.

As for devC++ the version of g++ it uses is too out of date and the IDE itself is no longer being developed. You should be fine with whatever compiler you have now, we just need to figure out why it's not liking your other CPP file.

BobbieJean commented: Jonsca always tries to help and always seems to give great advice!!! Awesome daniweb member!!! +1
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

You have text representations of the numbers for each of the boxes. Use the Double::TryParse to get the value. Look up the syntax of it as it uses an "out" variable to return the double.

Everything in .NET GUI programming is governed by events. Can you think of a means to signal that the calculation should be done (like how would you do the equivalent on a pocket calculator)?

Take a run at it and post back with any questions.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Try changing the MaximumSize property of the Form1

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Not sure. Probably setting the Location to {0,0} (or as close to it if it needs room for the border).

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

If I recall, you are using Winforms?

Here's an example in C# that you should be able to adapt pretty readily for C++/CLI: http://www.codeproject.com/KB/cs/csdynamicscrres.aspx

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Change your loop from

while(!fin.eof())
-to-
while(fin >> temp)

and eliminate line 17.

See http://www.daniweb.com/forums/post155265-18.html about why .eof() exhibits this behavior.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

There's an extra '\n' (newline) in the stream (from when you press enter after the number) that's being taken in by the first getline as a signal that input has ended. So it's getting skipped. ShadowScripter's idea is correct and probably preferable (though you could use stringstreams as well) but an alternate would be to put a cin.ignore() before your getline statement to sop up that extra '\n' .

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Trace through your program by hand and see what happens when you enter 0 (hint the while is going to spin forever because integer == 0 will always be true -- which causes the loop to continue not stop).

Also do this for finding the highest and lowest numbers. You'll find you'll need to rearrange your loop to encompass more of your statements. As it stands your code only goes through once if the number is nonzero and gets stuck in an infinite loop if it is zero.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

The for loop was solely for 80-117 and would actually be inside your do/while loop.

You should be able to have something like

//some pseudocode
WHILE(true) //says same as your while(1==1)
{
"Enter 'e' for expression and 'q' to quit"
 READ IN a character
 if(character is 'e')
 {
      //your other code
 }

 else if (character is 'q')
     return 0;  //since your function returns an int

}
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Because comparing the value in dvar1 with 'q' can be dicey, the best solution would be to make a sub menu (I know it's not exactly what you want but aside from reading in your expression as a string and parsing it out that may be the best way).

The menu would say "enter e for an expression and q to quit." If they select 'e' you can move on to 127, if they select q you should return a value (e.g., 0 but it doesn't have to be) so you can return to main1 without having to call it explicitly (when you are calling main1 like that it's a new copy of main1, not the one you originally came from).


Look up for loops for lines 80-117

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

From www.cplusplus.com about the operators < > <= >= etc:
http://www.cplusplus.com/reference/string/operators/
"These function [sic] depend on the value returned by string member compare."

Regarding compare:
http://www.cplusplus.com/reference/string/string/compare/

"The member function returns 0 if all the characters in the compared contents compare equal, a negative value if the first character that does not match compares to less in the object than in the comparing string, and a positive value in the opposite case."

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Yes, I have noticed this on my profile page too and have reported it to Dani. So, you're not alone :)

I'll let her speak to any findings, though.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Line 51 -- check your = vs. ==

deleteNode is incorrect as you need to translate your position into i,j
(see your instructor's comment)

// This array will hold the data. Notice we only need a
// one-dimensional array since each node is represented
// in both the rows and columns of the adjacency matrix.
// So we'll use rows, but columns will also work.

I didn't try to run it but take another crack at it with those things and set up a main to test it with. There may be other logical errors but those are the most obvious to me.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Ok, thanks for understanding. First off, I'm a bit confused about the void type that's declared for the myswap function. They say void is used for types that do not return values, but doesn't it return a value? I would think that it returns the value of a and b by looking at it. So you can see how much I understand this, not very much. Next, I understand how the swap takes place and a and b are going to swap with x and y. Now if I were to replace (int & y) with (double & y) would that create an error? Because I am trying to pass an int and a double to the function. Or, will the double convert automatically?

In this case there's nothing being returned, that is the function has no value when we've returned to main. Don't necessarily think of the parameters you pass in as being returned, it's almost as if you would be using the variables defined in main right in the swap method itself.

If you were to replace one of the ints with a double it would give you a warning just because of the nature of the function I created (assigning one to the other would truncate the double to it's integer portion when it was assigned it to the int). For what you intend there should be no problem.

By the way, I looked at a swap function using pointers and the difference that I …

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

He had the a^2 there. Doesn't matter, glad you got it working.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Assuming you have some experience with pointers this has a great summary in the top post about references versus pointers. If you don't have any experience with pointers, look up a swap function using them and note how it's different from the one I showed you.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Sorry I was just trying to get a better understanding of the concepts of functions and references. After reading your response to the previous thread I was confused and wasn't sure what you were trying to tell me. I think my problem lies with not understanding functions and multi values period let alone using references. I was wanting someone to give me sort of a tutorial on this before I went back to tackling the problem again. I sure do thank you and everyone else who has helped.

It's okay, just didn't want to you to think that you couldn't ask for clarification. I'm not sure what you mean by multi values. Functions can take any number of arguments (parameters). There is definitely a finite number that you can have but it depends on how large the stack is, so don't worry about that for now.

Functions are analogs to their counterparts in math. y = f(x) takes one value of x and transforms it into a y value. You could also have z = f(x,y) which takes a pair of x and y and transforms it into a third value.

A function in C or C++ can only return one value. Therefore we have to resort to "trickery" to get more than one thing back. It would be like if we had y = f(x) in mathematics which would not only return y, but would also change the value of x for the next time we needed …

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Why didn't you continue this thread?http://www.daniweb.com/forums/thread294268.html

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Ok, I'm not sure if when helping people figure out their code if you actually copy, paste and attempt to run the code, or if you just look it over, so I am going to explain what I have done and what the error is now.

If it's a compilable piece I usually try to run it, or if it's a function that I can couple with a small main to test.

The whole idea that tesuji was trying to show you would eliminate the need for 2 separate file openings:

while(inFile>>next)
{
   total+=next;
   totalsq+=next*next;
}

ave = next/counter;

Then use the formula on line 14 of tesuji's proof. ( sum(xi^2) is totalsq ).

Your problem with your current code is on line 52:

while (inStream >> next);
Griff0527 commented: Excellent at guiding in the right direction while making you think for yourself. +1
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Sorry, even a phrenologist is allowed to make a mistake.

Yes. It's an aperiodic event when it does happen :)

Unfortunately, your implication is incorrect.

Granted. Apologies to the OP.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

For Example Suppose we have arr[10]= {2,4,7,3,7,8,1,7,3,1};
and file1 have elements 1,4,7,8,10 and File2 have elements 2,3,5,6,9.

Arrays have a starting index of 0, so you'd have to subtract 1 from these numbers to get the actual index.

Read the numbers from your first file into an int array X, and use X[0],X[1], etc as the index of your arr[10], so arr[X[0]] would be the element corresponding to the first index listed in the first file. Do the same for an array Y read from the second file.

If you're not clear on file handling there are probably dozens of tutorials out there. Here's a good one: http://www.cplusplus.com/doc/tutorial/files/

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Standard dev is Sum((datapt - mean)^2)/(counter -1) so you can't add the squared data and then subtract the mean squared unfortunately since there is a cross term of -2*datapt*mean when you square it out. You need to hold all the data then calculate the mean, then do the calculation.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Use the .is_open() method on lines 16 and 38 rather than fail. Fail is something different like for example if your stream is trying to read in doubles and it encounters a letter in the file (there are other situations that this arises in too). See http://www.cplusplus.com/reference/iostream/ios/fail/

Also, you could just read the data into an array (as long as it's not overwhelmingly huge) and do both of your calculations off that (unless you assignment precludes this).

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

The code you have posted is not the same as the one you compiled. C++ is case sensitive for variable names so Std:: isn't std:: (you need the latter). Also, rather than being end1 (number one) its endl (letter l). Again, both of these seem to be correct in the code you posted so you must be compiling something you typed in.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

I'm not familiar with the objective-c syntax, but in C++ it would be something like

void func2(NSScanner * myNSScanner)
{
   //yada
}

void func1()
{
   NSScanner * aScanner = new NSScanner(); //or your parameters 
   func2(aScanner);  

}

It's different from the integer example because your aScanner is already a pointer

(You could also pass around a reference to your object (using the & operator instead of * in func2 -- just pass in a NSScanner object to the function directly you won't need the pointer))

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Combine them into one procedure:

Are any of them equal to each other
    Yes: Move to next step
    No:  Is the first the largest?
         Yes: Set largest to first
              Is the second greater than the third?
              Yes: Set smallest to third

Etc.
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

If you leave the average as an integer you will lose the decimals. That's what I was saying, if you have 1 + 3 + 4 = 8/3 = 2.6666etc will become 2

You have the average incorrectly in your code as the product/3. It needs to be the sum.

I'm checking for the sake of your sanity, I'm assuming this if/else bit is specified in your assignment. If it isn't you could get the whole thing done in just a few lines (fewer with a for loop).

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

A couple of things to check:
What is the definition of average? You do not have the formula correct.
Secondly, if you have the numbers 1,3,4 (for example) will your average be an integer?

What you are neglecting in your if/else cluster there is that you can test for more than one condition at a time, e.g. if(firstNum > secondNum && firstNum > thirdNum) will cut down on the number of steps. Your specification only asks for the largest and smallest, so if there are two smallest it doesn't matter which is which (at least in my opinion, your instructor may feel differently).

For the flow charting, just go through the steps of your if/else and write them out on paper noting the different paths, make branches off of the earlier tests. For the other portion with the sum, product, and average the calculations can probably be listed as a single step (plus an additional step for the average from the earlier result).

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Use str.str().c_str() instead of str.str() on line 26. You need the std::string value represented as a C-style string ( char * ) and the c_str() method gets you that.