mrnutty 761 Senior Poster

Ugh...

#include <iostream> 
#include <iomanip> 
#include <string> 
#include <fstream> 
#include <cstdlib>

using namespace std; 
int main() { 

 float totalTax = 0; // declare total tax = 0
 float totalGrossEarnings =0; // declare total gross earnings = 0
 float totalMedicalLevy = 0; // declare total medical levy = 0
 float totalEarnings =0; // declare total earnings =0
 float taxPayable;
 float netEarnings;
 float medicalLevy;
 float grossEarnings;
 string employeeRecord[11];
 int employeeNumber;
 int hours=0;
 int rate=0;
 
 // declare input and output file streams 
 ifstream infile; 
 infile.open("earnings.txt", ios::in);
 
 ofstream outfile; 
 outfile.open("data.txt", ios::out); // open the files for input and output 
 
 // set format manipulators 
 cout.setf(ios::fixed); 
 cout << setprecision(2);


 outfile << setw(11)<< "employee Number" << setw(11)<<"gross Earnings" <<setw(11) << "tax Payable" << setw(11) <<"medical Levy" << setw (11) <<"net Earnings" <<endl;
// write to outfile 
infile >>setw(11)>>employeeNumber>>setw(11)>>grossEarnings>>setw(11)>>taxPayable>>setw(11) 
 >>setw(11)>>netEarnings>>endl; // read from infile

 while (infile>>next) // do more records
{

 grossEarnings = (hours * rate);
 taxPayable = grossEarnings * 0.15;
 medicalLevy = grossEarnings * 0.01;
 netEarnings = grossEarnings - taxPayable - medicalLevy;
 totalGrossEarnings = totalGrossEarnings + grossEarnings;
 totalTax = totalTax + taxPayable;
 totalMedicalLevy = totalMedicalLevy + medicalLevy;
 totalEarnings = totalEarnings + netEarnings;

} 

while (infile>>next) // do more records
{ 
 outfile << Total Gross Earnings
 outfile << Total tax;
 outfile << Total Medical Levy;
 outfile << Total Net Earnings< endl;
}

 infile.close() ;
 outfile.close () ;

 return 0;

}
}
mrnutty 761 Senior Poster

Can you show me what a[] contains? It is probably an overflow problem.

mrnutty 761 Senior Poster

Look into double linked list.

mrnutty 761 Senior Poster

Let me emphasis this :

How do I access reg in ExecutionFlow.cpp?

Excitized: You need access to the specific instance of ThreadContext

mrnutty 761 Senior Poster

whats line 18 doing?

mrnutty 761 Senior Poster

In little programs like your it doesn't show why its so bad. Just trust us, when
we say do not use global variables. It leads to much complication, and ugly code.
Compare your code with this non global variable one :

#include <iostream>
using namespace std;

int main(){
 char op;
 float in1,in2;
 cin >> op;
 cin >> in1 >> in2 ;
 switch(op){
   case '*' : cout << in1 * in2 << endl; break;
   case '/' : cout << in1 / in2 << endl; break;
   case '-' : cout << in1 - in2 << endl; break;
   case '+' : cout << in1 + in2 << endl; break;
   default : cout << "Invalid\n";
 }
 return 0;
}

The above program is easier and better to read than yours. One of the reason why
that is, is because I didn't use global variables

One problem is name collision :

#include <iostream>
int x = 0;
int main(){
 int x = 0;
 x = 12; //which one did you want to change?
}
mrnutty 761 Senior Poster

>>expression == true + true

what caused you to think this is correct? What do you think it does?

mrnutty 761 Senior Poster

You do not account for the second "fin >> next" in your second code.

mrnutty 761 Senior Poster

oh i got it! thanks!

Are you sure? I feel like I made a mistake, and just given you a code you can copy/paste?

mrnutty 761 Senior Poster

@OP would you settle for something like this :

int main(){
 
 Timer timer; //Timer is some class that does timing stuff
 string input;

 timer.start();
 getline(cin,input);
 long milliSec= timer.getMilliSeconds();
 timer.reset();
 
 const int ALLOWED_TIME = 3000; //milli seconds
 if(milliSec > ALLOWED_TIME){
   //tell user time had passed
 }
 else{
   //input was in time
 }
}
mrnutty 761 Senior Poster

umm ok, see if this is what you get.

For question what does it yield the correct answer :

#1)
ln(1 + 0) = 0
ln(1 + 1) = 0.69314718055994530941723212145818
ln(1 + 2) = 1.0986122886681096913952452369225
ln(1 + 3) = 1.3862943611198906188344642429164

