# Which Algorithm To Use?



## Munki (Nov 6, 2009)

Say I have a number that is "x" int long. That I want to divide it by a number that as equally as long.

x == "14"


So what algorithm should I use with C++?


If you need further information, please ask.


Thanks,



   Chris


----------



## W1zzard (Nov 6, 2009)

umm .. give an example ?


----------



## Munki (Nov 6, 2009)

In a C++ Calculator I want to divide 65,000,654,999,000 by 43,998,897,987,888


I wouldnt be asking such a out of the blue question if I wasnt trying to prepare for my next school year. Computer Science requires me to learn to program. Thought I would get a jump on it. Ive tinkered with it before, but nothing as serious as this is going to be.


----------



## W1zzard (Nov 6, 2009)

convert to double and divide? or do you need more precision ?


```
const char a[]="65000654999000";
const char b[]="43998897987888";

double c=atof(a)/atof(b);
```

c=1.4773246142867795;

ok that's standard c, not c++, but the basic q is do you want to convert and divide or do you want to divide the "strings" ? in that case a good start would be the same approach how you divide numbers on paper


----------



## 3870x2 (Nov 6, 2009)

wow w1zz, you are very hardcore.  I dont even have a C compiler on this machine! pretty much nothing before 1973 on here


----------



## Munki (Nov 6, 2009)

W1zzard said:


