Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Strange output
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
jamonathin




PostPosted: Sun Sep 24, 2006 7:18 pm   Post subject: Strange output

Hey all, I haven't done C in forever and i decided to do a sample problem out of my textbook. I recently got lcc-win32 as my compiler, and just started to use it today.

The problem is very simple - You have 2 yards. You have the lengths and widths of the yards and the houses on them, determine the leftover (grass area).
Now im probabily doing something wrong in my printf command, but i kept getting a HUGE number for a very small input. And to test it i put the printf result part at the top (where the areas should be 0), yet i still get some some HUGE number . . . wtf mayte?

c:

#include <stdio.h>

int main ()
{
        int lengthYard1 = 0, widthYard1 = 0, lengthHouse1 = 0, widthHouse1 = 0;
        int lengthYard2 = 0, widthYard2 = 0, lengthHouse2 = 0, widthHouse2 = 0;
        int areaYard1 = 0, areaYard2 = 0;

        // Here's what I dont get.
        printf ("The area of grass on Yard 1: %d",&areaYard1);
        printf ("\nThe area of grass on Yard 2: %ld\n\n",&areaYard2);
        //

        // Getting length and widths of first yard
        printf ("Enter Length of Yard1 and Width of Yard1: ");
        scanf ("%d%d",&lengthYard1, &widthYard1);
        printf ("\nEnter Length of House1 and Width of House1: ");
        scanf ("%d%d", &lengthHouse1, &widthHouse1);

        // Getting length and widths of second yard
        printf ("\n\nEnter Length of Yard2 and Width of Yard2: ");
        scanf ("%d%d",&lengthYard2, &widthYard2);
        printf ("\nEnter Length of House2 and Width of House2: ");
        scanf ("%d%d", &lengthHouse2, &widthHouse2);

        // "The calculation"
        areaYard1 = (lengthYard1 * widthYard1) - (lengthHouse1 * widthHouse1);
        areaYard2 = (lengthYard2 * widthYard2) - (lengthHouse2 * widthHouse2);

        // Same output as before
        printf ("The area of grass on Yard 1: %d",&areaYard1);
        printf ("\nThe area of grass on Yard 2: %d\n\n",&areaYard2);

        return 0;
}


Example Output wrote:

Yard1: 1245036
Yard2: 1245032
Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: Sun Sep 24, 2006 7:24 pm   Post subject: (No subject)

don't pass pointers; just pass the ints
Tony




PostPosted: Sun Sep 24, 2006 8:11 pm   Post subject: (No subject)

yeah, &areaYard1 actually prints the address of the variable, not the value stored there. Notice how your yard1 and yard2 are 4 units appart. Incidently the space reserved for a single integer variable is 4 bytes Wink
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
jamonathin




PostPosted: Sun Sep 24, 2006 8:38 pm   Post subject: (No subject)

ooooo, lol thanks Doh!
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: