Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › [C] Need help with easy c programming issue.
New Posts  All Forums:Forum Nav:

[C] Need help with easy c programming issue.

post #1 of 7
Thread Starter 
I am taking a beginner comp sci class and I am having a weird issue with my program that I am writing. The program is meant to calculate how much grass needs to be cut on a lawn by taking the area of the yard and subtracting the are of the house.
Here is the code:
Code:
#include <stdio.h>
int main()
{
int yardlength;
int yardwidth;
int houselength;
int housewidth;
int grasssize;

grasssize = yardlength * yardwidth - houselength * housewidth;

scanf("%d%d", &yardlength, &yardwidth);
scanf("%d%d", &houselength, &housewidth);

printf("%d square feet will need to be cut.\n", grasssize);
return 0;
}
I will put in values such as:
30 40
10 20
and instead of getting 1000 as my answer I will get a value of something like: -24615476 square feet will need to be cut. Even more perplexing is the fact that my output is different each time I run the program.
If somebody could find the problem with my code I would be super grateful! thumb.gif
Project Predator
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X4 4.2 GHz GA-MA785GM-US2H EVGA GTX 260 Crucial Technology 4GB DDR2 800 
Hard DriveOptical DriveCoolingOS
1tb Samsung F3 LG dvd burner x24 XSPC Rasa rs240 Windows 7 Ultimate 64-bit 
MonitorKeyboardPowerCase
Samsung 22in 2253LW Razer Lycosa Corsair HX850w Alienware Area 51 
Mouse
Coolermaster Storm Sentinel 
  hide details  
Reply
Project Predator
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X4 4.2 GHz GA-MA785GM-US2H EVGA GTX 260 Crucial Technology 4GB DDR2 800 
Hard DriveOptical DriveCoolingOS
1tb Samsung F3 LG dvd burner x24 XSPC Rasa rs240 Windows 7 Ultimate 64-bit 
MonitorKeyboardPowerCase
Samsung 22in 2253LW Razer Lycosa Corsair HX850w Alienware Area 51 
Mouse
Coolermaster Storm Sentinel 
  hide details  
Reply
post #2 of 7
Couple of problems here...

First is you aren't initializing your variables (your compiler should have caught this...). If you don't do this, you get stack garbage. This is why you are getting different output each time because of the stack garbage is always different.

Your declarations should look like this:
Code:
int yardlength = 0;
int yardwidth = 0;
int houselength = 0;
int housewidth = 0;
int grasssize = 0;

Second is that you are doing the calculation BEFORE the scanf() call, so this line should be after scanf():
Code:
grasssize = yardlength * yardwidth - houselength * housewidth;

Third is that you should probably be using a floating point type (such as double).

Completed program:
Code:
#include <stdio.h>

int main()
{
        /* Use double and initialize each to zero to ensure we have a known state (no garbage) */
        double yardlength = 0;
        double yardwidth = 0;
        double houselength = 0;
        double housewidth = 0;
        double grasssize = 0;

        /* Scanf out input from the user */
        scanf("%lf%lf", &yardlength, &yardwidth);
        scanf("%lf%lf", &houselength, &housewidth);

        /* Make sure we perform the calculation after the input retrieval */
        grasssize = yardlength * yardwidth - houselength * housewidth;

        /* Print the data to the user - %.2lf outputs data to two decimal places */
        printf("%.2lf square feet will need to be cut.\n", grasssize);
        return 0;
}
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 #3 of 7
Thread Starter 
Oh and I forgot to mention that I am coding using vi in Cygwin (a Windows Unix environment) if it makes a difference.
Project Predator
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X4 4.2 GHz GA-MA785GM-US2H EVGA GTX 260 Crucial Technology 4GB DDR2 800 
Hard DriveOptical DriveCoolingOS
1tb Samsung F3 LG dvd burner x24 XSPC Rasa rs240 Windows 7 Ultimate 64-bit 
MonitorKeyboardPowerCase
Samsung 22in 2253LW Razer Lycosa Corsair HX850w Alienware Area 51 
Mouse
Coolermaster Storm Sentinel 
  hide details  
Reply
Project Predator
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X4 4.2 GHz GA-MA785GM-US2H EVGA GTX 260 Crucial Technology 4GB DDR2 800 
Hard DriveOptical DriveCoolingOS
1tb Samsung F3 LG dvd burner x24 XSPC Rasa rs240 Windows 7 Ultimate 64-bit 
MonitorKeyboardPowerCase
Samsung 22in 2253LW Razer Lycosa Corsair HX850w Alienware Area 51 
Mouse
Coolermaster Storm Sentinel 
  hide details  
Reply
post #4 of 7
Thread Starter 
Quote:
Originally Posted by tompsonn View Post

