Here is what I have so far... User is to enter 2 integers. One digit
at a time. After that I have to print out what the user entered, and
then add the two together. Example:
Enter First Integer (one digit at a time) //-1 ends it
1
2
3
-1
Enter Second Intger (one digit at a time) //-1 ends it
2
3
4
5
6
-1
You entered:
123
&
23456
The sum of these numbers is 23579
//I AM LOST TRYING TO ADD THE TWO TOGETHER....
HERE IS MY CODE.. THANKS FOR HELPING!!!
#include <iostream>
using namespace std;
void display(int x[], int n);
void main() {
int arr[100],num;
int i=0, cnt;
cout << "enter the digits of your first integer (one at a time)"
<<endl;
while(1) {
cin >> num;
if (num !=-1) {
arr[i]=num;
i++;
}
else {
break;
}
}
cnt=i;
int arr1[100],num2;
int j=0, cnt1;
cout << "enter the digits of your second integer (one at a
time)"<<endl;
while(1) {
cin >> num2;
if (num2 !=-1) {
arr1[j]=num2;
j++;
}
else {
break;
}
}
cnt1=j;
//display the results
cout << "you entered the numbers: "<<endl;
display(arr, cnt);
cout<<"and"<<endl;
display(arr1, cnt1);
}
void display(int x[], int n) {
int i;
for (i=0; i<n; i++) {
cout << x[i];
}
cout << "\n";
} 11 1444
"1111111111" <eh***@hotmail.com> wrote in message
news:3f**************************@posting.google.c om... Here is what I have so far... User is to enter 2 integers. One digit at a time. After that I have to print out what the user entered, and then add the two together. Example:
Enter First Integer (one digit at a time) //-1 ends it 1 2 3 -1 Enter Second Intger (one digit at a time) //-1 ends it 2 3 4 5 6 -1
Some quibbles. You didn't enter an integer, you entered three integers.
Entering 1 then 2 then 3 would require you to scale the integers you have
and add them together to get a single integer. (1 * 100 + 2 * 10 + 3 = 123).
You would need an algorithm to do that to get the final integer with your
"one-digit-at-a-time" entry method. (Start with a 0, and for each digit
entered multiply your integer by 10 and add the new digit.)
Of course, why not allow the user to enter the integer 123 as a single
entry? It's not clear what the assignment is, so it may have called for the
"one-digit-at-a-time" entry, but we don't know.
You entered: 123 & 23456 The sum of these numbers is 23579
//I AM LOST TRYING TO ADD THE TWO TOGETHER....
HERE IS MY CODE.. THANKS FOR HELPING!!!
#include <iostream> using namespace std; void display(int x[], int n);
void main() {
int main( ) {
please. int arr[100],num; int i=0, cnt; cout << "enter the digits of your first integer (one at a time)" <<endl; while(1) { cin >> num; if (num !=-1) { arr[i]=num; i++; } else { break;
} }
cnt=i;
int arr1[100],num2; int j=0, cnt1; cout << "enter the digits of your second integer (one at a time)"<<endl; while(1) { cin >> num2; if (num2 !=-1) { arr1[j]=num2; j++; } else { break; } } cnt1=j;
//display the results cout << "you entered the numbers: "<<endl; display(arr, cnt); cout<<"and"<<endl; display(arr1, cnt1);
}
void display(int x[], int n) { int i; for (i=0; i<n; i++) { cout << x[i]; } cout << "\n"; }
Well, you got the digits into arrays and printed out the whole array one
digit at a time. It makes it look like a single integer, but it isn't. And
that's why you have a problem adding the numbers together -- you havn't got
two numbers yet.
I'm afraid the whole design at this point is making the assignment too
cumbersome (although it still can be done by multiplying and adding the
digits). The best bet at this point is to rethink how an entire integer can
be read in using cin. Since you can read a single digit, why can't you read
several digits that are entered before the user hits Enter?
Think it over and try again. But you did an interesting job using arrays for
this.
--
Gary
* 1111111111: Here is what I have so far... User is to enter 2 integers. One digit at a time. After that I have to print out what the user entered, and then add the two together. Example:
Enter First Integer (one digit at a time) //-1 ends it 1 2 3 -1 Enter Second Intger (one digit at a time) //-1 ends it 2 3 4 5 6 -1 You entered: 123 & 23456 The sum of these numbers is 23579
//I AM LOST TRYING TO ADD THE TWO TOGETHER....
HERE IS MY CODE.. THANKS FOR HELPING!!!
#include <iostream> using namespace std; void display(int x[], int n);
void main() {
'main' must have return type 'int'.
If your compiler doesn't issue a warning or error it's broken.
The Visual C++ compiler is one example.
int arr[100],num; int i=0, cnt; cout << "enter the digits of your first integer (one at a time)"
You mean, "one per line".
<<endl; while(1) {
Better use 'for( ;; )' or else you risk a compilation warning.
Also, if you absolutely must use 'while', then 'while( true )'.
cin >> num; if (num !=-1) { arr[i]=num; i++;
Use ++i, not i++.
} else { break;
Why not simply 'if( num == -1 ) { break; }'?
Also, more serious, here you risk a _buffer overflow_ if the
user enters more than one hundred digits.
You should check for that, and that necessitates using a symbolic
constant for the array size. } }
cnt=i;
int arr1[100],num2; int j=0, cnt1; cout << "enter the digits of your second integer (one at a time)"<<endl; while(1) { cin >> num2; if (num2 !=-1) { arr1[j]=num2; j++; } else { break; } } cnt1=j;
Note that this is virtually the same code as before.
So this should be made into a function.
Also, not that array plus a count of elements is essentially what
a std::vector gives you, so do use a std::vector instead.
//display the results cout << "you entered the numbers: "<<endl; display(arr, cnt); cout<<"and"<<endl; display(arr1, cnt1);
Now, how to "add together" the numbers depends on whether you can
assume a limited range (so you can convert to integer and use the
built-in addition) or not.
If not, the use a loop that starts at the least significant digit,
adds those, and from the result computes the 0...9 range digit
and carry (if any). Next iteration of the loop, add the carry and
the next least significant digits. And so on.
}
void display(int x[], int n) { int i; for (i=0; i<n; i++) { cout << x[i]; } cout << "\n"; }
I'd structure that a little bit differently, as a function returning
a string, and with no newline char in it.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
> Now, how to "add together" the numbers depends on whether you can assume a limited range (so you can convert to integer and use the built-in addition) or not.
How would I go about doing this?????
"1111111111" <eh***@hotmail.com> wrote in message
news:3f**************************@posting.google.c om... Now, how to "add together" the numbers depends on whether you can assume a limited range (so you can convert to integer and use the built-in addition) or not.
How would I go about doing this?????
If you can do it on a piece of paper it would be a start. You can use a
similar method in your program.
When you add numbers on paper you have to make sure that the numbers line
up, so that you add ones to ones, tens to tens, hundreds to hundreds. If any
if the additions give you a number of 10 or more then you have to carry one
into the next column.
Does any of this sound familiar? It used to be taught to eight year olds but
maybe now that we have calculators etc. perhaps it isn't.
Implementing the above is actually a fairly simple for loop, about half a
dozen lines of code tops.
john
1111111111 writes: Now, how to "add together" the numbers depends on whether you can assume a limited range (so you can convert to integer and use the built-in addition) or not.
How would I go about doing this?????
People are guessing (necessarily) as to what your assignment is and what you
have coded so far. I suggest posting both of these to clarify what the
situation is. Are there any constraints on the user? Is he allowed to
start typing random gibberish? Only digits? How many digits? Be sure that
you yourself understand the difference between a digit and a number.
One answer to the question "how would I go about .." is: if you add two
positive numbers and the answer is negative the range for an int (or long)
has been exceeded. al***@start.no (Alf P. Steinbach) wrote in message news:<40****************@news.individual.net>...
<snip> <<endl; while(1) {
Better use 'for( ;; )' or else you risk a compilation warning. Also, if you absolutely must use 'while', then 'while( true )'.
cin >> num; if (num !=-1) { arr[i]=num; i++;
Use ++i, not i++.
</snip>
i++ appears on a line by itself, so whats the difference
between writing i++ and ++i ? Also, why should while(1)
produce a warning and not for(;;) ?
-Arijit
Arijit wrote: al***@start.no (Alf P. Steinbach) wrote in message news:<40****************@news.individual.net>...
<snip>
<<endl; while(1) {
Better use 'for( ;; )' or else you risk a compilation warning. Also, if you absolutely must use 'while', then 'while( true )'. cin >> num; if (num !=-1) { arr[i]=num; i++;
Use ++i, not i++.
</snip>
i++ appears on a line by itself, so whats the difference between writing i++ and ++i ? Also, why should while(1) produce a warning and not for(;;) ?
It's just a matter of keeping good habits. 'while (true)' is better
than 'while (1)' simply because 1 (an int) doesn't have to be converted
to 'bool'. Writing ++i instead of i++ is better because you're not
using the return value (the original value of 'i' before the increment)
anyway, so why bother getting it? Yes, for integral types it most likely
doesn't matter, but if you get into the habit of writing ++i instead of
i++, you will never run into trouble where it does matter.
As to "should produce a warning", it probably should not, but just like
with other "should" type of things in life, they rarely do what they
should (or what we think they should), and a well-known compiler does
give a warning when it probably shouldn't...
Just my $0.02...
V eh***@hotmail.com (1111111111) wrote in message news:<3f**************************@posting.google. com>... Now, how to "add together" the numbers depends on whether you can assume a limited range (so you can convert to integer and use the built-in addition) or not.
How would I go about doing this?????
My guess as to your assignment is you are trying to add two
arbitrarily large integers. In that case your program will
not work for more than 100 digit long numbers.
Here's a rough solution. Add a call to your display routine
as needed.
const int MAX = 100;
int main()
{
int arr1[MAX],arr2[MAX],arr3[MAX];
int i=0,j=0;
do
{ cin >> arr1[i]; }
while(arr1[i++]>=0);
do
{ cin >> arr2[j]; }
while(arr2[j++]>=0);
i-=2;
j-=2;
int k = (i>j) ? i : j;
arr3[++k] = 0;
for(;i>=0 && j>=0;--i,--j,--k)
{
arr3[k] += arr1[i] + arr2[j];
arr3[k-1] = arr3[k] / 10;
arr3[k] %= 10;
}
if(i<0)
for(;j>=0;--j,--k)
{
arr3[k] += arr2[j];
arr3[k-1] = arr3[k] / 10;
arr3[k] %= 10;
}
else
for(;i>=0;--i,--k)
{
arr3[k] += arr1[i];
arr3[k-1] = arr3[k] / 10;
arr3[k] %= 10;
}
return 0;
}
You should improve on my rough attempt by using vectors. i++ appears on a line by itself, so whats the difference between writing i++ and ++i ? Also, why should while(1) produce a warning and not for(;;) ?
++i:
i += 1;
i++:
int temp = i;
return temp;
++i;
Turning the latter into the former would be an optimization. If you going to
depend on things like that, you may aswell throw the inline keyword out the
window too.
Victor Bazarov <v.********@comAcast.net> wrote: Arijit wrote: al***@start.no (Alf P. Steinbach) wrote: Better use 'for( ;; )' or else you risk a compilation warning. Also, if you absolutely must use 'while', then 'while( true )'.
why should while(1) produce a warning and not for(;;) ?
Some compilers would give a warning along the lines that the
test-expression in the while loop is always true.
It's just a matter of keeping good habits. 'while (true)' is better than 'while (1)' simply because 1 (an int) doesn't have to be converted to 'bool'.
Why should it be converted to bool? Doesn't the standard say that
while(x)
is equivalent to
while ((x) == 0)
? And the result of a == b is an int, either 0 or 1.
Even if you are right and it should be converted to bool, why
not write "while(1)" ? "1" converted to bool could never be "false".
This is a compile-time conversion, and it's probably slower to
read 4 chars from input than it is to "convert" 1 to true, which
I doubt would actually be an operation anyway !
"Old Wolf" <ol*****@inspire.net.nz> wrote in message
news:84**************************@posting.google.c om... Victor Bazarov <v.********@comAcast.net> wrote: It's just a matter of keeping good habits. 'while (true)' is better than 'while (1)' simply because 1 (an int) doesn't have to be converted to 'bool'. Why should it be converted to bool? Doesn't the standard say that while(x) is equivalent to while ((x) == 0) ? And the result of a == b is an int, either 0 or 1.
Don't you mean "while((x) != 0)"?
Even if you are right and it should be converted to bool, why not write "while(1)" ? "1" converted to bool could never be "false". This is a compile-time conversion, and it's probably slower to read 4 chars from input than it is to "convert" 1 to true, which I doubt would actually be an operation anyway !
What? The difference in compile time would be negligible even on the
slowest modern machine. I think the real question is "What helps
readability?", in which case using a bool is definitely superior. However,
when it comes right down to it, it doesn't really matter. If the warning
bothers you, you can use a flag to turn it off in your makefile, right? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: shang |
last post by:
Hi!
I am trying to find a function that converts a string into a double
or a long double number. I found atod() but I don't know which library
I have to include to use it. Could someone help me?
...
|
by: Shapper |
last post by:
Hello,
I have this code in Global.asax:
Sub Session_Start(Sender As Object, E As EventArgs)
Dim cookie As HttpCookie = Request.Cookies("MyCookie")
If Not cookie Is Nothing Then...
|
by: Amy |
last post by:
Hi,
I have 6 If Then Else statements I was supposed to write. I did so but I
know that they have to be wrong because they all look the same. Could
someone take a look at them and point me in the...
|
by: shapper |
last post by:
Hello,
I created a div with margin and padding and a fixed width.
It seems the behavior is different in Firefox and IE.
I am not sure but it seems:
True width = width + paddingORmargin
OR...
|
by: Bart |
last post by:
Could someone explain me what is wrong with this code ?
I gives me a compile error:
Error 1 Use of unassigned local variable 'fileStreamObject' C:\Documents and
Settings\Bart\Local...
|
by: Greatness |
last post by:
#include <iostream>
void sizeYear(double,double,double ,int);
using namespace std;
int main()
{
double population;
|
by: hanaa |
last post by:
Hi
I am making a test taker. I need to put up a countdown timer, and I haven't a clue how. Could someone please help me start. I google it and all I see is readymade scripts that can be downloaded....
|
by: shapper |
last post by:
Hello,
I have two tables: Polls and Options:
Poll PollID, Question
Options OptionID, PollID, Answer
I want to select a Poll given its ID and all Options associated to
it.
Options should...
|
by: Lovro Mirnik |
last post by:
Hello,
I've been working with Dates today and the entire thing baffles my mind. I'm looking for an explanation of this simple code.
DateTime date1 = new DateTime(2008, 6, 1, 7, 47, 0);...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |