c。与错误的答案 [英] c with the wrong answer

查看:139
本文介绍了c。与错误的答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

写一个程序分配的变化。用户输入支付的金额和到期金额。该计划确定的美元,宿舍,硬币,镍和便士应如何给出的变化。要求两个输入端的用户(到期金额和支付金额)在main(),然后把这些与指针一起5个参数(块钱,宿舍,硬币,镍,便士),以一个函数调用的变化(),由它来计算每个的数目得到的。从打印结果的main()。

所以我的计划是编译好的,但我得到错误的答案。我究竟做错了。

 的#include<&stdio.h中GT;
#包括LT&;&CONIO.H GT;//函数原型
无效的变化为(int * D,INT * Q,为int *迪,为int * N,INT * P,诠释支付,由于INT);
诠释的main()
{
    //变量
    INT支付;
    由于INT;
    INT美元;
    诠释宿舍;
    诠释角钱;
    诠释镍;
    INT便士;    //引用变量
    INT * D;
    INT * Q;
    INT *迪;
    INT * N;
    INT * P;    的printf(请输入到期金额:\\ n);
    scanf函数(%d个,&安培;因);    的printf(请输入支付金额:\\ n);
    scanf函数(%d个,&安培;支付);    变化(安培;元,&安培;宿舍,和放大器;助攻,和放大器;镍,和放大器;便士,支付,到期);    的printf(元=%d个\\ nquarters =%d个\\ ndimes =%d个\\ nnickels =%d个\\ npennies =%d个,美元,宿舍,硬币,镍,便士);    残培();
    返回0;
}无效的变化为(int * D,INT * Q,为int *迪,为int * N,INT * P,诠释支付,由于INT)
{
    INT dollarChange;
    INT quarterChange;
    INT dimeChange;
    INT nickelChange;
    INT pennyChange;
    INT MODU;
    INT modu2;
    INT modu3;
    INT总;
    INT penniestotal;    总=因 - 支付;    penniestotal =总* 100;    dollarChange = penniestotal / 100;
    MODU = penniestotal%100;
    quarterChange = MODU / 25;
    modu2 = MODU%25;
    dimeChange = modu2 / 10;
    modu3 = modu2%10;
    nickelChange = modu3 / 5;
    pennyChange = modu3 5%;
}


解决方案

更改()需要大量的指针作为参数,但这些指针永远不会在函数中使用。但也没有返回值。

这意味着什么你在做检查()其实很重要。

您正在阅读由于付费从输入,然后调用不要紧功能,然后打印大量的未初始化变量的输出。

Postscriptum:

您已经问五个问题,到目前为止,他们每个人遵循类似的模式:解释什么是你的程序应该做的这是行不通的一条线,和code转储

这不是计算器,或任何其他Q&放技能;一个网站,作品

您的问题在这里,例如,是不是计算变革,它的参数传递。就像你发布的所有其他问题,它可以想见,在code的十行pssed前$ P $,最大的。那你倾倒在我们的变化,而不是计算显示你一个不够努力,这就是为什么你不断收到downvotes。

Postscriptum 2:

我访问了您的其他问题。五分之四的问题基本一致:你没理解指针和参数传递怎样的工作。因为你的问题是如此含糊不清的,你得到的是所有的地方,这是不是非常有帮助的答案。然后你去试&安培;错误没有真正理解你在做什么,你的程序实际上得到的更糟的每个尝试...

所以,为您考虑,一个程序,做一些指点,并参数传递,做对,只有一个参数做它的时间,而且是相当冗长关于它在做什么。研究它,它真的回答了一口气你最后的四个问题:

 的#include<&stdio.h中GT;void函数(INT *参数)
{
    INT本地= 987;    的printf(中本地地址为:%p \\ n,&安培;地方);
    的printf(中本地值是:%d个\\ n \\ n,本地);    的printf(地址参数指的是:%P \\ N,参数);
    的printf(在那个地址值数:%d \\ n \\ n,*参数);    看跌期权(开关参数,指向本地...的\\ n);    为int * copy_of_parameter =参数;    参数=安培;地方;    的printf(解决'参数'现在指向为:%p \\ n,参数);
    的printf(在那个地址值数:%d \\ n \\ n,*参数);    看跌期权(通过'参数'\\ n改变本地的价值);    *参数= 0;    的printf(地址参数指的是:%P \\ N,参数);
    的printf(在那个地址值数:%d \\ n \\ n,*参数);    看跌期权(进行再次在老地址'参数'点...... \\ n);    参数= copy_of_parameter;    *参数= 666;    的printf(解决'参数'现在指向为:%p \\ n,参数);
    的printf(在那个地址值数:%d \\ n \\ n,*参数);    返回;
}
诠释的main()
{
    INT in_main = 123;    的printf('in_main'的地址为:%p \\ n,&安培; in_main);
    的printf(在那个地址值数:%d \\ n \\ n,in_main);    看跌期权(调用函数()... \\ n);    函数(安培; in_main);    看跌期权(从功能()... \\ n返回);    的printf('in_main'的地址为:%p \\ n,&安培; in_main);
    的printf(在那个地址值数:%d \\ n \\ n,in_main);    返回0;
}