Does your program yield very close answer to that?

For question # 2)

is any of these a prime by using your program :

2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71

the answer is all of them should be a prime. Check if you pass all of the test above.

mrnutty 761 Senior Poster

Try this, write std::list<int> a, into your compiler, and right click it. See if there
is an option that lets you "see deceleration or initialization".

I doubt that you will find the code. Most of them are written into a dll, and
the libraries aren't very easy to read. So if you want to look into the source code
of std::list, then I suggest a different route, that is, go online and read theory
about singleLinkedList and doubleLinkedList, and then try to write one yourself.
And when you do succeed, you will have a good idea on how std::list was implemented.

mrnutty 761 Senior Poster

Why dont you do a test run and see if its correct for your self. Use your calculator
to check if Question #1 is correct, and use the web to check if Question #2 is correct.
I can't really tell you because your code is hard to read.

mrnutty 761 Senior Poster

Wait how is boost timer supposed to help? He wants to time the input from the user.

jonsca commented: Yup +4
mrnutty 761 Senior Poster

That function is badly coded and harder to understand. I haven't tried it but
this should work and easier to read :

//calls a helper function
int sumLeafs(){
 int sum = 0;
 _sumLeafs(getRoot(),sum); //_sumLeafs is a private helper function
 return sum;
}

//a node is a leaf, if it has No children
bool isLeaf(const Node* n){
 return n.leftChild() == NULL && n.rightChild() == NULL;
}

//private helper function
void _sumLeafs(const Node* root, int& sum = 0){
  if(isLeaf(root)) sum += root.getValue();
  else{
    _sumLeafs(root.leftChild(),sum); //sum the left side of the tree
    _sumLeafs(root.rightChild(),sum); //sum the right side of the tree
  }
}

The only way you will understand this, is if you trace out the call. Otherwise it
will be hard to explain. The code above might be wrong, so I make no promise, but
it should be good.

mrnutty 761 Senior Poster

>> I think that the one with pre-allocation should be faste

First rookie mistake, pre-optimization!

Forget about it using pre-allocation. Just do it without it. It will definitely be fast
enough, unless you have some special case.

mrnutty 761 Senior Poster

>>But another problem I have is that is has to accept an int type and also a double type

There are couple of ways to do this. It depends on what you know. Have you
heard of function overloading? Here is an example :

void print(int a){ cout << a << endl; }
void print(double b){ cout << b << endl; }

int main(){
 int a = 5;
 double b = 3.14;
 print(a); //call the print(int);
 print(b); //call the print(double);
}

>>have to use references for this

Using reference is not that hard. You just need to understand what it means to use reference.
Here is an example :

void nonRef(int a){ a = 0; }
//use the '&' to mean pass-by-reference
void ref(int& b){ b = 0; }

int main(){
 int a = 123;
 nonRef(a); //a = 123 still because nonRef makes a copy of the variable a
 cout << a << endl;
 ref( a ); //a = 0 because ref does NOT use a copy of the variable a, instead it uses the actual variable a.
  cout << a << endl;
}
mrnutty 761 Senior Poster

>>I'm not sure how to create a function that accepts multiple arguments.

Its easy, its very same as a single argument function. Compare these 2 functions.

void one(int a){ /* do something with a */}
void two(int a, int b){ /* do something with a and b */ }

int main(){
  one(5); //call the one parameter function
  two(1,4); //call the two parameter function
}

That should get you started.

mrnutty 761 Senior Poster

>>g. your line result=pre_result; will actually call the copy constructor and not the assignment operator (operator=)

Really? I thought it would call the assignment operator and could call the copy constructor depending on the implementation.

mrnutty 761 Senior Poster

Here are my guess:

1) Dont get the question, what does "name" mean? DataType?
2) false;
3) C: Increment operators
4) confusing
5) false;
6) c: *
7) false: runtime variable?
8) false;
9) b: 12,81
10) d: shallow
11) true;
12) false, it can;
13) false;
14) true;
15) false;
16) b: deep;
17) false; incremental and subtraction and addition are some counter examples
18) d: pointer-to-int
19) false;
20) d: 46
mrnutty 761 Senior Poster

I bet you have a memory leak. If you are going to use pointers, then use "smart pointers".

mrnutty 761 Senior Poster

Well That's part of learning I guess. I note you said it looked pretty bad, to bad you didn't offer some suggestions other than "Learn more"...