> convert to double and divide? or do you need more precision ?
> 
> 
> ```
> ...




Well obviously im a n00b, because I used single quote for a int where I needed to use a double quote in my OP. I noticed you turned my numbers into ASCII then, ASCII into Float, im still trying to process how the computer will see it. Like what "65000654999000" would look in ASCII.


----------



## W1zzard (Nov 6, 2009)

"65000654999000" looks like "65000654999000" .. thats what a string is .. when you put it into "

single quote defines a single character: char ch='x';

other in-memory representations, educational, you will probably never need to know why it looks like that

float a=65000654999000;






double a=65000654999000;





uint64 a=65000654999000;


----------



## Munki (Nov 6, 2009)

W1zzard said:


> "65000654999000" looks like "65000654999000" .. thats what a string is .. when you put it into "
> 
> other in-memory representations, educational, you will probably never need to know why it looks like that
> 
> ...





Okay when in integer form its just the number, period. How would I go about changing char ' 65000654999000' ?


----------



## W1zzard (Nov 6, 2009)

you can not create a single ascii character that contains 65000654999000 .. a single character is just a single character, not several characters


----------



## Munki (Nov 6, 2009)

W1zzard said:


> you can not create a single ascii character that contains 65000654999000 .. a single character is just a single character, not several characters




I understand, but could you create an array of char to form said num?


----------



## Kreij (Nov 6, 2009)

He did that in post #4 using 

const char[] a = "123456789"

The brackets mean the variale or constant (in this case 'a') is an array.


----------



## Munki (Nov 6, 2009)

Kreij said:


> He did that in post #4 using
> 
> const char[] a = "123456789"
> 
> The brackets means it is an array.





ASCII is a set chart of conversions? Im a complete n00b here, but im going to have to bite the bullet and try to learn even if im asking questions that have no meaning. I wanted to make a C++ calculator that did 14 digit numbers. 


So when he did Const Char a[] = " 12345678 "

Char is referring to ASCII. ASCII does not contain such a number.


65000654999000 is a 6 byte number.

and W1zzard used a double which can handle up to 8 Bytes correct?

and the 



double c=atof(a)/atof(b);

double c == answer?

atof == ASCII to Float

So in this case the ASCII code is 'a' for "65000654999000"
and 'b' is for "43998897987888"

and the \ is telling it to divide Array 'a' by array 'b'?


Can someone verify this for me?


----------



## Kreij (Nov 6, 2009)

When you enclose something in double quotes (like "1234") it takes it as a string literal, not the value of the numbers. So so the computer sees "1234" as ascii 49,50,51 & 51.

When you send a string into a character array it simply breaks the string into it's respective characters.

char[] A = "1234"

So A[0] will be equal to ascii 49 and visually represented as the number 1
A[1] will be ascii 50 etc. etc.

In order to do mathematical calculations on the string literal you have to cast it back to a number.

That is what atof(string) did. atof means ascii to floating point number.


----------



## Munki (Nov 6, 2009)

Kreij said:


> When you enclose something in double quotes (like "1234") it takes it as a string literal, not the value of the numbers. So so the computer sees "1234" as ascii 49,50,51 & 51.
> 
> When you send a string into a character array it simply breaks the string into it's respective characters.
> 
> ...



Im slowly understanding this, im sure its taking my longer than it would take a mentally ill patient, but its going. So what your telling me is that when I call Char its going to break "1234" into "1" "2" "3" "4" *ASCII* and if I just call Int "1234" It sees it as one number?


Example for verification:


Const Char A[] -= "1234"


Char A[0] = 1 *ASCII*
Char A[1] = 2
Char A[2] = 3
Char A[3] = 4



Back on in about 30 mins.


----------



## Kreij (Nov 6, 2009)

Munki said:


> Im slowly understanding this, im sure its taking my longer than it would take a mentally ill patient, but its going. So what your telling me is that when I call Char its going to break "1234" into "1" "2" "3" "4" *ASCII* and if I just call Int "1234" It sees it as one number?
> 
> 
> Example for verification:
> ...



Remember that A[0] is NOT the ascii code 1 (which would be SOH "Start Of Heading"), but the ascii character '1' (which has an ascii code of 49)


----------



## Munki (Nov 7, 2009)

Kreij said:


> Remember that A[0] is NOT the ascii code 1 (which would be SOH "Start Of Heading"), but the ascii character '1' (which has an ascii code of 49)



So, don't use char A[0]

?


----------



## Kreij (Nov 7, 2009)

No. When you doing calculations you will have to do them in a type that is numerical (int, float, double). When you are doing input and output you will need to display them in a string or character format. You will simply have to convert between them to accomplish what you want.

So for instance let say you ask the user for two numbers. They type in 10 and 20 on the keyboard. This will be in string format "10" and "20"

Convert them to numeric format. 
Int a = atoi("10")
Int b = atoi("20")

Add them together...
Int c = a + b

output the result

char[] output = itoa(c)

You must learn the basic types of the language and how they are used and converted.
It's not hard, it will just take some studying.

As always, we are here to help.


----------



## Oliver_FF (Nov 7, 2009)

For anyone interested in doing really really really big arithmetic...

There is a library out there called "The GNU Multiple Precision Arithmetic Library" which lets you perform arithmetic on arbitrary length numbers 

You can do cool stuff like:

```
mpz_t n;
mpz_init_set_str (n, "184764827574746458753962934628956823764982346829348264826583465893465389465262894689326583946589346583465834", 10);

mpz_t pie;
mpz_init_set_str (pie, "3141592653589793238462643383279502884", 10)

