10035 - Primary Arithmetic
Moderator: Board moderators
any ideas what is wrong with this
[cpp]
#include<iostream>
using namespace std;
int count_carries(unsigned long a, unsigned long b) {
int carry = 0;
int digita = 0;
int digitb = 0;
int counter = 0;
while(a != 0 || b!=0) {
digita = a % 10;
digitb = b % 10;
a = a/10;
b = b/10;
if(digita + digitb + carry > 9) {
carry = 1;
counter++;
}
else
carry = 0;
}
return counter;
}
int main() {
unsigned long a;
unsigned long b;
int total_carry = 0;
cin>>a>>b;
while(a != 0 || b !=0) {
if(total_carry = count_carries(a, b))
if(total_carry == 1)
cout<<total_carry<<" carry operation."<<endl;
else
cout<<total_carry<<" carry operations."<<endl;
else
cout<<"No carry operation."<<endl;
cin>>a>>b;
}
return 0;
}
[/cpp]
[/cpp]
#include<iostream>
using namespace std;
int count_carries(unsigned long a, unsigned long b) {
int carry = 0;
int digita = 0;
int digitb = 0;
int counter = 0;
while(a != 0 || b!=0) {
digita = a % 10;
digitb = b % 10;
a = a/10;
b = b/10;
if(digita + digitb + carry > 9) {
carry = 1;
counter++;
}
else
carry = 0;
}
return counter;
}
int main() {
unsigned long a;
unsigned long b;
int total_carry = 0;
cin>>a>>b;
while(a != 0 || b !=0) {
if(total_carry = count_carries(a, b))
if(total_carry == 1)
cout<<total_carry<<" carry operation."<<endl;
else
cout<<total_carry<<" carry operations."<<endl;
else
cout<<"No carry operation."<<endl;
cin>>a>>b;
}
return 0;
}
[/cpp]
[/cpp]
10035
Can someone tell to me what is wrong with this code?
[cpp]
int count_carries(unsigned long a, unsigned long b) {
int carry = 0;
int digita = 0;
int digitb = 0;
int counter = 0;
while(a != 0 || b!=0) {
digita = a % 10;
digitb = b % 10;
a = a/10;
b = b/10;
if(digita + digitb + carry > 9) {
carry = 1;
counter++;
}
else
carry = 0;
}
return counter;
}
int main() {
unsigned long a;
unsigned long b;
int total_carry = 0;
cin>>a>>b;
while(a != 0 || b !=0) {
if(total_carry = count_carries(a, b))
if(total_carry == 1)
cout<<total_carry<<" carry operation."<<endl;
else
cout<<total_carry<<" carry operations."<<endl;
else
cout<<"No carry operation."<<endl;
cin>>a>>b;
}
return 0;
}
[\cpp][/cpp]
[cpp]
int count_carries(unsigned long a, unsigned long b) {
int carry = 0;
int digita = 0;
int digitb = 0;
int counter = 0;
while(a != 0 || b!=0) {
digita = a % 10;
digitb = b % 10;
a = a/10;
b = b/10;
if(digita + digitb + carry > 9) {
carry = 1;
counter++;
}
else
carry = 0;
}
return counter;
}
int main() {
unsigned long a;
unsigned long b;
int total_carry = 0;
cin>>a>>b;
while(a != 0 || b !=0) {
if(total_carry = count_carries(a, b))
if(total_carry == 1)
cout<<total_carry<<" carry operation."<<endl;
else
cout<<total_carry<<" carry operations."<<endl;
else
cout<<"No carry operation."<<endl;
cin>>a>>b;
}
return 0;
}
[\cpp][/cpp]
-
- Learning poster
- Posts: 94
- Joined: Wed Jul 31, 2002 12:44 pm
- Location: Dacca, Bangladesh
- Contact:
Few checks
consider this line
if(total_carry = count_carries(a, b))
if i compile it with Turbo C, i get a warning message,
Possibly incorrect assignment
although this may not be the reason, try midifying ur code so there is no warning.
if(total_carry = count_carries(a, b))
if i compile it with Turbo C, i get a warning message,
Possibly incorrect assignment
although this may not be the reason, try midifying ur code so there is no warning.
why WA?
already modified it twice still WA......
[c]
#include<stdio.h>
#include<string.h>
void main()
{ char a[10],b[10];
int i,res,temp,t1,t2,p1,p2;
do{
scanf("%s %s",a,b);
temp=0;
i=0;
p1=strlen(a)-1;
p2=strlen(b)-1;
if((a[0]==48 && b[0]==48) && (!p1 && !p2)) break;
while(p1>=0 || p2>=0)
{ if(p1>=0) t1=a[p1]-48;
else if(p1<0) t1=0;
if(p2>=0) t2=b[p2]-48;
else if(p1<0) t2=0;
res=t1+t2+temp;
if(res<10)
{ temp=0;
}
else
{ temp=res/10;
i++;
}
p1--;
p2--;
}
if(i)
{ if(i==1)
printf("%d carry operation.\n",i);
else
printf("%d carry operations.\n",i);
}
else
{ printf("No carry operation.\n");
}
}while(1);
}
[/c]
already modified it twice still WA......
[c]
#include<stdio.h>
#include<string.h>
void main()
{ char a[10],b[10];
int i,res,temp,t1,t2,p1,p2;
do{
scanf("%s %s",a,b);
temp=0;
i=0;
p1=strlen(a)-1;
p2=strlen(b)-1;
if((a[0]==48 && b[0]==48) && (!p1 && !p2)) break;
while(p1>=0 || p2>=0)
{ if(p1>=0) t1=a[p1]-48;
else if(p1<0) t1=0;
if(p2>=0) t2=b[p2]-48;
else if(p1<0) t2=0;
res=t1+t2+temp;
if(res<10)
{ temp=0;
}
else
{ temp=res/10;
i++;
}
p1--;
p2--;
}
if(i)
{ if(i==1)
printf("%d carry operation.\n",i);
else
printf("%d carry operations.\n",i);
}
else
{ printf("No carry operation.\n");
}
}while(1);
}
[/c]
wa again......
I wonder why it's wrong?
did I miss something?
[c]
#include<stdio.h>
#include<string.h>
void main()
{ char a[10],b[10];
int i,res,temp,t1,t2,p1,p2;
do{
scanf("%s %s",a,b);
temp=0;
i=0;
p1=strlen(a)-1;
p2=strlen(b)-1;
if((a[0]==48 && b[0]==48) && (!p1 && !p2)) break;
while(p1>=0 || p2>=0)
{ if(p1>=0) t1=a[p1]-48;
else if(p1<0) t1=0;
if(p2>=0) t2=b[p2]-48;
else if(p1<0) t2=0;
res=t1+t2+temp;
if(res<10)
{ temp=0;
}
else
{ temp=res/10;
i++;
}
p1--;
p2--;
}
if(i)
{ if(i==1)
printf("%d carry operation.\n",i);
else
printf("%d carry operations.\n",i);
}
else
{ printf("No carry operation.\n");
}
}while(1);
}
[/c]
I wonder why it's wrong?
did I miss something?
[c]
#include<stdio.h>
#include<string.h>
void main()
{ char a[10],b[10];
int i,res,temp,t1,t2,p1,p2;
do{
scanf("%s %s",a,b);
temp=0;
i=0;
p1=strlen(a)-1;
p2=strlen(b)-1;
if((a[0]==48 && b[0]==48) && (!p1 && !p2)) break;
while(p1>=0 || p2>=0)
{ if(p1>=0) t1=a[p1]-48;
else if(p1<0) t1=0;
if(p2>=0) t2=b[p2]-48;
else if(p1<0) t2=0;
res=t1+t2+temp;
if(res<10)
{ temp=0;
}
else
{ temp=res/10;
i++;
}
p1--;
p2--;
}
if(i)
{ if(i==1)
printf("%d carry operation.\n",i);
else
printf("%d carry operations.\n",i);
}
else
{ printf("No carry operation.\n");
}
}while(1);
}
[/c]
10035 - Primary Arithmetic
What is wrong ?plz help
Thnx
suman.
Code: Select all
[c]
#include<stdio.h>
void reverse(char *s)
{
int c,j,i;
for( i = 0, j = strlen(s)-1 ; i < j ; i++, j-- ) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
int main()
{
char no[2][12];
int i,len[2],lower,flag,cnt;
while ( scanf("%s %s",no[0],no[1]) == 2 ) {
len[0] = strlen(no[0]);
len[1] = strlen(no[1]);
if ( !strcmp(no[0],"0") && !strcmp(no[1],"0") )
exit(0);
reverse(no[0]);
reverse(no[1]);
lower = len[0] > len[1] ? len[1] : len[0];
flag = 0;
cnt = 0;
for ( i = 0 ; i < lower ; i++ ) {
if ( no[0][i]-'0' + no[1][i]-'0' + flag > 9 ) {
cnt++;
flag = 1;
}
}
if ( flag == 1 ) {
if ( lower == len[0] ) {
while ( lower++ < len[1] )
if ( no[1][lower-1]-'0' + flag > 9 ) {
cnt++;
flag = 1;
}
}
else {
while ( lower++ < len[0] )
if ( no[0][lower-1]-'0' + flag > 9 ) {
cnt++;
flag = 1;
}
}
}
if ( cnt )
printf("%d carry operation%s.\n",cnt,cnt>1?"s":"");
else printf("No carry operation.\n");
}
return 0;
}
[/c]
suman.
10035
great !
finally something to work on,
i revised the code many times ...
Thanks a hell of a lot!!!
suman.
finally something to work on,
i revised the code many times ...
Thanks a hell of a lot!!!
suman.
10035:Carry
Shamim
Thanks a million tonnes
I finally got AC
Suman
PS:i didnt chk when there was no carry
Thanks a million tonnes
I finally got AC

Suman
PS:i didnt chk when there was no carry
-
- New poster
- Posts: 21
- Joined: Sun Sep 14, 2003 11:44 pm
- Location: USA
- Contact:
10035 Primary Arithmetic (WA)
I could swear I have this correct!! I've tested all sticky test cases and haven't come across an error so far.
If someone could help me out, I would really appreciate it. I know I'm really close. My code is really inefficient, but I just want to get the problem solved
[cpp]
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
long *setone; int len1;
long *settwo; int len2;
long *setfinal; int a;
long temp;
long x,y;
cin >> x >> y;
while(x != 0 || y != 0){
setone = new long[9];
settwo = new long[9];
setfinal = new long[10];
//X Length
len1 = 1;
if(x==0){
goto divide1;
}
temp = pow(10,len1);
a = 0;
if(temp==x){
x += 1;
a = 1;
}
while(temp / x < 1){
len1++;
temp = pow(10,len1);
if(temp==x){
x += 1;
a = 1;
}
}
if(a==1)
x -= 1;
divide1:
//Y Length
len2 = 1;
if(y==0){
goto divide2;
}
temp = pow(10,len2);
a = 0;
if(temp==y){
y += 1;
a = 1;
}
while(temp / y < 1){
len2++;
temp = pow(10, len2);
if(temp==y){
y += 1;
a = 1;
}
}
if(a==1)
y -= 1;
divide2:
//Split up X
int b = len1 - 1;
int modulus = 1;
while(b >= 0){
if(b == 0){
setone[8-b] = x;
break;
}
temp = pow(10,b);
modulus = x % temp;
x = x / pow(10, b);
setone[8-b] = x;
x = modulus;
b--;
}
for(int w = 8; w - len1 >= 0; w--)
setone[w-len1] = 0;
//Split Y
int c = len2 - 1;
modulus = 1;
while(c >= 0){
if(c == 0){
settwo[8-c] = y;
break;
}
temp = pow(10,c);
modulus = y % temp;
y = y / pow(10, c);
settwo[8-c] = y;
y = modulus;
c--;
}
for(int p = 8; p - len2 >= 0; p--)
settwo[p-len2] = 0;
//Combine the two elements
for(int t = 9; t > -1; t--){
setfinal[t] = 0;
}
int j=9;
int shiftremainder = 0;
setfinal[0] = 0;
while(j >= 0){
if(j != 9 && setfinal[j] != 0){
setfinal[j] += (setone[j-1] + settwo[j-1]);
}
else if(j!=9 && setfinal[j] == 0){
setfinal[j] = setone[j-1] + settwo[j-1];
}
else if(j==9){
setfinal[j] = setone[j-1] + settwo[j-1];
}
if(setfinal[j] > 9){
setfinal[j] = setfinal[j] - 10;
if(j>=1)
setfinal[j-1] = 1;
shiftremainder++;
}
j--;
}
if(shiftremainder == 0)
cout << "No carry operation." << endl;
if(shiftremainder == 1)
cout << "1 carry operation." << endl;
if(shiftremainder > 1)
cout << shiftremainder << " carry operations." << endl;
delete [] setfinal;
delete [] setone;
delete [] settwo;
cin >> x >> y;
}
return 0;
}
[/cpp]
If someone could help me out, I would really appreciate it. I know I'm really close. My code is really inefficient, but I just want to get the problem solved

