Reputation: 3423
i am trying to write a program which inputs two numbers from user as strings....if the first number is greater than the second number they are multiplied and the result is returned...i converted the input numbers into character arrays and then used ascii codes to convert the charcters into actual numbers...then i performed calculations on the numbers...here are the functions that i used...
double greater1(char a[],char b[],int size1,int size2)//the arrays here are those containing the two numbers
{ double first;
double second;
for(int i=0;i<size1;i++)
first=first+(pow(10.0,(double)(size1-i-1))*(a[i]-48));
for(int i=0;i<size2;i++)
second=second+(pow(10.0,(double)(size2-i-1))*(b[i]-48));
return(first>second?first:second);
}
double smaller1(char a[],char b[],int size1,int size2)
{ double first;
double second;
for(int i=0;i<size1;i++)
first=first+(pow(10.0,(double)(size1-i-1))*(a[i]-48));
for(int i=0;i<size2;i++)
second=second+(pow(10.0,(double)(size2-i-1))*(b[i]-48));
return(first<second?first:second);
}
double multiply(char a[], char b[],int size1,int size2)
{double first=greater1(a,b,size1,size2);
double second=smaller1(a,b,size1,size2);
//cout<<second;....(a)
//cout<<smaller1(a,b,size1,size2);....(b)
//cout<<smaller1(a,b,size1,size2);....(c)
//cout<<smaller1(a,b,size1,size2);....(d)
double mult=first*second;
return mult;
}
now to test these functions i input 43 and 10...but the product which was returned was 860...so to find the error i inserted the lines (a),(b),(c),(d)....(a) gave output 20,(b) gave 30 and after that all further lines (c),(d)... were giving output as 10...when i inserted
cout<<smaller1(arr1,arr2,ln1,ln2);//these parameters were the ones also passed to the above three functions
in main(), everytime i got the output 10....so there must be some problem with using smaller1() in multiply func...plz can anyone point out the problem
Upvotes: 0
Views: 118
Reputation: 9340
It's easy actually: you haven't initialized first and second in smaller1 and greater1. Local variables are never initialized implicitly.
Upvotes: 1