GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 30-Mar-2011, 23:45
tiffan tiffan is offline
New Member
 
Join Date: Mar 2011
Posts: 8
tiffan is on a distinguished road
Question

Adding two integers


please help me with this question
I need to write a program that read two integers up to 30 digits each, add these integers together, and display the result.

Assume that your computer has the very limited capability of being able to read and write only single-integer digits and to add two integers consisting of one decimal digit each.
Hint: Store the two numbers in two int arrays of size 30, one digit per array element. If the number is less than 30 digits in length, enter enough leading zeros (to the left of the number) to make the number 30 digits long.
You will need a loop to add the digits in corresponding array elements. Don’t forget to handle the carry digit if there is one!


the code is only asking for the first integer and the second one
then it stops
no sum is shown
thanks in advance


CPP / C++ / C Code:
#include <iostream>
#include <conio.h>

using namespace std;

void inputValue1(long int[]);
void inputValue2(long int[]);
void Sum(long int[]);
const int SIZE = 30;
void main()
{
	long int val1[SIZE];
	long int val2[SIZE];
	long int total[SIZE]; 

	inputValue1(val1);
	inputValue2(val2);
	Sum(total); 

getch();
}

void inputValue1(long int val1[])
{
	cout << "Enter the first number: ";
	for(int i = 0; i < SIZE; i++)
		cin >> val1[i];
	return;
}

void inputValue2(long int val2[])
{
	cout << "Enter second integer: ";
	for(int i=0; i<SIZE; i++)
		cin >> val2[i];
	return;
}

void Sum(long int total[])
{
	long int i;
	long int val1[30],val2[30];
	
for(i<=SIZE;i>=0;i--)
if ((val2[i]+val1[i]) > 9)
{
	 val2[i-1] = val2[i-1] + 1;
}
total[i] = (val2[i]+val1[i]) % 10;

	cout << "The sum of these two integers is: " << total[i];
}

  #2  
Old 31-Mar-2011, 03:13
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,435
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: adding two integers


As I said in the other forum, use the hint and rewrite your input functions first.
__________________

Definition: Politics
Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
  #3  
Old 31-Mar-2011, 08:21
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 969
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Adding two integers


I'm guessing that the problem is that it takes the 30-digit number and puts the entire number into the first element of the array.

In order to get around this, try reading it into a string first then loop through each character in the string putting that character into the array.

Not that any of this matters because your Sum() function is not any good. You need to pass in your val1 and val2 arrays.
  #4  
Old 01-Apr-2011, 07:06
quneur quneur is offline
Junior Member
 
Join Date: Mar 2011
Posts: 59
quneur has a spectacular aura about

Re: Adding two integers


A few items which I see without compiling your code:

Besides the use of <conio.h> (old console i/o library), your sum function needs two arrays and return the sum array. Or all three needs to be global, which is not recommended. You have to decide whether to pass by address or pass by value.

If it was up to me, the program's purpose is to sum two numbers so I would eliminate the sum function and put it in main. Unless there are other program functions, such as; subtraction, multiplication, etc. why break it up for such a little program?

Why use longs? From what I understand, each array element has a value of 0-9 (base 10).

Your for loop in Sum function is all screwed up. Why do you have for(i <= SIZE;....)? ' i ' needs to be set to a value and not 'less than' a value. You are getting the output because the for loop is encompassing only the if statement. Watch your {}.
  #5  
Old 01-Apr-2011, 08:28
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Adding two integers