[cpp]
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
long *setone; int len1;
long *settwo; int len2;
long *setfinal; int a;
long temp;
long x,y;
cin >> x >> y;
while(x != 0 || y != 0){
setone = new long[9];
settwo = new long[9];
setfinal = new long[10];
//X Length
len1 = 1;
if(x==0){
goto divide1;
}
temp = pow(10,len1);
a = 0;
if(temp==x){
x += 1;
a = 1;
}
while(temp / x < 1){
len1++;
temp = pow(10,len1);
if(temp==x){
x += 1;
a = 1;
}
}
if(a==1)
x -= 1;
divide1:
//Y Length
len2 = 1;
if(y==0){
goto divide2;
}
temp = pow(10,len2);
a = 0;
if(temp==y){
y += 1;
a = 1;
}
while(temp / y < 1){
len2++;
temp = pow(10, len2);
if(temp==y){
y += 1;
a = 1;
}
}
if(a==1)
y -= 1;
divide2:
//Split up X
int b = len1 - 1;
int modulus = 1;
while(b >= 0){
if(b == 0){
setone[8-b] = x;
break;
}
temp = pow(10,b);
modulus = x % temp;
x = x / pow(10, b);
setone[8-b] = x;
x = modulus;
b--;
}
for(int w = 8; w - len1 >= 0; w--)
setone[w-len1] = 0;
//Split Y
int c = len2 - 1;
modulus = 1;
while(c >= 0){
if(c == 0){
settwo[8-c] = y;
break;
}
temp = pow(10,c);
modulus = y % temp;
y = y / pow(10, c);
settwo[8-c] = y;
y = modulus;
c--;
}
for(int p = 8; p - len2 >= 0; p--)
settwo[p-len2] = 0;
//Combine the two elements
for(int t = 9; t > -1; t--){
setfinal[t] = 0;
}
int j=9;
int shiftremainder = 0;
setfinal[0] = 0;
while(j >= 0){
if(j != 9 && setfinal[j] != 0){
setfinal[j] += (setone[j-1] + settwo[j-1]);
}
else if(j!=9 && setfinal[j] == 0){
setfinal[j] = setone[j-1] + settwo[j-1];
}
else if(j==9){
setfinal[j] = setone[j-1] + settwo[j-1];
}
if(setfinal[j] > 9){
setfinal[j] = setfinal[j] - 10;
if(j>=1)
setfinal[j-1] = 1;
shiftremainder++;
}
j--;
}
if(shiftremainder == 0)
cout << "No carry operation." << endl;
if(shiftremainder == 1)
cout << "1 carry operation." << endl;
if(shiftremainder > 1)
cout << shiftremainder << " carry operations." << endl;
delete [] setfinal;
delete [] setone;
delete [] settwo;
cin >> x >> y;
}
return 0;
}
[/cpp]
-
- New poster
- Posts: 21
- Joined: Sun Sep 14, 2003 11:44 pm
- Location: USA
- Contact:
10035 Primary Arithmetic (WA)
I could swear I have this correct!! I've tested all sticky test cases and haven't come across an error so far.
If someone could help me out, I would really appreciate it. I know I'm really close. My code is really inefficient, but I just want to get the problem solved
[cpp]
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
long *setone; int len1;
long *settwo; int len2;
long *setfinal; int a;
long temp;
long x,y;
cin >> x >> y;
while(x != 0 || y != 0){
setone = new long[9];
settwo = new long[9];
setfinal = new long[10];
//X Length
len1 = 1;
if(x==0){
goto divide1;
}
temp = pow(10,len1);
a = 0;
if(temp==x){
x += 1;
a = 1;
}
while(temp / x < 1){
len1++;
temp = pow(10,len1);
if(temp==x){
x += 1;
a = 1;
}
}
if(a==1)
x -= 1;
divide1:
//Y Length
len2 = 1;
if(y==0){
goto divide2;
}
temp = pow(10,len2);
a = 0;
if(temp==y){
y += 1;
a = 1;
}
while(temp / y < 1){
len2++;
temp = pow(10, len2);
if(temp==y){
y += 1;
a = 1;
}
}
if(a==1)
y -= 1;
divide2:
//Split up X
int b = len1 - 1;
int modulus = 1;
while(b >= 0){
if(b == 0){
setone[8-b] = x;
break;
}
temp = pow(10,b);
modulus = x % temp;
x = x / pow(10, b);
setone[8-b] = x;
x = modulus;
b--;
}
for(int w = 8; w - len1 >= 0; w--)
setone[w-len1] = 0;
//Split Y
int c = len2 - 1;
modulus = 1;
while(c >= 0){
if(c == 0){
settwo[8-c] = y;
break;
}
temp = pow(10,c);
modulus = y % temp;
y = y / pow(10, c);
settwo[8-c] = y;
y = modulus;
c--;
}
for(int p = 8; p - len2 >= 0; p--)
settwo[p-len2] = 0;
//Combine the two elements
for(int t = 9; t > -1; t--){
setfinal[t] = 0;
}
int j=9;
int shiftremainder = 0;
setfinal[0] = 0;
while(j >= 0){
if(j != 9 && setfinal[j] != 0){
setfinal[j] += (setone[j-1] + settwo[j-1]);
}
else if(j!=9 && setfinal[j] == 0){
setfinal[j] = setone[j-1] + settwo[j-1];
}
else if(j==9){
setfinal[j] = setone[j-1] + settwo[j-1];
}
if(setfinal[j] > 9){
setfinal[j] = setfinal[j] - 10;
if(j>=1)
setfinal[j-1] = 1;
shiftremainder++;
}
j--;
}
if(shiftremainder == 0)
cout << "No carry operation." << endl;
if(shiftremainder == 1)
cout << "1 carry operation." << endl;
if(shiftremainder > 1)
cout << shiftremainder << " carry operations." << endl;
delete [] setfinal;
delete [] setone;
delete [] settwo;
cin >> x >> y;
}
return 0;
}
[/cpp]
If someone could help me out, I would really appreciate it. I know I'm really close. My code is really inefficient, but I just want to get the problem solved

