使用这些精确的方程式使此代码工作 [英] Make this code work using these exact equations

查看:68
本文介绍了使用这些精确的方程式使此代码工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

void main(void)
{
	printf("Name and Date: Matthew Adair, January 20, 2017 \n");
	printf("Course and Section: ENGR 19700 and Class #24743 \n");
	printf("Problem: Adair_temp5_M.c C Programming Homework 5 \n");
	printf("Statement: Program is to print information with \n");

	double TF_, TK_, TR_;


	printf("What is the temperature in Farenheit? \n");
	scanf("%Lf", &TF_);
	TF_ = TR_ - 459.67;
	TR_ = (9 / 5)*TK_;
	printf("The Kelvin equivalent to the given temperature in Farenheit is %Lf \n", TK_);


	

}





我有什么尝试过:



我觉得它应该可行但是我一直在运行时检查失败#3 - T



What I have tried:

I feel like it should work but I keep getting a runtime check failure #3 - T

推荐答案

*阅读评论后更新*

*Updated after reading comment*
double TF_, TK_, TR_;
printf("What is the temperature in Farenheit? \n");
scanf("%Lf", &TF_); //Read TF
TF_ = TR_ - 459.67; //Overwrite TF?
TR_ = (9 / 5)*TK_;
printf("The Kelvin equivalent to the given temperature in Farenheit is %Lf \n", TK_);



在您的代码中,您正在阅读华氏温度然后用 TR_ - 459.67 覆盖。为什么甚至首先输入TF?此外,这些方程式对于printf语句所说的内容仍然不正确。华氏对兰金是 R = F + 459.67 而兰金对开尔文是 K = R *(5/9)。你的方程试图从朗肯转换为华氏,然后从开尔文转换为兰金。这是固定代码:


In your code, you are reading in the temperature in Fahrenheit then overwriting that with TR_ - 459.67. Why even input TF in the first place? Also, these equations are still incorrect for what your printf statements say is happening. Fahrenheit to Rankine is R = F + 459.67 and Rankine to Kelvin is K = R * (5/9). Your equations are attempting to convert from Rankine to Fahrenheit, then from Kelvin to Rankine. This is the fixed code:

double TF_, TK_, TR_;
printf("What is the temperature in Farenheit? \n");
scanf("%Lf", &TF_);
TR_ = TF_ + 459.67;
TK_ = (5/9) * TR_;
printf("The Kelvin equivalent to the given temperature in Farenheit is %Lf \n", TK_);



我还建议使用有用的变量姓名。


I'd also recommend using useful variable names.

double fahrenheit, kelvin, rankine;
printf("What is the temperature in Farenheit? \n");
scanf("%Lf", &fahrenheit);
rankine = fahrenheit + 459.67;
kelvin = (5/9) * rankine;
printf("The Kelvin equivalent to the given temperature in Farenheit is %Lf \n", kelvin);



快速注释: double variableName; 是变量声明。现在你有一个与变量相关的记忆。那个记忆现在可以保留任何东西。它可能是0;它可能是随机垃圾。您需要在使用之前分配(初始化)它。示例: variableName = 0; 或更紧凑 double variableName = 0;



它经常工作的原因是因为许多操作系统内核作为安全功能不会发出未初始化的内存。此外,许多语言规范都有默认值(尽管编译器是否遵守此规范可能会有所不同)。所有这些都可以改变,并且不受你的控制。你不应该依赖它,特别是在这种情况下避免半非确定性效应是如此简单。只需一个简单的 = 0;


Quick note: double variableName; is a variable declaration. Now you have memory associated with a variable. That memory can hold anything right now. It could be 0; it could be random junk. You need to assign (initialize) it before using it. Example: variableName = 0; or more compactly double variableName = 0;.

The reason it often works anyways is because many operating system kernels as a security feature do not give out uninitialized memory. Also many language specifications have defaults (though whether the compiler respects this can vary). All of this can change and is out of your control. You shouldn't rely on it especially when avoiding semi-non-deterministic effects is so easy in this case. Just a simple = 0;.


引用:

但是当我使用double TF_,TK_,TR_;时,无论如何都不是初始化的变量,就像我使用int时一样?

but anyways aren't the variables initialized when I use "double TF_, TK_, TR_;", same as they would be if I used "int"?



int float 用于分配内存,没有别的。

初始化变量意味着给它一个值。


int and float are used to allocate memory, nothing else.
Initializing a variable means giving it a value.

Quote:

在课堂上我们不使用调试器,

In class we don't use the debugger,

主要失败来自你的老师。

-----

当你不明白你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器查看代码正在执行的操作。它允许你逐行执行第1行并在执行时检查变量,它是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做的比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。

Major failure from your teacher.
-----
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于使用这些精确的方程式使此代码工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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