// n = n - pie;
mpz_sub (n, n, pie);
```


----------



## TheMailMan78 (Nov 7, 2009)

WOW! There is so much math in this thread it makes my butthole pucker!


----------



## Munki (Nov 8, 2009)

Well I am trying, last night I didnt come back to TPU because I had my head in a C++ book that deprived me of $35  Sure hope it pays off....im working on a calculator that uses this as I type this.


----------



## Munki (Nov 8, 2009)

Okay well, im about to install my compiler and see how well my first program works. Wish me luck, God knows im going to need it.


----------



## <<Onafets>> (Nov 8, 2009)

Good luck..wanna learn C++ too so I can make some hardcore programms and send viruses to those bitches in year 6. 3 More years to year 10, 3 More years to year 10, 3 More years to year 10...


----------



## Munki (Nov 8, 2009)

<<Onafets>> said:


> Good luck..wanna learn C++ too so I can make some hardcore programms and send viruses to those bitches in year 6. 3 More years to year 10, 3 More years to year 10, 3 More years to year 10...



What in the hell are you talking about? 



BTW the program worked  Just a bit messy. Gotta do some cleanup.


----------



## TheMailMan78 (Nov 8, 2009)

Munki said:


> Well I am trying, last night I didnt come back to TPU because I had my head in a C++ book that deprived me of $35  Sure hope it pays off....im working on a calculator that uses this as I type this.



I guess you are doing better than I ever will. If someone had my children for ransom and asked me to solve any of those things yall were talking about I would have no idea where to start. However Ill paint the hell out of a landscape.


----------



## Kreij (Nov 8, 2009)

Having a book for reference will come in handy, Munki. It's easier that always having to search through the net for answers for basics.

As soon as you get some code going, post it here so we can take a look at it and help you out. 

PS. I have no idea what Onafets is talkiing about either. lol


----------



## Munki (Nov 8, 2009)

Kreij said:


> Having a book for reference will come in handy, Munki. It's easier that always having to search through the net for answers for basics.
> 
> As soon as you get some code going, post it here so we can take a look at it and help you out.
> 
> PS. I have no idea what Onafets is talkiing about either. lol





Well, I did it made a simple calculator, and then amazingly deleted it (perminatly) 


ALTHOUGH! Im going to redo it anyways just to give me more practice, and then ill post the code up.


----------



## andrewsmc (Nov 8, 2009)

Reading all this just makes me feel stupid!


----------



## Munki (Nov 9, 2009)

Okay so I just did a simple code. It wasnt compiled or debuged. Im copy and paste straight from my script.


```
#include <iostream>
#include <cstdlib>

using namespace STD;

int main()