[cpp]
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
long *setone; int len1;
long *settwo; int len2;
long *setfinal; int a;
long temp;
long x,y;
cin >> x >> y;
while(x != 0 || y != 0){
setone = new long[9];
settwo = new long[9];
setfinal = new long[10];
//X Length
len1 = 1;
if(x==0){
goto divide1;
}
temp = pow(10,len1);
a = 0;
if(temp==x){
x += 1;
a = 1;
}
while(temp / x < 1){
len1++;
temp = pow(10,len1);
if(temp==x){
x += 1;
a = 1;
}
}
if(a==1)
x -= 1;
divide1:
//Y Length
len2 = 1;
if(y==0){
goto divide2;
}
temp = pow(10,len2);
a = 0;
if(temp==y){
y += 1;
a = 1;
}
while(temp / y < 1){
len2++;
temp = pow(10, len2);
if(temp==y){
y += 1;
a = 1;
}
}
if(a==1)
y -= 1;
divide2:
//Split up X
int b = len1 - 1;
int modulus = 1;
while(b >= 0){
if(b == 0){
setone[8-b] = x;
break;
}
temp = pow(10,b);
modulus = x % temp;
x = x / pow(10, b);
setone[8-b] = x;
x = modulus;
b--;
}
for(int w = 8; w - len1 >= 0; w--)
setone[w-len1] = 0;
//Split Y
int c = len2 - 1;
modulus = 1;
while(c >= 0){
if(c == 0){
settwo[8-c] = y;
break;
}
temp = pow(10,c);
modulus = y % temp;
y = y / pow(10, c);
settwo[8-c] = y;
y = modulus;
c--;
}
for(int p = 8; p - len2 >= 0; p--)
settwo[p-len2] = 0;
//Combine the two elements
for(int t = 9; t > -1; t--){
setfinal[t] = 0;
}
int j=9;
int shiftremainder = 0;
setfinal[0] = 0;
while(j >= 0){
if(j != 9 && setfinal[j] != 0){
setfinal[j] += (setone[j-1] + settwo[j-1]);
}
else if(j!=9 && setfinal[j] == 0){
setfinal[j] = setone[j-1] + settwo[j-1];
}
else if(j==9){
setfinal[j] = setone[j-1] + settwo[j-1];
}
if(setfinal[j] > 9){
setfinal[j] = setfinal[j] - 10;
if(j>=1)
setfinal[j-1] = 1;
shiftremainder++;
}
j--;
}
if(shiftremainder == 0)
cout << "No carry operation." << endl;
if(shiftremainder == 1)
cout << "1 carry operation." << endl;
if(shiftremainder > 1)
cout << shiftremainder << " carry operations." << endl;
delete [] setfinal;
delete [] setone;
delete [] settwo;
cin >> x >> y;
}
return 0;
}
[/cpp]