Write a program to dispense change. The user enters the amount paid and the amount due. The program determines how many dollars, quarters, dimes, nickels, and pennies should be given as change. Ask the user for the two inputs (amount due and amount paid) in main() and then send these along with pointers to 5 parameters (dollars, quarters, dimes, nickels, pennies) to a function called change(), which will then calculate the number of each to give out. Print the results from main().

so my program is compiling good but am getting wrong answer. what am i doing wrong.

#include <stdio.h>
#include <conio.h>

//function prototype
void change( int *d, int *q, int *di, int *n, int *p, int paid, int due );


int main()
{
    //variables
    int paid;
    int due;
    int dollars;
    int quarters;
    int dimes;
    int nickels;
    int pennies;

    //reference variables
    int *d;
    int *q;
    int *di;
    int *n;
    int *p;

    printf( "Enter amount due: \n" );
    scanf( "%d", &due );

    printf( "Enter amount paid: \n" );
    scanf( "%d", &paid );

    change( &dollars, &quarters, &dimes, &nickels, &pennies, paid, due );

    printf( "Dollars = %d\nquarters = %d\ndimes =%d\nnickels = %d\npennies = %d", dollars, quarters, dimes, nickels, pennies );

    getch();
    return 0;
}

void change(int *d,int *q,int *di, int *n, int *p, int paid, int due )
{
    int dollarChange;
    int quarterChange;
    int dimeChange;
    int nickelChange;
    int pennyChange;
    int modu;
    int modu2;
    int modu3;
    int total;
    int penniestotal;

    total = due - paid;

    penniestotal = total * 100;

    dollarChange = penniestotal / 100;
    modu = penniestotal % 100;
    quarterChange = modu / 25;
    modu2 = modu % 25;
    dimeChange = modu2 / 10;
    modu3 = modu2 % 10;
    nickelChange = modu3 / 5;
    pennyChange = modu3 % 5;
}

解决方案

change() takes lots of pointers as arguments, but those pointers are never used in the function. There's also no return value.

Which means that nothing you do in check() actually matters.

You're reading due and paid from the input, then you call a function that doesn't matter, and then you print lots of uninitialized variables to output.

Postscriptum:

You've asked five questions so far, each of them following a similar pattern: Explaining what your program should do, a line of "it does not work", and a code dump.

That is not how StackOverflow, or any other Q&A site, works.

Your problem here, for example, is not "calculating change", it's "parameter passing". Like all other questions you posted, it could conceivably be expressed in ten lines of code, maximum. That you dumped your change calculation on us instead shows a lack of effort on your part, which is why you keep getting downvotes.

Postscriptum 2:

I visited your other questions. Four out of five questions are basically identical: You have not understood how pointers and parameter passing work. Since your questions are so nebulous, you get answers that are all over the place, which isn't very helpful. Then you went trial & error without really understanding what you're doing, and your programs actually got worse with each try...

So, for your consideration, a program that does some pointing-to and parameter passing, does it right, does it with only one parameter at a time, and is quite verbose about what it is doing. Study it, it really answers your last four questions in one go:

#include <stdio.h>

void function( int * parameter )
{
    int local = 987;

    printf( "Address of 'local' is:                     %p\n", &local );
    printf( "Value of 'local' is:                       %d\n\n", local );

    printf( "Address that 'parameter' points to is:     %p\n", parameter );
    printf( "Value at that address is:                  %d\n\n", *parameter );

    puts( "Switching 'parameter' to point to 'local'...\n" );

    int * copy_of_parameter = parameter;

    parameter = &local;

    printf( "Address that 'parameter' NOW points to is: %p\n", parameter );
    printf( "Value at that address is:                  %d\n\n", *parameter );

    puts( "Changing value of 'local' through 'parameter'...\n" );

    *parameter = 0;

    printf( "Address that 'parameter' points to is:     %p\n", parameter );
    printf( "Value at that address is:                  %d\n\n", *parameter );

    puts( "Making 'parameter' point at the old address again...\n" );

    parameter = copy_of_parameter;

    *parameter = 666;

    printf( "Address that 'parameter' NOW points to is: %p\n", parameter );
    printf( "Value at that address is:                  %d\n\n", *parameter );

    return;
}


int main()
{
    int in_main = 123;

    printf( "Address of 'in_main' is:                   %p\n", &in_main );
    printf( "Value at that address is:                  %d\n\n", in_main );

    puts( "Calling function()...\n" );

    function( &in_main );

    puts( "Returned from function()...\n" );

    printf( "Address of 'in_main' is:                   %p\n", &in_main );
    printf( "Value at that address is:                  %d\n\n", in_main );

    return 0;
}

这篇关于c。与错误的答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