Quote:
rewrite your input functions first.
I know how irritating it is for some people to see double postings that ignore advice from one and then ask the same question somewhere else. Since I don't know what other forum you are referring to, I can't really say whether it was helpful. (I'm guessing that it could have been helpful if it had been heeded.)

However...

I do know that many people read threads on gidforums without ever asking questions, and they might benefit from your advice here. If you don't feel like repeating it, I understand. I mean, I just hate repeating myself. Really. I hate repeating myself.

Quote:
...
If it was up to me, the program's purpose is to sum two numbers so I would eliminate the sum function and put it in main.
Heck...why not just put everything in main?

Hint: it's an assignment, and it is not up to the Original Poster. Furthermore, if it were up to me, I would (probably) organize the project into separate functions so that each function did some particular thing that I could test, and then I would put them all together...kind of like the way that it is shown

My guess is that one of the purposes (maybe the main purpose) of the assignment is to let people learn about how functions work on arrays if they haven't done it before. That's something that the Original Poster needs help with. Maybe we should try. I mean, instead of just saying that he needs to rewrite the input function(s) or the Sum function, maybe we should try to help see how to get to the bottom of things.

To the Original Poster: So, let's start with the input functionality.

First of all, except for text in the user prompt, they do exactly the same thing! You do not need two of them; it's wasteful, and the fact that there are two different blocks of code doing essentially the same thing means that if debugging makes you see that you have to change one of them, you have to make sure that you make exactly the same change(s) in the other place and then keep testing both of them until you get it right. (And my guess is that you will have to repeat the testing more than a couple of times to get it right. At least, I usually do.)

Anyhow, here's my first concrete suggestion:
Make one function and call it twice. First time call it with the name of one array as the argument, and the second time call it with the name of the other array.

Furthermore, as you are doing program development I strongly recommend that you build the program one step at a time:

1. Make sure that your input function works. Test it. Test it a lot.

2. Then write the sum function and feed it values obtained from the input function calls. You could test the sum function separately, using fixed values in arrays, and maybe that would be appropriate, but you now know that the input functions present your intended data to the sum function. Test it. Test it a lot.

My main point is to separate the functionality (input and sum) in such a way that if testing tells you that there is a problem, it can point out where the problem is.


So, incorporating some of the very good suggestions from previous helpful hints, I might start with the following:
CPP / C++ / C Code:
#include <iostream>

using namespace std;

void getInputValue(int[]);

// Since we will be printing out everything for debug purposes
// start with a small number of items
const int SIZE = 5;

// Standard C++ requires that the return type of main() be an int
int main()
{
    // Start with one array.  Test the function until it returns with the right stuff in the array
    int val1[SIZE];

    // Since we won't have a separate function for separate inputs, we can put the prompt in main()
    // There are other ways, but I just wanted to start with something simple
    cout << "Enter values for the first integer." << endl;
    getInputValue(val1);
    cout << "In main, after return from inputValue():" << endl;
    for (int i = 0; i < SIZE; i++) {
        cout <<"val1[" << i << "] = " << val1[i] << endl;
    }
    return 0;
}

// This is not a very good way of doing a general input function
// since it only works with a fixed SIZE of array.  That's what
// you started with, so I'll leave it that way for now.  The first
// important thing is to make sure that it works the way you
// have in mind. 
//
void getInputValue(int x[])
{
    cout << "Entering inputValue()" << endl;
    for (int i = 0; i < SIZE; i++) {
        cout << "i = " << i << ": Enter an integer" << endl;
        cin >> x[i];
        cout << "For i = " << i << " you entered " << x[i] << endl;
    }
    cout << endl;
    cout << "Returning from inputValue()" << endl;
    return;
}


Now, test that and see how it goes.

Here's the drill:

You don't have to use the code that I showed, but whatever you do, make sure that the program is verbose enough to let you know exactly what you are doing at each step of the way.

If you have questions, then
  1. Post your code. The entire code.

  2. If there were compiler messages (errors or warnings) that you don't understand, then past the messages into your post. The exact messages (don't paraphrase). Tell us what compiler and operating system you are using. Sometimes it makes a difference to people who want to help.

  3. If the code compiles OK, then tell us how you tested it. Show us exactly the input that you gave it.

  4. If the program gave some output that you didn't expect, then tell us exactly what you expected and tell us what you don't understand about the difference.

So, for starters, start with the input function and show us how you tested it. (See Footnote.)

Here's my test run:
Code:
Enter values for the first integer. Entering inputValue() i = 0: Enter an integer 1 For i = 0 you entered 1 i = 1: Enter an integer 2 For i = 1 you entered 2 i = 2: Enter an integer 3 For i = 2 you entered 3 i = 3: Enter an integer 4 For i = 3 you entered 4 i = 4: Enter an integer 5 For i = 4 you entered 5 Returning from inputValue() In main, after return from inputValue(): val1[0] = 1 val1[1] = 2 val1[2] = 3 val1[3] = 4 val1[4] = 5

Regards,

Dave

Footnote:

With five-digit inputs, it's easy enough just to type them in, and I suggest that's may be a good way to get started. Make the program very verbose so that you can see exactly what is happening every step of the way. Test it. A lot.

But...

For the real assignment (30-digit integers) how are you going to test the program? Realizing that you are going to run it many times to be able to verify its operation (not to say the number of runs you might have to execute during debugging) are you going to redirect input from a file or what? I mean, if you just want to enter "12345" from a console window or from a line in a file, your function won't work. You can, perhaps, look back at fakepoo's suggestion and work from there.

Before I write the first line of code for any project, I devise a plan for testing. Always. Really.
__________________
Sometimes I just can't help myself...
Last edited by davekw7x : 01-Apr-2011 at 09:12.
  #6  
Old 06-Apr-2011, 18:21
tiffan tiffan is offline
New Member
 
Join Date: Mar 2011
Posts: 8
tiffan is on a distinguished road

Re: Adding two integers


thanks and i will try it
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
printing integers in graphics mode enggwaqas C Programming Language 4 04-Oct-2004 20:47
I cant believe this doesnt work: - basic printing of integers to screen davo C Programming Language 2 24-Jul-2004 21:50
Tutorial: Adding Music to your Site, WAV, MP3, Flash? abacomedia Web Design Forum 1 20-May-2004 15:10
Need help adding a footer demtro MySQL / PHP Forum 2 10-Feb-2004 19:48
Trouble adding RAM JBrewMan1 Computer Hardware Forum 13 29-Apr-2003 20:32

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:46.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.