You can use the sin
function from the C++ library ...
(Look here for a reference)
hurbano commented: thanks for the help +1
Did you actually write this code ?
Maybe you should also add an error handler ...
e.g: If this is your input:
a: 1
b: 2
c: 2
You'll get an error ...
Maybe it's useful to first check whether (b*b-4*a*c) > 0 ...
Why are you passing a pointer to your array in the function void quadraticFormula(double a, double b, double c, double *totalResults)
??
You can also pass an array to a function as follows: void quadraticFormula(double a, double b, double c, double totalResults[])
as an array's name can be used as a constant pointer to that array ...
Edit:: With a constant pointer to that array I mean: a constant pointer to the first element of that array ...
cin >> tside;
?
Yup that's right! :)
Use 'cin' ...
Edit:: e.g: cin >> your_variable;
Edit:: e.g: cin >> tside;
Instead of
exit(0);
, consider returning a bool from your function for success or failure. Then the program can continue.
Yup, I forgot to mention that, but generally it isn't recommended to use the exit(0);
function ...
I second the
cin.get()
thing. It probably doesn't really matter in this case, but it's something to consider.
I only gave him a better alternative to system("PAUSE");
...
I guess it's more of a principle thing for me. All other things being equal, if one thing has benefits over the other (even if they are very small), you should choose that thing.
I just find when I stray from this, even on small things, I am more likely to stray from it with bigger things. That's why it's so important to me. But really not a big deal in this discussion.
Now if you said there are benefits to no space, I would be more likely to accept it as being ok.
If you don't type a space, your code will be 1 character shorter, less typing :P ...
But actually both don't have real benefits, it's only a matter of preference ...
Agreed with all above :)
I disagree.
Why it's just a useless discussion, and what's the main topic?
"a space" ...
BTW, What wrong choice could he make in this case ?
NO ONE !
Because the C++ preprocessor is just ignoring that space ...
You should not put any spaces
between the arithmetic expression, it should
be written as 5+3 or -6+3, and so on
Yeah, agreed, but you're expecting the user is always inputting valid information, without ever making a typo, so it might be better to do some input validation and display an error in that case ...
Of course it is but when given a choice people often make a bad one ;)
I'm not trying to make a big stand on this but I would say this is one of those things that just make sense to do and I thought I would encourage it.
In this case he can't make a bad choice ...
And here stops the discussion !
It was a suggestion for readability. It's minor but the space should be there as far as I'm considered. But it's just my opinion.
Actually it doesn't make much sense as it's his choice to write it with or without a space :) ...
Thanks a lot for that suggestion there, it condensed the code and made it much more manageable! All though if I could trouble you to explain exactly how that works and is able to be the equivalent of several if statements, I would be very appreciative and probably a much better programmer. :)
OK, I'm trying to explain this to you:
We start looking at the graphical representation of the instruction 'char board[9];'
0 1 2 3 4 5 6 7 8
[ ][ ][ ] | [ ][ ][ ] | [ ][ ][ ]
(in total we've 9 elements in the array (element 0-8))
0 3 6
[ ][ ][ ] | [ ][ ][ ] | [ ][ ][ ]
| | |
row 1 row 2 row 3
starts starts starts
here here here
If we know the row where we want to write in, we first have to calculate the actual place
of the row in the array before we start writing ...
So, after a bit trying you can find the place where the row starts (in the array)
by using the following formula:
row_place_in_array = (row-1)*3 ;
|
in each row there are
three columns
Now we only need to find a formula which is indicating the place in the array if we know
the row and the column ...
We already know the formula to find the row in the array, so if we start from here and
if we adjust …
Include a space between
#include
and<iostream>
The space isn't really needed, the code will also compile without a space between #include
and <iostream>
Agreed with all above ...
To the OP: Please remember that every C++ keyword is written in lowercase, this was introduced to keep everything simple, so actually you don't have to hesitate whether you've to enter an uppercase letter or a lowercase one, it's just always in lowercase ...
Your Example is cute and all (and a bonus, it works =]) but firstly its in C++ and while I like C++ I want to do it in C hence why its in the C forum ^_^. secondly this doesn't do what I want it to do.
Until you translate that to C we can't move any further with that code.
Oh, sorry for that I didn't notice I was posting in the C forum ...
In that case you'll have to take a look at the link I posted ...
Yes I did notice that it returned a int type, I designed it to be like scanf which returns the number of variable scanned (at least thats what it did last time I checked, I can't seem to remember =[ ).
*Starts Testing tux4life's suggestion*
...
... ...
... ... ...
Ah, I see. Apparently your method fails because of the fact that its an array of characters, but that is a datatype I hadn't thought of before so I will experiment with it
It fails because you implemented it in the wrong way ...
Maybe the following thread might be useful for you: http://www.daniweb.com/forums/thread56000.html
I've also included an example:
#include<iostream>
using namespace std;
char * strRet();
int main()
{
char * string;
string = strRet();
cout << string << endl;
delete[] string;
return 0;
}
char * strRet()
{
char * sth = new char[15];
strcpy(sth, "Hello World!");
return sth;
}
I think you should also do some form of syntax checking ...
Check what happens if you input 56 + 5
? (with very much spaces between '56' and '+' ... And with very much spaces between '+' and '5') ...
You could make a function which reads out all the spaces (and characters between the operand and the operandus)
Or, you could just give an error ...
You have to decide it !
BTW, Nice program ! :)
Did you already notice that your function getString()
actually is returning a value of type int
?
To return a c-string your function has to return something of type const char *
...
its just like -_-
i sorta get the syntax of code sorta
but like it all seems like gibberish i dont know what to put where i see everyones response and i can read them and im sure they make sense but to me it might as ell be in a different language and this stuff is so frustrating i was crying in the middle of class and had to leave cause i dont get this stuff and thats just more stuff i missed and just less i know how to do and i was lost enough before and i just wanna drop out but i need to pass this stupid class for my major for some ridiculous reason and just ahhh i wanna screami dont know what a dynamic variable is, how to make one, why youd wanna, i dont get what classes are, set functions none of this stuff..ugh sorry to rant
In that case you should read a tutorial/book about C++ first ...
Thanks a lot for that suggestion there, it condensed the code and made it much more manageable! All though if I could trouble you to explain exactly how that works and is able to be the equivalent of several if statements, I would be very appreciative and probably a much better programmer. :)
It's just a formula I found by making a graphical presentation of the array which represents the board, and after searching a little time I came up with this ...
You can try it out and you'll see it actually works ! :)
I was just trying to stem any confusion. I think we can all drop this topic, doesn't look like the starter is interested.
Agreed, he has only one post in this thread ...
Please post using code tags !
A C++ compiler which follows the ANSI/ISO-standard will never compile this code ...
Dear rahul,
I thought you were saying that compiling the following is possible:
#include <iostream.h>
using namespace std;
...
So, I must have misinterpreted your post, sorry for that !
In your function phase1 you've to change everything in the while loop to:
c = in.get();
/* We use an if-statement to ignore the newlines */
if(c != '\n')
{
c = (int)c;
array[c] = array[c]+1;
}
It should work now !
I guess the unwanted number '1' is the number of newlines your program has counted so far ...
I actually don't understand your question ...
This function have to change order of chars: first becomes last, second - last but one etc. Then, I have to write some simple program with this function.
Actually that's the same as: Write a function which uses recursion to reverse a c-string ...
I too can see nothing wrong in your code (syntactically) except the thing what vmanes did already mention ...
Sounds like a class will be useful to implement it ...
Awfully! You compare unitialized variable c...
Oh, that was a dumb mistake !
Change line 3 in the code I posted to int c = a;
...
Actually you can replace lines 37 - 63 with: board[((row-1)*3)+(col-1)] = *player;
...
Or with board[((row-1)*3)+(col-1)] = player;
if you're using a simple char-variable ...
Yeah, it's true what niek_e said ...
You could make use of a function like this one:
int get_range(int a, int b)
{
int c;
do
{
if(c > a && c < b)
return c;
else
cout << "Please enter a number between " << a << " and " << b << ": " << endl;
} while (cin >> c);
}
If you have to use pointers I would rather suggest you to use C-strings (character based/array of characters) instead of C++-strings as a C-string actually is an array and an array's name can be used as a constant pointer it will be easier to implement it this way ...
>>> You can use it like the sin function
I said that because the function prototypes of the sin
and asin
function are the same ...
Try one of the following links:
http://mathcentral.uregina.ca/QQ/database/QQ.09.07/s/mukesh1.html
http://www.mathpages.com/home/kmath489.htm
http://www.newdream.net/~sage/old/numbers/fact.htm
http://www.mathlinks.ro/Forum/viewtopic.php?p=577096#577096 (here's described a formula)
This code will calculate the last digit of a multiplication of two numbers:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int str2int(string s);
string int2str(int i);
int main()
{
int n1, n2, digit;
string s1_dig, s2_dig, s3_dig;
n1 = 25;
n2 = 6;
s1_dig = int2str(n1);
s1_dig = s1_dig[s1_dig.length()-1];
s2_dig = int2str(n2);
s2_dig = s2_dig[s2_dig.length()-1];
s3_dig = int2str(str2int(s1_dig)*str2int(s2_dig));
s3_dig = s3_dig[s3_dig.length()-1];
cout << s3_dig << endl;
return 0;
}
int str2int(string s)
{
stringstream ss;
int i;
ss << s;
ss >> i;
return i;
}
string int2str(int i)
{
stringstream ss;
string s;
ss << i;
ss >> s;
return s;
}
NOTE: It might be useful to put the code in the main into an apart function (e.g: f_lastdigit(int n1, int n2) )
He mentions needing to use a for loop, not recursion. At least that is how I took it.
Taking user input is easily done using a Scanner.
Yup, that's right !