{
  double num;
  double num2;
  char input;
  For (;;)
  { 
  
  Do
   {
  
  COUT << "Welcome to Th3 C41c";
  COUT << "To begin Choose what function you would like to preform from the list provided for you";
  COUT << "1 - Addition";
  COUT << "2 - Subtraction";
  Cout << "3 - Multiplication";
  Cout << "4 - Divide";
  Cin >> input
  }
  while ( input > 1 || input < 4 );
  { Case '1';
          Cout << "Please enter your first number";
          Cin >> num;
          Cout << "Please enter your second number";
          cin >> num2;
          Cout << num + num2;
    Case '2';
          Cout << "Please enter your first number";
          Cin >> num;
          Cout << "Please enter your second number";
          Cin >> num2;
          Cout << num - num2;
   Case '3';
          Cout << "Please enter your fist number";
          Cin >> num;
          Cout << "please enter your second number";
          cin >> num2;
          Cout << num * num2;
   Case '4';
          Cout << "Please enter your first number";
          Cin >> num;
          Cout << "Please enter your second number";
          Cin >> num2;
          Cout << num / num2;
          }
Return 0;
}
```


----------



## Kreij (Nov 9, 2009)

You have a couple of problems.

First, you while statment is only looping on 2 and 3.
You want to fo something like (input >= 1 && input <= 4) so you include 1 and 4

You will also need a switch statement in order to use the case statements

```
switch (input)
{
    Case '1':
    .....
}
```

Notice the case statement ends with a colon, not a semicolon.


----------



## Munki (Nov 9, 2009)

Kreij said:


> You have a couple of problems.
> 
> First, you while statment is only looping on 2 and 3.
> You want to fo something like (input >= 1 && input <= 4) so you include 1 and 4
> ...




Oh okay.Well there wasn't as many mistakes as I thought there was going to be. haha. I fully understand everything but the For loop portion of my mistake. Im going to fix that and try to figure that out. For my first real code, it wasnt that bad.


----------



## Munki (Nov 9, 2009)

Sorry for the double post, but this is irrealivant to what the previous post said and if i were to edit, no one would see it as they have already looked at it without this.


Can I buy a complier? If so, Where? 

(p.s. I just made another calculator that does Square Root. When Visual Studio gets done downloading i will test and post if anyone wants to see it.)


----------



## Kreij (Nov 9, 2009)

You can download Microsoft's C++ express for free. 
http://www.microsoft.com/Express/VC/


----------



## Munki (Nov 9, 2009)

Kreij said:


> You can download Microsoft's C++ express for free.
> http://www.microsoft.com/Express/VC/



Well I just downloaded the Microsoft Visual Studio 2010 Beta 2. Do you feel one is better than the other?


----------



## Kreij (Nov 10, 2009)

In all honesty, Munki, at the level you are programming any version will do fine.
No offense intended, but you have to start somewhere and you are off to a great start !!!
I think that you find using the Integrated Development Environment (IDE) that the Visual Studio products integrate will make you life much easier.
Don't worry about what compiler/IDE is better, work on learning using the language you are working with, and in particular, learn the basics very well. 

Keep the questions coming.


----------



## Munki (Nov 10, 2009)

Kreij said:


> In all honesty, Munki, at the level you are programming any version will do fine.
> No offense intended, but you have to start somewhere and you are off to a great start !!!
> I think that you find using the Integrated Development Environment (IDE) that the Visual Studio products integrate will make you life much easier.
> Don't worry about what compiler/IDE is better, work on learning using the language you are working with, and in particular, learn the basics very well.
> ...





No offense taken (I know its true )


I built another script that does, Addition, Subtraction, Multiplication, Division and square root. I'm getting errors that i cant find the error. I ran debugger but I do not understand why its like that, I go back to the line(s) and it appears right to me. Ill access my external and post up the code in case you have time time to check it out.


```
#include <iostream>
#include <math>
#include <cstdlib>

using namespace STD;

int main ()

{
 Double num;
 Double num2;
 Char input;
 For (;;)
{ 
 Cout << " Please select your function. ";
 COut << " 1 - Addition ";
 Cout << " 2 - Subtraction";
 Cout << " 3 - Multiplication ";
 Cout << " 4 - Division ";
 Cout << " 5 - Square Root:;
}
While ( input <= '1' && input >= '5' )
Switch (input)
{
 Case '1':
	Cout << " Please input your first number ";
	Cin >>  num;
	Cout << " Please input your second number ";
	Cin >> num2;
	Cout << num + num2;
 Case '2':
	Cout << " Please input your first number ";
	Cin >> num;
	Cout << " Please input your second number ";
	Cin >> " num2;
	Cout << num - num2; 
 Case '3':
	Cout << "Please input your first number ";
	Cin >> num;
	Cout << "Please input your second number ";
	cin >> num2;
	Cout num * num2;
 Case '4':
	Cout << " Please input your first number ";
	Cin >> num;
	Cout << " Please input your second number ";
	Cin >> num2;
	Cout << num / num2;
 Case '5':
	Cout << " Please input the number you want to find the square root of. ";
	Cin >> num;
	if ( num < 0 )
{
 	Cout << "Number is negative. ";
}
	Else
{ 
	num2 = sqrt(num);
	Cout << num2;
}
Return 0;
}
```


I'm going to continue to try to locate this error.


----------



## Kreij (Nov 10, 2009)

```
While ( input <= '1' && input >= '5' )
```

This is backward ... should be (input >= '1' && input <= '5')

Also in case '3'  The output should be cout << num * num2; (You are missing the '<<')

What error are you getting ?


----------



## Munki (Nov 10, 2009)

Well for the most part it was claiming that COUT, and CIN were not called (initiated)


My compiler is installing on this computer. I wish I could install it on a jump drive so I didn't have to install it on 7 computers.


Im going to add some 'breaks' throughout the code to keep it from leaking through. I fixed my less than and greater than error. As well as my COUT error. Im going to end the forever loop, seeing as to how I blankly failed to do so. And finally added my Switch statement within the brackets.


----------



## Munki (Nov 10, 2009)

Isnt 
#include <iostream> suppose to declare COUT and CIN?


----------



## Kreij (Nov 10, 2009)

If you are using Visual C++ add #include "stdafx.h" above the iostream include.

Don't capitalize cout.


----------

