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;
}
My System
(30 items)
 
"Zeus"
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500K (4.5ghz @ 1.320v) Gigabyte Z68X-UD3R-B3 MSI R7970 Lightning Corsair 16GB (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Crucial M4 128GB 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 7 Ultimate 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2212HM Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Microsoft IntelliMouse Optical  XTRAC Ripper XXL 
AudioAudioAudioAudio
Westone W3 IEMs RE-272 IEMs Shure SE-215 IEMs Schiit Bifrost DAC 
AudioAudio
Schiit Asgard 2 amp HiVi Swan M50W 2.1 
CPUMotherboardGraphicsRAM
Intel Core i7 950 GA-X58-UD3R Radeon HD 5450  24GB Corsair @ 1333mhz 
Hard DriveOSPowerCase
4x WD Cavair Red 1TB in RAID 0 Windows Server 2008 R2 x64 Corsair HX-520 LianLi LanCool 
  hide details  
Reply
My System
(30 items)
 
"Zeus"
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500K (4.5ghz @ 1.320v) Gigabyte Z68X-UD3R-B3 MSI R7970 Lightning Corsair 16GB (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Crucial M4 128GB 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 7 Ultimate 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2212HM Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Microsoft IntelliMouse Optical  XTRAC Ripper XXL 
AudioAudioAudioAudio
Westone W3 IEMs RE-272 IEMs Shure SE-215 IEMs Schiit Bifrost DAC 
AudioAudio
Schiit Asgard 2 amp HiVi Swan M50W 2.1 
CPUMotherboardGraphicsRAM
Intel Core i7 950 GA-X58-UD3R Radeon HD 5450  24GB Corsair @ 1333mhz 
Hard DriveOSPowerCase
4x WD Cavair Red 1TB in RAID 0 Windows Server 2008 R2 x64 Corsair HX-520 LianLi LanCool 
  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
My System
(30 items)
 
"Zeus"
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500K (4.5ghz @ 1.320v) Gigabyte Z68X-UD3R-B3 MSI R7970 Lightning Corsair 16GB (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Crucial M4 128GB 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 7 Ultimate 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2212HM Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Microsoft IntelliMouse Optical  XTRAC Ripper XXL 
AudioAudioAudioAudio
Westone W3 IEMs RE-272 IEMs Shure SE-215 IEMs Schiit Bifrost DAC 
AudioAudio
Schiit Asgard 2 amp HiVi Swan M50W 2.1 
CPUMotherboardGraphicsRAM
Intel Core i7 950 GA-X58-UD3R Radeon HD 5450  24GB Corsair @ 1333mhz 
Hard DriveOSPowerCase
4x WD Cavair Red 1TB in RAID 0 Windows Server 2008 R2 x64 Corsair HX-520 LianLi LanCool 
  hide details  
Reply
My System
(30 items)
 
"Zeus"
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500K (4.5ghz @ 1.320v) Gigabyte Z68X-UD3R-B3 MSI R7970 Lightning Corsair 16GB (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Crucial M4 128GB 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 7 Ultimate 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2212HM Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Microsoft IntelliMouse Optical  XTRAC Ripper XXL 
AudioAudioAudioAudio
Westone W3 IEMs RE-272 IEMs Shure SE-215 IEMs Schiit Bifrost DAC 
AudioAudio
Schiit Asgard 2 amp HiVi Swan M50W 2.1 
CPUMotherboardGraphicsRAM
Intel Core i7 950 GA-X58-UD3R Radeon HD 5450  24GB Corsair @ 1333mhz 
Hard DriveOSPowerCase
4x WD Cavair Red 1TB in RAID 0 Windows Server 2008 R2 x64 Corsair HX-520 LianLi LanCool 
  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
My System
(30 items)
 
"Zeus"
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500K (4.5ghz @ 1.320v) Gigabyte Z68X-UD3R-B3 MSI R7970 Lightning Corsair 16GB (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Crucial M4 128GB 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 7 Ultimate 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2212HM Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Microsoft IntelliMouse Optical  XTRAC Ripper XXL 
AudioAudioAudioAudio
Westone W3 IEMs RE-272 IEMs Shure SE-215 IEMs Schiit Bifrost DAC 
AudioAudio
Schiit Asgard 2 amp HiVi Swan M50W 2.1 
CPUMotherboardGraphicsRAM
Intel Core i7 950 GA-X58-UD3R Radeon HD 5450  24GB Corsair @ 1333mhz 
Hard DriveOSPowerCase
4x WD Cavair Red 1TB in RAID 0 Windows Server 2008 R2 x64 Corsair HX-520 LianLi LanCool 
  hide details  
Reply
My System
(30 items)
 
"Zeus"
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500K (4.5ghz @ 1.320v) Gigabyte Z68X-UD3R-B3 MSI R7970 Lightning Corsair 16GB (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Crucial M4 128GB 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 7 Ultimate 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2212HM Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Microsoft IntelliMouse Optical  XTRAC Ripper XXL 
AudioAudioAudioAudio
Westone W3 IEMs RE-272 IEMs Shure SE-215 IEMs Schiit Bifrost DAC 
AudioAudio
Schiit Asgard 2 amp HiVi Swan M50W 2.1 
CPUMotherboardGraphicsRAM
Intel Core i7 950 GA-X58-UD3R Radeon HD 5450  24GB Corsair @ 1333mhz 
Hard DriveOSPowerCase
4x WD Cavair Red 1TB in RAID 0 Windows Server 2008 R2 x64 Corsair HX-520 LianLi LanCool 
  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.