Couple of problems here...
First is you aren't initializing your variables (your compiler should have caught this...). If you don't do this, you get stack garbage. This is why you are getting different output each time because of the stack garbage is always different.
Your declarations should look like this:
Code:
int yardlength = 0;
int yardwidth = 0;
int houselength = 0;
int housewidth = 0;
int grasssize = 0;
Second is that you are doing the calculation BEFORE the scanf() call, so this line should be after scanf():
Code:
grasssize = yardlength * yardwidth - houselength * housewidth;
Third is that you should probably be using a floating point type (such as double).
Completed program:
Code:
#include <stdio.h>
int main()
{
        /* Use double and initialize each to zero to ensure we have a known state (no garbage) */
        double yardlength = 0;
        double yardwidth = 0;
        double houselength = 0;
        double housewidth = 0;
        double grasssize = 0;
        /* Scanf out input from the user */
        scanf("%lf%lf", &yardlength, &yardwidth);
        scanf("%lf%lf", &houselength, &housewidth);
        /* Make sure we perform the calculation after the input retrieval */
        grasssize = yardlength * yardwidth - houselength * housewidth;
        /* Print the data to the user - %.2lf outputs data to two decimal places */
        printf("%.2lf square feet will need to be cut.\n", grasssize);
        return 0;
}

Thanks so much for the reply!
1. So variables have to be given a value even if that value is reassigned later in the program?
2. I didn't know that doing the calculation after scanf was important. Thanks for the heads up.
3. Our instructor (for some reason) wanted us to use integers instead of floating point.

I will try this out asap. thumb.gif
Project Predator
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X4 4.2 GHz GA-MA785GM-US2H EVGA GTX 260 Crucial Technology 4GB DDR2 800 
Hard DriveOptical DriveCoolingOS
1tb Samsung F3 LG dvd burner x24 XSPC Rasa rs240 Windows 7 Ultimate 64-bit 
MonitorKeyboardPowerCase
Samsung 22in 2253LW Razer Lycosa Corsair HX850w Alienware Area 51 
Mouse
Coolermaster Storm Sentinel 
  hide details  
Reply
Project Predator
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X4 4.2 GHz GA-MA785GM-US2H EVGA GTX 260 Crucial Technology 4GB DDR2 800 
Hard DriveOptical DriveCoolingOS
1tb Samsung F3 LG dvd burner x24 XSPC Rasa rs240 Windows 7 Ultimate 64-bit 
MonitorKeyboardPowerCase
Samsung 22in 2253LW Razer Lycosa Corsair HX850w Alienware Area 51 
Mouse
Coolermaster Storm Sentinel 
  hide details  
Reply
post #5 of 7
Quote:
Originally Posted by spRICE View Post

Thanks so much for the reply!
1. So variables have to be given a value even if that value is reassigned later in the program?
2. I didn't know that doing the calculation after scanf was important. Thanks for the heads up.
3. Our instructor (for some reason) wanted us to use integers instead of floating point.
I will try this out asap. thumb.gif

1. No not really, it is just good practice because if you pass that variable somewhere else, you want it to be in a consistent state. Some compilers (VC++ comes to mind) enforce variable initialization.
2. Yes it is because the variables contain no useful value until after running scanf smile.gif
3. That's very weird ... what if the house length is 12.5 or the yard width is 17.64? tongue.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 #6 of 7
Thread Starter 
1. Got it.
2. Makes so much more sense.
3. Building code in California is so strict I wouldn't be surprised if the dimensions of houses had to be integers lachen.gif
Thanks again.
Project Predator
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X4 4.2 GHz GA-MA785GM-US2H EVGA GTX 260 Crucial Technology 4GB DDR2 800 
Hard DriveOptical DriveCoolingOS
1tb Samsung F3 LG dvd burner x24 XSPC Rasa rs240 Windows 7 Ultimate 64-bit 
MonitorKeyboardPowerCase
Samsung 22in 2253LW Razer Lycosa Corsair HX850w Alienware Area 51 
Mouse
Coolermaster Storm Sentinel 
  hide details  
Reply
Project Predator
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X4 4.2 GHz GA-MA785GM-US2H EVGA GTX 260 Crucial Technology 4GB DDR2 800 
Hard DriveOptical DriveCoolingOS
1tb Samsung F3 LG dvd burner x24 XSPC Rasa rs240 Windows 7 Ultimate 64-bit 
MonitorKeyboardPowerCase
Samsung 22in 2253LW Razer Lycosa Corsair HX850w Alienware Area 51 
Mouse
Coolermaster Storm Sentinel 
  hide details  
Reply
post #7 of 7
Quote:
Originally Posted by spRICE View Post

1. Got it.
2. Makes so much more sense.
3. Building code in California is so strict I wouldn't be surprised if the dimensions of houses had to be integers lachen.gif
Thanks again.

thumb.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: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › [C] Need help with easy c programming issue.