函数中的数组

arrays in functions

本文关键字:数组 函数      更新时间:2023-10-16

我正试图编写一个程序,输入两个数字从用户作为字符串....如果第一个数字大于第二个数字,它们相乘并返回结果…我把输入的数字转换成字符数组,然后用ASCII码把字符转换成实际的数字…然后我对这些数字进行了计算……以下是我使用的函数…

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;
} 

现在测试这些函数,我输入43和10…但是被退回的产品是860…因此,为了找到错误,我插入了(a),(b),(c),(d)行....(a)给出了输出20,(b)给出了输出30,之后所有其他行(c),(d)…我们给出的输出是10…当我插入

cout<<smaller1(arr1,arr2,ln1,ln2);//these parameters were the ones also passed to the above three functions             

在main()中,每次我得到输出10....所以在乘法函数中使用smaller1()一定有一些问题…谁能指出这个问题

实际上很简单:您没有在smaller1和greater1中初始化first和second。局部变量永远不会隐式初始化。