1 - 10 of 10 Posts

Sean Webster

·
Storage Nut
Joined
·
21,146 Posts
Discussion Starter · ·
My head hurts, I am tired and I spelled Celsius wrong in my program (but that's besides the point). I need help with this:

Write a program to generate a table of conversions from Celsius to Rankin. Allow the user to enter the starting temperature and increment between lines. Print 25 lines in the table. Use a for loop in your solution.

Code:

Code:
``````//----------------------------------------------------------------------------------------------------------
//      Sean Webster
//      Assignment 3
//      This program solves Problems 40, 42, and 44 on pages 78-79 of the text.

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

#define PI 3.141593

int main()
{
//      Declare and initialize variables for problem
double initial, increment, final;
double C, F, R, celcius;

printf("input initial: ");
scanf("%lf", &initial);

printf("input increment: ");
scanf("%lf", &increment);

//      calculate final so = to 25 lines for table
//      floor((final - initial)/(inc))+1
final = ((25*initial) + increment) + 1

//      Print celcius to rankin in a loop
printf("celcius to rankin");

for (celcius=initial; celcius<=final; celcius+=increment)
{
// convert from celcius to rankin
F = ((9.0/5)*initial) + 32
R = F + 459.67

// Show tables of celcius and rankin
printf("%.2lf %.2lf \n", initial, R);
}

//      Separate the programs
printf("--------------------------------------------------------------------------------");

//      Exit program
getchar();
return 0;
}
//----------------------------------------------------------------------------------------------------------``````

{Unregistered}

·
Joined
·
1,563 Posts
I quickly skimmed through the code and it looks fine except that you wrote,

Code:

Code:
``final = ((25*initial) + increment) + 1``
This should be,

Code:

Code:
``final = ((25*increment) + initial) + 1``
I'm assuming you've already checked if adding 1 to final, gives the desired effect.

Did you just want us to check the code or did you have any specific question?

Sean Webster

·
Storage Nut
Joined
·
21,146 Posts
Discussion Starter · ·
Yea, I forgot to add it does not work, need help debugging.

Code:

Code:
``````1>------ Build started: Project: test, Configuration: Debug Win32 ------
1>Build started 9/25/2012 6:08:18 PM.
1>PrepareForBuild:
1>  Creating directory "d:\data\documents\visual studio 2010\Projects\test\Debug\".
1>InitializeBuildStatus:
1>  Creating "Debug\test.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1>  tesrt.cpp
1>d:\data\documents\visual studio 2010\projects\test\test\tesrt.cpp(19): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
1>d:\data\documents\visual studio 2010\projects\test\test\tesrt.cpp(23): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
1>d:\data\documents\visual studio 2010\projects\test\test\tesrt.cpp(30): error C2146: syntax error : missing ';' before identifier 'printf'
1>d:\data\documents\visual studio 2010\projects\test\test\tesrt.cpp(36): error C2146: syntax error : missing ';' before identifier 'R'
1>d:\data\documents\visual studio 2010\projects\test\test\tesrt.cpp(39): error C2146: syntax error : missing ';' before identifier 'printf'
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.45
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========``````

{Unregistered}

·
Joined
·
1,563 Posts
Ignore the scanf() warning. You do not need to change to scanf_s(). As far as I'm aware, only Microsoft has supposedly deprecated the use of scanf(). Just add,

Code:

Code:
``#define _CRT_SECURE_NO_DEPRECATE``
at the beginning of the code.

Or you can read up on scanf_s() anyway and use it instead. http://code.wikia.com/wiki/Scanf_s

Make sure to add a ";" after each statement, whether inside the for loop or outside. I can count 3 statements in your code, that do not have the semi-colons at the end.

Sean Webster

·
Storage Nut
Joined
·
21,146 Posts
Discussion Starter · ·
Not working right lol

Code:

Code:
``````//----------------------------------------------------------------------------------------------------------
//      Sean Webster
//      Assignment 3
//      This program solves Problems 40, 42, and 44 on pages 78-79 of the text.

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

#define _CRT_SECURE_NO_DEPRECATE

int main()
{
//      Declare and initialize variables for problem
double initial, increment, final;
double C, F, R;
double celcius;

printf("input initial: ");
scanf("%lf", &initial);

printf("input increment: ");
scanf("%lf", &increment);

//      calculate final so = to 25 lines for table
//      floor((final - initial)/(inc))+1
final = ((25*increment) + initial);

//      Print celcius to rankin in a loop
printf("celcius to rankin \n");

for (celcius=initial; celcius<=final; celcius+=increment)
{
// convert from celcius to rankin
F = ((9.0/5)*initial) + 32;
R = F + 459.67;

// Show tables of celcius and rankin
printf("%.2lf %.2lf \n", initial, R);
}

//      Separate the programs
printf("--------------------------------------------------------------------------------");

//      Exit program
getchar();
getchar();
return 0;
}
//----------------------------------------------------------------------------------------------------------``````

frozne

·
Registered
Joined
·
423 Posts
In your for loop you are doing the calculation and printing with "initial" instead of "celcius". The initial variable isn't being changed, that is why you have the same values over and over.

Sean Webster

Sean Webster

·
Storage Nut
Joined
·
21,146 Posts
Discussion Starter · ·
Quote:
Originally Posted by frozne

In your for loop you are doing the calculation and printing with "initial" instead of "celcius". The initial variable isn't being changed, that is why you have the same values over and over.
Thanks, fixed.

Code:

Code:
``````//----------------------------------------------------------------------------------------------------------
//      Sean Webster
//      Assignment 4
//      This program solves problems 29 and 34 for pages 143-145

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

#define _CRT_SECURE_NO_DEPRECATE

int main()
{
//      Declare and initialize variables for problem
double initial, increment, final;
double C, F, R;
double celcius;

printf("input initial: ");
scanf("%lf", &initial);

printf("input increment: ");
scanf("%lf", &increment);

//calculate final so = to 25 lines for table
final = ((25*increment) + initial)-1;

//      Print celcius to rankin in a loop
printf("celcius to rankin \n");

for (celcius=initial; celcius<=final; celcius+=increment)
{
// convert from celcius to rankin
F = ((9.0/5)*celcius) + 32;
R = F + 459.67;

// Show tables of celcius and rankin
printf("%.2lf %.2lf \n", celcius, R);
}

//      Separate the programs
printf("--------------------------------------------------------------------------------");

//      Exit program
getchar();
getchar();
return 0;
}
//----------------------------------------------------------------------------------------------------------``````

tompsonn

·
Joined
·
10,774 Posts
I still recommend against using scanf directly... you'll run into tricky bugs if users don't input the right format.

Sean Webster

·
Storage Nut
Joined
·
21,146 Posts
Discussion Starter · ·
Quote:
Originally Posted by tompsonn

I still recommend against using scanf directly... you'll run into tricky bugs if users don't input the right format.
Yea, i'll deal with that if I have to later lol, we are about to move away from C now anyways to MATLab.

·