将数组放入函数中并且数组的奇数不起作用

时间:2016-07-26 22:30:19

标签: c arrays function

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void calculate(float *payperhour, float *hoursworked, float *wage, float *overtime,int p)
{
if (&hoursworked[p] > 40)
    overtime[p] = ((hoursworked[p] - 40) * payperhour[p] * 1.5);
    wage[p] = overtime[p] + (payperhour[p] * 40);
if (hoursworked[p] <= 40)
    overtime[p] = 0;
    wage[p] = hoursworked[p] * payperhour[p];
}



void main(void)
{
int i = 0;
char employee[5][10];
float payperhour[10];
float hoursworked[10];
float wage[10];
float overtime[10];
int p = 0;
// establish the variables needed to run the formulas for pay




for (i = 0; i <= 4; i)
{
    printf("Give me an Employees name.\n");
    scanf_s(" %[^\n]s %d", &employee[i], 10);
    if (strcmp(employee[i], "-1") == 0)
        break;
    printf("What is this Employees wage per hour?\n");
    scanf_s("%f", &payperhour[i]);
    if (payperhour[i] == -1)
        break;
    printf("How many hours did this employee work?\n");
    scanf_s("%f", &hoursworked[i]);
    if (hoursworked[i] == -1)
        break;
    i++;
}
//for loop that assigns all the variables needed to figure out pay
for (p = 0; p <= 4; p)
{
    calculate(&payperhour[p], &hoursworked[p], &wage[p], &overtime[p], p);
    p++;
}
//actualy equation that computes all the pay that everyone is receiving
for (p = 0; p < i; p)
{
    printf("Employee %s:\n", employee[p]);
    printf("Pay per hour:\n %.2f\n", payperhour[p]);
    printf("Hours Worked:\n %.2f\n", hoursworked[p]);
    printf("Gross Pay:\n %.2f\n", wage[p]);
    printf("Overtime pay:\n %.2f\n", overtime[p]);
    printf("Pay after taxes:\n %.2f\n", wage[p] * .8);
    p++;
    //finishes the application by giving the user the information that was computed
}
system("pause");
}

这是我的代码我目前正在进行C编程并且一直坚持这个问题。当您将用户输入阵列点0(在所有阵列上)时,它可以正常工作。但是,如果您将信息输入现场1或3,则不会为该信息产生适当的工资或加班费。我无法弄清楚为什么它可以在偶数数组上工作,但不能在偶数数字上工作。

[1]的工资和加班费总是以数百万的数字出现,但[0]的工资和加班时间正常工作

ps我知道代码甚至不是完美的,但我正在努力解决这个问题,并且不能为我的生活弄清楚任何事情或获得有关我所遇到的问题的任何信息。

我已编辑过包含要求

的全部代码

3 个答案:

答案 0 :(得分:2)

您将&payperhour[p]作为payperhour传递,然后使用payperhour[p]

请注意,(&payperhour[p])[p]payperhour[p+p]payperhour[p*2]相同。

看起来你想要调用这样的函数:

calculate(payperhour, hoursworked, wage, overtime, p);

答案 1 :(得分:1)

你在这里混淆了一些不同的东西。首先,调用循环:

for (p = 0; p <= 4; p)
{
    calculate(&payperhour[p], &hoursworked[p], &wage[p], &overtime[p], p);
    p++;
}

此步骤介绍各个人,并将其详细信息传递给calculate(...)。无需通过p - 您已经使用它来从阵列中获取数据。请注意,p++应移至for的最后部分:

for (p = 0; p <= 4; p++)
{
    calculate(&payperhour[p], &hoursworked[p], &wage[p], &overtime[p]);
}

跳过条目的问题是因为您在呼叫中应用[p]之后重复calculate(...)。所以calculate(...)需要完全重写 - 不是逻辑,而是变量引用:

void calculate(float *payperhour, float *hoursworked, float *wage, float *overtime)
{
    if (*hoursworked > 40)
        *overtime = ((*hoursworked - 40) * *payperhour * 1.5);
    *wage = *overtime + (*payperhour * 40);
    if (*hoursworked <= 40)
        *wage = *hoursworked * *payperhour;
    *overtime = 0;
}

但是请注意,一些传入的变量仅用作计算的输入,而其他变量则用于输出。你应该区分两者:只传递&#34;仅输入&#34;的值。变量,以及&#34;输入/输出&#34;的指针变量的地址变量:

void calculate(float payperhour, float hoursworked, float *wage, float *overtime)
{
    if (hoursworked > 40)
        *overtime = ((hoursworked - 40) * payperhour * 1.5);
    *wage = *overtime + (payperhour * 40);
    if (hoursworked <= 40)
        *wage = hoursworked * payperhour;
    *overtime = 0;
}

for (p = 0; p <= 4; p++)
{
    calculate(payperhour[p], hoursworked[p], &wage[p], &overtime[p]);
}

注意我也改变了你的缩进 - 也许你需要围绕{语句的大括号(}if)?

答案 2 :(得分:1)

由于calculate读取数组的p条目,因此应传递数组的基址。它应该被调用为:

calculate(payperhour, hoursworked, wage, overtime, p);

循环中的语法令人困惑,可能不正确:

for (p = 0; p <= 4; p) {
    calculate(&payperhour[p], &hoursworked[p], &wage[p], &overtime[p], p);
    p++;
}

如果数组包含4元素,则应使用i < 4停止循环,因为有效索引值为012和{ {1}}。同时在3语句中增加索引,而不是在循环体的末尾:

for

此外,for (p = 0; p < 4; p++) { calculate(payperhour, hoursworked, wage, overtime, p); } 中还存在主要问题:您的帖子缩进是假的。正确的缩进看起来像这样:

calculate

这是不正确的,void calculate(float *payperhour, float *hoursworked, float *wage, float *overtime, int p) { if (hoursworked[p] > 40) overtime[p] = ((hoursworked[p] - 40) * payperhour[p] * 1.5); wage[p] = overtime[p] + (payperhour[p] * 40); if (hoursworked[p] <= 40) wage[p] = hoursworked[p] * payperhour[p]; overtime[p] = 0; } 在进入函数时不一定是overtime,并且在返回之前不应强制0

该函数应以这种方式编写:

0

编辑:重新阅读代码后,在调用序列和函数中对索引进行索引解释观察到的行为:您只访问偶数索引,如由immibis解释。应用上面解释的更正来纠正这两个错误,程序应该正常工作。