By offer suggestion do you mean write some code so you can copy-paste from? If not then
I did offer a suggestion; which was to use arrays, and composition. But here is some
code to get a idea of what I mean :

#include <iostream>
#include <cassert>
#include <string>

using namespace std;
 
enum Number { ONE,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE};

string convertNumberToString(const Number& n){
  assert(n> 0 && n < 10 );
  string ret;
  switch(n){
	  case ONE: ret = "ONE"; break;
	  case TWO: ret = "TWO"; break;
	  case THREE: ret = "THREE"; break;
	  //...
	  default: ret = "INVALID"; break;
  }
  return ret;
}

int main(){	
  using namespace std;
  Number n = THREE;
  cout << convertNumberToString( n ) << endl;
  return 0;
}
mrnutty 761 Senior Poster

Why is Vector3::operator* returning a const Vector3 and not just a regular Vector3?

Why does your Quanternion class contain a "x,y,z" variable, when you have Vector3 for
that exact purpose?

Unless, your using dynamic memory in Vector3, which I doubt, then I am doubting your
debugging skills. What happens if you just return pre_result?

mrnutty 761 Senior Poster

Replace all char arrays with, std::string, and replace all cin.getline with,
getline(cin,stringVariable);

mrnutty 761 Senior Poster

Are you just using breakpoints, or are you using step-by-step debugger?

mrnutty 761 Senior Poster

Ok, so now one of those lines is causing it. Now try to step into line 3 and see what
happens, similarly to line 4 as well. What does showObject and setModel look like?

mrnutty 761 Senior Poster

OMG, just use vectors :

#include <string>
#include <vector>
#include <iostream>

using namespace std;

