函数中的数组
arrays in functions
我正试图编写一个程序,输入两个数字从用户作为字符串....如果第一个数字大于第二个数字,它们相乘并返回结果…我把输入的数字转换成字符数组,然后用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。局部变量永远不会隐式初始化。
相关文章:
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 打印 2D 动态数组 c++ 函数
- C++数组函数
- 在数组函数指针中调用void
- 数组函数似乎没有找到最大值
- 使用数组函数和循环求解伪元编码|C
- 在头文件中的函数原型中获取指针数组:函数声明为 void
- 数组函数错误输出
- 读取文件,然后创建数组函数C++
- 指针数组函数参数-奇怪的行为
- 二维数组 - 函数
- "return"没有从数组函数返回正确的值
- 数组函数问题
- 数组函数的大小返回 int 的大小而不是数组
- 从数组函数中删除元素
- 如何从C++中最好地调用具有C99风格数组函数签名的函数
- 追加/打印/插入数组c++函数问题
- 如何将数组函数转换为指针函数
- 多维数组函数输出垃圾
- C++:数组函数的递归最大值