New Posts  All Forums:Forum Nav:

C programming

post #1 of 10
Thread 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:
//----------------------------------------------------------------------------------------------------------
//      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;

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

        //      ask for increment                                                                                                                               
        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;
}
//----------------------------------------------------------------------------------------------------------
post #2 of 10
I quickly skimmed through the code and it looks fine except that you wrote,
Code:
final = ((25*initial) + increment) + 1

This should be,
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?
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
post #3 of 10
Thread Starter 
Yea, I forgot to add it does not work, need help debugging.
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 ==========
post #4 of 10
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:
#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.
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
post #5 of 10
Thread Starter 
Not working right lol


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;

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

        //      ask for increment                                                                                                                               
        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;
}
//----------------------------------------------------------------------------------------------------------

Edited by Sean Webster - 9/25/12 at 3:33pm
post #6 of 10
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.
Sab Tower
(12 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k ASRock Z77 OC Formula HIS IceQ Radeon 7950 Corsair Dominator Platinum 
Hard DriveHard DriveCoolingOS
Samsung Samsung 830 XSPC Raystorm EX360 OpenSUSE 12.2 
MonitorPowerCaseMouse
Acer 21 Inch Flat Screen Seasonic x650 NZXT Switch 810 Logitech MX510 
  hide details  
Reply
Sab Tower
(12 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k ASRock Z77 OC Formula HIS IceQ Radeon 7950 Corsair Dominator Platinum 
Hard DriveHard DriveCoolingOS
Samsung Samsung 830 XSPC Raystorm EX360 OpenSUSE 12.2 
MonitorPowerCaseMouse
Acer 21 Inch Flat Screen Seasonic x650 NZXT Switch 810 Logitech MX510 
  hide details  
Reply
post #7 of 10
Thread Starter 
Quote:
Originally Posted by frozne View Post

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. biggrin.gif

3ed5925c089545f2b2859ba.png
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;

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

        //      ask for increment                                                                                                                               
        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;
}
//----------------------------------------------------------------------------------------------------------

Edited by Sean Webster - 9/25/12 at 4:18pm
post #8 of 10
I still recommend against using scanf directly... you'll run into tricky bugs if users don't input the right format.
smile.gif
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
post #9 of 10
Thread Starter 
Quote:
Originally Posted by tompsonn View Post

I still recommend against using scanf directly... you'll run into tricky bugs if users don't input the right format.
smile.gif
Yea, i'll deal with that if I have to later lol, we are about to move away from C now anyways to MATLab. cheers.gif
post #10 of 10
Quote:
Originally Posted by Sean Webster View Post

Yea, i'll deal with that if I have to later lol, we are about to move away from C now anyways to MATLab. cheers.gif

I've got a whole thread on scanf replacement... literally a drop in function you can just use straight out of the box, so there won't be much "dealing" tongue.gif
Sure thing... smile.gif
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming