Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#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_);


	

}


What I have tried:

I feel like it should work but I keep getting a runtime check failure #3 - T
Posted
Updated 6-Feb-17 12:24pm
v2
Comments
[no name] 6-Feb-17 15:30pm    
I don't see how you would think this would work. You use uninitialized variables, you read a value from the user and then overwrite it with a calculation with an uninitialized variable, perform another calculation using an uninitialized variable and then finally print the value of an uninitialized variable that you didn't even calculate. Learning to use the debugger is also a valuable skill that you will need often.
Member 12985896 6-Feb-17 15:55pm    
In class we don't use the debugger, but anyways aren't the variables initialized when I use "double TF_, TK_, TR_;", same as they would be if I used "int"? The program works if I reverse the equations like "TF_ = TR_ - 459.67;" would be "TR_ = TF_ + 459.67;".
[no name] 6-Feb-17 17:14pm    
"we don't use the debugger", then your teacher is not doing his job.
"variables initialized when I use "double ", not in C/C++ they are not. YOu have to initialize them.
Member 12985896 6-Feb-17 17:18pm    
By initializing a variable, do you mean giving it a value? Such as using "double TF_ = 81.3;" instead of "double TF_;" and then later doing "scan_s("%Lf", &TF_);"? I'm appreciate the help, but i'm not sure what you mean. My professor isn't the best and programming is definitely not my strong suit as of now.
[no name] 6-Feb-17 18:07pm    
"mean giving it a value", partially. When you declare a variable, it has a value but it could be anything and you won't know what it is. The point of initializing is giving your variable a known value.

*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_);

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);

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;.
 
Share this answer
 
v4
Comments
Member 12985896 6-Feb-17 18:41pm    
Sorry I was not totally clear and I think your answer makes a lot of sense, but my professor wanted me to use those equations. The equations being 1) TF = TR - 459.67 for converting Fahrenheit to Rankine and 2) TR = (9/5)TK for converting Rankine to Kelvin. I agree that it would be simpler to do it your way with only one equation, but this is how my professor wants it (he can be pretty unreasonable sometimes).

Is there a way that I can use those exact equations with the fahrenheit, kelvin, and rankine similar to the way I did? I think the reason why it's not working which is similar to the way you put it is because I have the equations in the format where the already initialized TF_ = TR_ - 459.67 when it should be TR_ = TF_ + 459.67, right?

Here are my professor's exact words: "Do not introduce new equations or other equations. The equations must show in this form in the program at least once."

PS: By "TF_" I mean temperature in fahrenheit, which I could have just put as TF, but my professor wants at least three characters per variable.
Jon McKee 6-Feb-17 19:15pm    
Updated =)
Member 12985896 6-Feb-17 19:20pm    
D: What's that supposed to mean?
Jon McKee 6-Feb-17 20:18pm    
What? "Updated + a smiley face"? I was just responding to say I updated my response after reading your comment. The new code above should work within your requirements.
Quote:
but anyways aren't the variables initialized when I use "double TF_, TK_, TR_;", same as they would be if I used "int"?

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,
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.
 
Share this answer
 
Comments
Member 12985896 6-Feb-17 17:22pm    
Thank you for the help, but wouldn't doing what I did end up initializing the variable "TF_" once scan_s goes through, because I will be typing in a variable then. If not, could you give me an example of how to get it to work?
Patrice T 6-Feb-17 17:36pm    
Learn debugger on your own and learn from your own mistakes, it is the best way to learn (most efficient).
Member 12985896 6-Feb-17 17:26pm    
Here is an example of a code I did without really initializing anything.

#include <stdio.h>
#include <math.h>
#define PI 3.1416

void main(void)
{
double T_period;
int Adair_mass, k11, k22;

printf("Enter the mass in kg:\n");
scanf_s("%d", &Adair_mass);
printf("Enter the stiffness value for the first spring:\n");
scanf_s("%d", &k11);
printf("Enter the stiffness value for the second spring:\n");
scanf_s("%d", &k22);

T_period = (double)2*PI*(sqrt((Adair_mass) / (k11 + k22)));

printf("The period of movement (T) for the entered values = %6.2Lf\n", T_period);
printf("The period in scientific notation is: %6.3e\n", T_period);
}

It worked perfectly fine and even though I did use int, I could have done it with double I believe.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900