class StringArray{
private:
 std::vector<string>  strArray;
public:
 StringArray(const std::vector<string>& vecStr): strArray(vecStr){};
 void print(){ /* print vector here */;
};

int main(){
 std::vector<string> vec(5,"hello"); //contains 5 of "hello"
 StringArray strArray(vec);
 vec.print();
}
mrnutty 761 Senior Poster

So when you step through the debugger, were you able to goto your update function?
If so, then where did it stop in there?

mrnutty 761 Senior Poster

Did you step through the debugger? Whats your update code looking like?

mrnutty 761 Senior Poster

Wow, that code is pretty bad. I don't know why you aren't using arrays for the months.
Dont know why you are using inheritance when its obvious that composition is best.
I'm sorry, but you need to learn the basics a lot more, before you move onto classes,
inheritance and such.

mrnutty 761 Senior Poster

or use the library and worry about only one :

#include <cctype> 
//..
int main(){
 //...
 do{
 }while( tolower(con) != 'n'); //similarly you can do "while( toupper(con) != 'N');
}
mrnutty 761 Senior Poster

>>6 years! :P

Lol nice. Its really easy, but also a good question as well. Next time message me the answer, so others can try it to.

mrnutty 761 Senior Poster

umm...couple of ways, first is the easiest.

//option #1
string path = "AMBER/data/runx/amhsk_run"; 
string runNumber;
cin >> runNumber;
validate(runNumber);
path += runNumber + ".root";
//option # 2
string path = "AMBER/data/runx/amhsk_run*.root";
string runNumber;
cin >> runNumber;
path.replace( path.find('*'),1,runNumber) ;
mrnutty 761 Senior Poster
mrnutty 761 Senior Poster

But vector<int> a is a lot smaller than int a[100]; and more efficient if the program only needs something < or > 100.

>>i'm just curious where you get the information from.

I try to use my head for something other than a hat rack. The reason stated above is pretty obvious.

Well yea, thats very obvious. Thanks

mrnutty 761 Senior Poster

>>By "efficient" I meant memory wise. And for access speed they can be just as efficient as C style arrays. And where do you find the dimensions hard-coded in a vector

I sure std::vector(5,0) takes up more memory than int array[5]; Just look :

#include <iostream>
#include <vector>
using namespace std;
int main(){
	vector<int> v(5,0);
	int a[5] = {0};
	//for me, its 24 and 20, respectively;
	cout << sizeof(v) << " " << sizeof(a) << endl;
	return 0;
}

But I heard from someone very knowledgeable and trustworthy, that when full
optimization is on, std::vector is almost exactly like an raw array.

I'm not questioning that one should use std::vector instead of raw arrays, i'm just
curious where you get the information from. Thanks

mrnutty 761 Senior Poster

except that using vectors is much more efficient because the dimensions are not hard coded

Citation please?

mrnutty 761 Senior Poster

Given two numbers, you can use std::max(a,b) to return the largest out of a and b.
Thus using the same logic you can just nest std::max for 3 inputs, so std::max( std::max(a,b) , c) returns the largest out of the 3 inputs. There is a very similar patterns for 4 inputs and so on.

mrnutty 761 Senior Poster

Given two numbers, you can use std::max(a,b) to return the largest out of a and b.
Thus using the same logic you can just nest std::max for 3 inputs, so std::max( std::max(a,b) , c) returns the largest out of the 3 inputs. There is a very similar patterns for 4 inputs and so on.

mrnutty 761 Senior Poster

Is it just me, or does it seems that people posting here are getting dumber and dumber? Dang if its this hard for you then, go work at McDonalds or something.

mrnutty 761 Senior Poster

Forget it. She was like the girl I explained. Now she is just another slut. She just
told me what happened during our break. She kissed 1 guy, slept with another, got
drunk, and smoked weed. You dont know how mad I am. I feel like beating the shit out of someone and slapping her slutty, ass in the face so hard. Unbidden, now I see what you
mean, in your 1st post. I can't believe this happened. I'mm beat the shit out of someone.

mrnutty 761 Senior Poster

What exactly do you mean?

If you have an object moving, and you want gravity to account for its movement, then
you can just do this :

const float GRAVITY = -9.8;
ball.x += ball.velocity.x * dt;
ball.y += ball.velocity.y + GRAVITY * dt;

Those follow from basic Newtonian equation.

mrnutty 761 Senior Poster

Look above at bench post.

mrnutty 761 Senior Poster

Does this mean that this thread is your last? How ya going to do it? Pills, gas

No it does not mean that. I wouldn't do that to my family friends and my loved ones.
Just because I thought about heaven doesn't mean I'm going to kill myself.

You may want to chill out a bit with regard to going to meet yer maker thingy. I've had some really wicked sugar rushes in these joints too - I find camomile tea brings me back down to Earth. Weird how the commonest substances can send you off yer rocker ain't it?

Actually I stay away from a lot of sugar. I try to keep myself in good shape for my basketball team.

mrnutty 761 Senior Poster

Yes, I concur, I saw the light some time ago. When I left my Independent Chapel one Sunday morning. This is what I felt.

Although funny, I disagree.

mrnutty 761 Senior Poster

Ok here goes. Thanks a lot for the care and interest Unbidden Ghost. I really
appreciate you taking the time to help a young one.

I'm gonna get straight to the point. She is actually doing some soul searching. So at
the beginning when I meet her, she didn't do anything "fun" as one would say. She was
very strict with herself. She didn't go out. She didn't smoke weed. She didn't drink.
So as we started going out. I got her to loosen up a little more. She always told me
she finds me very attractive, although I get sick of that. So as time went on, she
I got her to drink a little, we went out to clubs or parties, and she even wanted to
see how it feels to get high. So towards the end of this relationship, she really
let herself go. She is a really smart person( 4.0 GPA, major CompSci ), and even
worse, she is a deep deep thinker. She thinks about herself a lot. How much she
as gone away. So how much she has drifted her self. So thats why she wanted to do
some soul searching. She needs to find herself again. I totally respect that decision.

I know I don't love her. Even though she told me she loved me, which is very
confusing. So I don't think I'm gonna chase for her. I'll …

mrnutty 761 Senior Poster

Ignore all these scrub posters and use Horner's algorithm.

I take that as a compliment, thank you. And thanks for the info.

mrnutty 761 Senior Poster

Hey how about you add some random snippet section in that wiki? So we can post random snippet there, that is unless you want to organize the wiki in some way that you would like to.

mrnutty 761 Senior Poster

I didn't compile your program bench, but in some compiler you may need to use the
keyword, typename in your to_string function, specifically here

typedef std::iterator_traits<FwdIt>::value_type value_type

because the value_type depends on the type that is passed. So the line would be :

typedef typename std::iterator_traits<FwdIt>::value_type value_type
mrnutty 761 Senior Poster

Why would you do this recursively? It will be better to do it iteratively.
For example :

//assume coefficient is written from highest degree to lowest 
float evaluate(std::vector<float>& coeff, const float x){
 int degree = coeff.size();
 float result = 0.0f;
 for(int i = 0; i != degree; ++i){
   float powResult = std::pow(x,degree - i);
   result += coeff[i] * powResult;
 }
 return result;
}