Jump to content

Need Fast Help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
14 replies to this topic

#1
JP16.1470

JP16.1470

    Newbie

  • Members
  • Pip
  • 8 posts
Given two integers A and B, find their exact sum.

This time there are no limitations on A and B (of course, they will fit in memory).

Im stuck on this question and really need to know how to do it.

#2
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
what do you mean by exact sum?
if its like normal one just declare three variables two for input and one for answer

#3
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
Use strings. Store each number in a string, then add them together the way you do it on paper.
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

#4
JP16.1470

JP16.1470

    Newbie

  • Members
  • Pip
  • 8 posts
aeresnaa would you show an example please!! because i dont know how i could do tht. Thx

#5
nullbyte

nullbyte

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
#include <iostream>
using namespace std;
int main()
{
int a, b;
float c = (a + b);
cin << a;
cout << "\n";
cin << b;
cout << c;
return 0;
}
Is this what you want?

Edited.

Edited by nullbyte, 12 November 2008 - 11:21 PM.


#6
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
The spec says integers.
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
We don't do homework for people. The phrase "exact sum" implies something other than the standard result for addition, but it's not clear how.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
I think it means bignum.
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

#9
JP16.1470

JP16.1470

    Newbie

  • Members
  • Pip
  • 8 posts
it does mean bignum.

#10
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,712 posts
Input strings, then convert each digit into a number, then add them right to left, making sure to divide to catch the digit carry.

"12345" + "0492513":
'3'-'0' = 3 (int)
'5'-'0' = 5 (int)
3-5 =  8
'0' + 8 = '8' (char)

EXAMPLE: "9" + "13"
extend to make them the same length --> "09"+"13"
start from right end:
int digitA = '3' - '0'; //cheap conversion trick
int digitB = '9' - '0';
int carry = (digitA + digitB) / 10;  //get overflow
int digitResult = (digitA + digitB) % 10;
char chResult = (char)digitResult + '0';


#11
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
Write clear code, then optimize.
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

#12
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,712 posts
Try pseudocode first, then clear code, then optimize. You'll get fewer bugs that way, at least in my experience.