asin() in <cmath>
Try here for a description.
Yeah that's truly right !
You just use it like the sin
function ...
I've found a better algorithm !
Forget about what I said and just write a function which calculates the last digit of a multiplication of two numbers ...
Actually you've to do this ...
You can use the modulus operator to convert numbers to another base
(e.g: from decimal to octal/hexadecimal/binary/etc.) ...
Read a tutorial about C++ ...
There are many floating around on the internet ...
I've found a better algorithm !
Forget about what I said and just write a function which calculates the last digit of a multiplication of two numbers ...
Oh, sorry: I was wrong about the use of atof
as it is to convert a string to a double ...
So you'll have to use for e.g: string streams to convert a number to a string ...
You can also just read out the (n+1) character of the c-string ...
If you have 6! as a string: "0.00072", you notice that the last character is always the number where you took the factorial of plus one (for the decimal point), so you don't need strlen
...
Actually you only have to find the last non-zero digit of a factorial ...
So you even don't have to calculate the whole factorial ...
As you saw in my previous post you simply could do the following:
6! = 0.1*0.2*0.3*0.4*0.5*0.6*(10^6)
To calculate the factorial of 6 ...
Now you put this into a c-string ... (use the atof
-command)
If you've don this, you only have to read out the last character of that c-string/char-array, that's the last non-zero digit of the factorial ...
So if you want to find the last non-zero digit of the factorial of 6 you calculate: 0.1*0.2*0.3*0.4*0.5*0.6 = 0.00072
> You use the atof
-function to convert 0.00072 to a c-string ...
> You read out the last character of the c-string (the subscript of the last character is always returned by strlen
)
Try converting the value using static_cast<int>(yourvalue)
...
You could use Bignums, but you would need an extra library for that purpose:
> http://mattmccutchen.net/bigint/
> http://sourceforge.net/projects/cpp-bigint/
But as you have to implement it yourself, I think this isn't the solution ...
Thanks a lot. So if stringcomp() comes back as true I can use the if statement? Very helpful thanks again.
An if-statement evaluates a condition, and a condition can be TRUE or FALSE ...
Thus, everything what return true/false or something comparable can be evaluated using an if-statement ...
Hope this helps !
You can compare strings using the if-statement:
e.g:
string password = "password123";
string userinput = "lalala";
if(userinput == password)
{
/* The strings are equal */
/* REMARK: the evaluation is case-sensitive !! */
}
thats what i meant when i said arrays as a string is an array of chars :)..
A c-string is an array of chars, but this algorithm is also working for C++-strings ...
Maybe you should try to implement the following algorithm:
E.g: 6! = 1*2*3*4*5*6
But: 6! = (1/10) * (2/10) * (3/10) * (4/10) * (5/10) * (6/10) *(10^6)
In that case you can already calculate factorials of very high numbers as you can simply do the following:
> 6! = 0.1*0.2*0.3*0.4*0.5*0.6 = 0.00072
> Now you convert this number to a string
> Now you shift your result (in the string) by 6 times (= the number where you're calculating the factorial of) e.g:
0.00072 (before you started shifting)
0.0072 (first shift)
0.072
0.72
7.2
72
[U]720[/U] (sixth shift) ==> Now you've found the factorial of 6 !
> Now you've your factorial (in a string)
REMARK: If you're working with C-strings you shouldn't forget to reserve enough memory ...
the problem is in handling the big numbers
If I'm not wrong you mean: The factorials are becoming too big to store in a standard C++ datatype ?
Is that right?
Well i use StRiNg so i don't get confused with any other string or String class already created... I made a file class and called it FiLe.
plus i like the look to StRiNg XD but that doesn't have much to do w/ this. I'm marking this as solved because i found my answer already. Thanks for all your help.
OK, I understand your way to do it, as I said it's only a matter of choice ...
It could be that you've to write your own function (which can handle strings) to multiply two numbers, in that case you can calculate factorials of very big numbers ...
Wouldnt it be much more sensible to make a linked list
That's also an option, but:
"Define a class Student that ...
need to input ID and display? how to?
This is an example of a 'dog' class I wrote:
#include <iostream>
#include <string>
using namespace std;
class dog {
string name;
int age;
public:
string get_name() {return name;}
int get_age() {return age;}
dog(string his_name);
dog(string his_name, int his_age);
dog(){};
void set_name(string his_name) {name = his_name;}
void set_age(int his_age) {age = his_age;}
void disp_details();
};
dog::dog(string his_name)
{
name = his_name;
}
dog::dog(string his_name, int his_age)
{
name = his_name;
age = his_age;
}
void dog::disp_details()
{
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
int main()
{
int number_of_dogs = 0;
string name;
int age = 0;
cout << "Please specify the number of available dogs in the shop: ";
cin >> number_of_dogs;
dog * doggies = new dog[number_of_dogs];
/* Create a dog database */
cout << endl << "DOG-DATABASE INPUT" << endl;
cout << "~~~~~~~~~~~~~~~~~~" << endl << endl;
for(int i = 0; i < number_of_dogs; i++)
{
cout << "Please type dog number " << i+1 << "'s name: ";
cin >> name;
doggies[i].set_name(name);
cout << "Please type dog number " << i+1 << "'s age: ";
cin >> age;
doggies[i].set_age(age);
cout << endl;
}
/* Disp dog database */
cout << endl << "DOG-DATABASE DISPLAY" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~" << endl << endl;
for(int i = 0; i < number_of_dogs; i++)
{
doggies[i].disp_details();
cout << endl;
}
/* Delete our dog database */
delete[] doggies;
return 0; …
Oh, sorry I made a mistake on line 24: you have to replace dec(ss);
by ss.clear();
...
(Assuming the order of the elements will never be changed)
> You could read the file line by line ...
(and every time you put the line into a string)
> Then you only have to scan the string for the character '=' and read out the value after it ...
Hope this helps !
Why are you using such weird naming like: "StRiNg"
??
Why don't you just call it 'mystring', 'mystrlen', etc. or something ?
It will be much easier to type ...
Unless you don't care by everytime type an UPPERCASE character, and after it a lowercase character ...
It's your choice ...
Could you please post using code tags ?
BTW, using goto
is a bad practice in C++ ...
Try at least doing some effort, post the code you have so far ...
You'll never learn to program if you aren't writing programs yourself ...
What is the FTP server? Please tell me the key of the Renames a file or directory on the FTP server, Changes the current directory to the parent directory on the FTP server?
What are you asking for?
I think you're asking for the following:
> What's an FTP-server ? (Just use Google or look at this link ...)
> How can I change to another directory using PHP ?
> How can I rename a directory using PHP?
(Look here)
Is that what you're asking for ?
Actually I don't get what you're saying ...
So if you have a question can you please reformulate it so we can understand what you mean ?
You can make use of string streams to do the conversion:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
stringstream ss;
int integer;
string s;
integer = 500;
/* Convert an integer to a hexadecimal string */
hex(ss);
ss << integer;
ss >> s;
cout << "Integer: " << integer << endl;
cout << "Hexadeximal: " << s << endl << endl;
/* Convert a hexadecimal string to an integer */
dec(ss);
ss << s;
ss >> integer;
cout << "Hexadeximal: " << s << endl;
cout << "Integer: " << integer << endl;
return 0;
}
It's only an example and you'll have to adjust it to your needs ...
To make the sum of two hexadecimals you could do the following:
> Convert them both to decimal base ...
> Add those two integers ...
> Convert them back to hexadecimal base ...
> If you want to know if there was a so-called 'overflow' you only have to save the result in a string, and use the length()
-method to check the number of characters, if it's higher than 10 you print out a message there was an 'OVERFLOW' ...
> You can use the width
manipulator to set the output width (= of the result of the addition) to 10 characters ...
Use Google to find some information about stringstreams ...
Hope this helps !
Oh, I didn't notice it but you're absolutely right ...
Where you allocate your pointer, notice something wrong with new's "type"?
By the way, you should be checking if you allocation was successful or not!
int *p; try{ p = new int [5]; if(!p) throw; } catch(...) { emergency_handling(); }
You could also check it by using the following code:
if(p == NULL)
{
/* The memory wasn't allocated */
}
But MosaicFuneral's code is superior if you want to use error-handling ...
Look at the comments in the code to see what I've changed ...
#include <iostream>
#include <string> /* In your class you've functions which are returning string values */
using namespace std;
class student
{
public:
student();
void setid(string a);
void setname(string b);
string getid();
string getname();
private:
string id;
string name;
};
/* IF YOU DEFINE A CONSTRUCTOR, YOU ALSO HAVE TO CREATE HIM */
student::student()
{
/* Constructor */
}
int main()
{
cout << "input number of students";
int numStudents;
cin >> numStudents;
student * students = new student[numStudents];
/* YOURS WAS: student * students = new students[numStudents]; */
delete[] students;
system ("PAUSE");
return 0;
}
Edit:: (Try 'student' instead of 'students' ==> probably won't work ...)
Can you please post the whole code you're trying to compile ?
True Image can backup your OS and programs and even though you upgrade your OS you can still restore all your programs onto the new OS ?
For that purpose I actually would recommend you using Genie Backup ...
student * students = new students(numStudents);
Has to be: student * students = new students[numStudents];
with [] brackets instead of the () ones ...
First of all you need a pointer to declare a dynamic array ...
Let's say we want a dynamic array which consists of integers:
int *ptr_to_array;
If you want a dynamic array of type 'double', you'll have to declare a pointer of that type instead of 'int', it's analog for the other types ...
In your case you might want to declare a pointer of type 'student', as it was in your assignment ...
After that we can dynamically assign memory to the pointer by using new[] :
ptr_to_array = new int[number_of_elements];
(where number_of_elements needs to be replaced by the number of elements you want to store into the array (duh;)))
When you don't need the dynamic array anymore you shouldn't forget to release the assigned memory by using delete[] :
delete[] ptr_to_array;
/* This will not delete the pointer, only the memory assigned to it... */
Hope this helps !!!
Nope ...
which will only run on PC's with .NET framework 2.0
Wrong !
In Microsoft Visual C++ 2008 you've the choice between a stand-alone application or one which relies on .NET ...
Look at this video: http://msdn.microsoft.com/en-gb/beginner/bb964629.aspx
Edit:: Sorry I didn't read the post carefully before I replied ...
Yeah, but you need that header file to compile your code, sure it isn't listed somewhere in the book?
Otherwise you won't be able to compile this example ...
thanks ddanbe, but i = i + 1 is the same as i++ is that correct?
Yes, that's absolutely right !
And where can I find the file: 'mystack.h' ?
Could you please post your code ?
No man, I am using VS 2008 and the executable file does not run by itself. Even if i use it, it runs and then after entering the input, it vanishes instantaneously. However, from command prompt it works.
Add cin.get();
at the end of your application ...
So how exactly do I do that?
Right hand click on "Header Files" folder, add New Item, choose .h or .cpp and what do I put in here?
Choose '.h' and then add your header file: 'mystack.h' ...