我正在编写一项作业(job),要求我做以下工作:
1. User input:十进制数字列表.(这些项目代表美元的数值,第一项是美元兑换比率,
(比方说1usd = 3.58another_currency个.)
- 将值转换为另一种货币(转换后的项目将以another list为单位)
2.1例如: 原文:[3.58 12 11.50 3.20 4.10] -> 转换:[(42.96),(41.17),.] (multiplied 3.58 by each item next)
- 以漂亮的表格形式显示它,表格的最后一行应该是the sum of the dollars和the sum of the converted currency
It seems that I have done the first step, and the second step correctly. But when calculating the sum of the other currency, I ran into a problem. Only the sum of the dollars happen to be correct, but the sum of the other currency is problematic. Not only it's incorrect, the sum is changing every time I run it. Green one is correct, red one is not correct.
我激活了same function upon the two different arrays,但它只正确计算了其中的一个和.
MY .H文件(doll.c):
#define SIZE 10
void dollar_to_shekel(float[], float[]);
void print_array(float[], float[]);
float sum_array(int, float[]);
我的.c文件(doll.c):
#include <stdio.h>
#include "doll.h"
void dollar_to_shekel(float usd[], float ils[])
{
int i;
for(i=1; i<SIZE; i++)
{
/*the first element in the received list, in index 0, is the dollar ratio*/
ils[i-1] = usd[i] * usd[0];
}
}
void print_array(float usd[], float ils[])
{
int i;
for(i=1; i<SIZE; i++)
{
printf("%.2f \t \t \t \t %.2f\n", usd[i], ils[i-1]);
}
}
float sum_array(int x, float arr[])
{
int i;
float sum = 0;
for(i=x; i<SIZE; i++)
{
sum = sum + arr[i];
}
return sum;
}
int main()
{
float usd[SIZE];
float ils[SIZE];
int i;
printf("Insert a list of numbers:");
for(i=0; i<SIZE; i++)
{
scanf("%f", &usd[i]);
}
dollar_to_shekel(usd, ils);
printf("$ \t \t \t \t IS\n");
print_array(usd,ils);
printf("%.2f \t \t \t \t %.2f\n", sum_array(1,usd), sum_array(0,ils));
return 0;
}
我真的不知道程序在哪里以及为什么在计算一列的总和时失败,但在另一列上做了这项工作.