more with my 1st structure program

This is a discussion on more with my 1st structure program within the C Programming forums, part of the General Programming Boards category; > With all the ranting and raving, and extreme comments, I am > frustrated now. Blame them. I already answered ...

  1. #16
    Unregistered
    Guest
    > With all the ranting and raving, and extreme comments, I am
    > frustrated now.

    Blame them. I already answered your question. :P

    > I am totally confused about :
    >
    > when to use struct and when not

    Use a 'struct' when you want to group a number of variables with
    a common goal in a handy name. Such as:
    Code:
    struct coordinate
    {
       int xCoord, yCoord;
    };
    > when using a variable of a structure
    > how to reference a global variable with structures
    > how to reference a local variable
    > when to know to use a global vs a local

    A global variable is a variable located outside of any function.
    Code:
    #include <stdio.h>
    
    /**
    *** This is not a global variable, it is only a definition.
    **/
    struct mystruct
    {
       int a;
       float b;
    };
    
    /**
    *** These are global variables. They are not in any function.
    **/
    int myGlobalInt;
    float myGlobalFloat;
    struct mystruct myGlobalStructInstance;
    
    int main( void )
    {
       struct mystruct myLocalInstance;
       int myLocalInt;
       float myLocalFloat;
    
       ...do stuff here...
       myLocalStructInstance.a = 10;
       myGlobalStructInstance.a = myLocalStructInstance.a;
       ...do more stuff...
    
       return 0;
    }
    > C language is hard.

    It can be.

    > It is hard because I am used to BASIC which I learned 20 years
    > ago. BASIC had line numbers and made sense to me. I didn't
    > do real complicated programs, but I HAD FUN doing what was
    > asked. Things have greatly changed, and now it isn't fun. I only
    > decided to get IT training last year, and the class I take now is
    > so fast, I don't get the darn stuff as fast as you guys. And to
    > make matters worse. I pay the stupid university $600 for this
    > course and the professor is as arrogant as some programmers
    > I know. and says everything is SIMPLE. It is simple, to someone
    > who's been doing it for a long time.

    You may want to consider Visual Basic.

    I DON'T GET IT FOLKS.

    > I learn by step by step instruction.
    > I learn without all the subjective comments.
    > I need a better book that shows many examples of whatever is > being taught. Without jumping from one set of parameters to
    > another.

    We are not your personal paid instructors.

    > I give up. And I guess C++ and Java are just as bad, huh?

    I actually like Java. Still, both of those languages are definately
    a half a dozen steps or so above "BASIC".

    Quzah.

  2. #17
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    Thanks, Troll King.

    I think it is fabulous you understand the C language with only 2 1/2 years experience. You learn well, and remember well. I suspect you know other programming languages too. I don't. I am at a disadvantage by not having a frame of reference with which to compare things to. To me C is like learning Greek. It would take years to switch to a new speaking language, and be a pro at it. Heck, I ain't a pro at English, and I have been speaking it for 33 years!!!!

    The program is to do the following:
    1) find area of rectangle r (done)
    2) find the center of rectangle r(having many problems with this part as far as using C syntax to explain it
    3) move r by x units in the x direction and y units in the y direction, returning the modified version of r (x and y are additional arguments to the function) --- I think this means that rectangle r is the same size as before but moved to where the lower left corner is at the sight of the former upper right corner (these points were not referenced in the point structure.)
    4) then find if a point (p) lies within r; returning TRUE or FALSE
    (p is an additional argument of type struct point)....now is p in the old r or the new r .... I have no idea.

    1) is done
    2) is almost done
    3) is confusing
    4) is confusing++
    Sue B.

    dazed and confused


  3. #18
    Unregistered
    Guest
    To get the center of a rectangle, find the width of each side:

    xLength = xRight - xLeft
    xCenter = xLeft + (xLength/2);

    Do the same for the y coord, and that should give you the center.

    xCoord += xDistance
    yCoord += yDistance

    That'll "move" your rectangle.

    If a point is greater than the left most point, and less than the right most point, then check to see if it is: greater than the north most point (top) and less than the south most point. If so, you've got a point in the center. If any of those statements is false, you don't.

    You may want to plot this out on graph paper, just so you have an idea of how it looks/works.

    Quzah.

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    #*$(@# @#($*@( #@#!@@@ POS board. I hate the timer on this login here.

    Quzah.

  5. #20
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,707
    >when to use struct and when not, when using a variable of a >structure

    Use a struct for only the most generic purposes! I mean, if you had a "struct car" to hold useful data about a model of car, you would not, say have a variable in it that contains your favorite radio station, or a variable that contains the fact that you spilled your cappucino on the floorboard! That's a little extreme but the point is, some people frankly misuse the structure to hold unrelated facts and figures.

    In other words, if there is some piece of data you might need frequently that is related to, say any given rectangle, then make it a data member!


    >how to reference a global variable with structures

    Since global variables can be seen anywhere in the program, you can literally refer to it/ access it in any function without even passing it in as a parameter! (But you won't be able to change it in these functions unless you DO pass it in, as a *pointer or a &reference).




    >how to reference a local variable

    Since a local variable cannot be "seen" by anything except for manipulations within it's own scope, to access it in a function, you must pass it in as a parameter.


    The less parameters, the better! So don't write a function that passes more parameters than necessary!





    >when to know to use a global vs a local

    Generally, ALWAYS use a local variable, and only create global variables about as often as you take trips to Alaska!! So RARELY.
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > Since global variables can be seen anywhere in the program,
    > you can literally refer to it/ access it in any function without
    > even passing it in as a parameter!

    This is correct.

    > (But you won't be able to change it in these functions unless
    > you DO pass it in, as a *pointer or a &reference).

    This is wrong.
    Code:
    #include <stdio.h>
    
    int myGlobal=0;
    
    void myFunction( int x ) { myGlobal = x; }
    
    int main ( void )
    {
       myGlobal = 20;
       printf("myGlobal: %d\n", myGlobal );
    
       myFunction( 15 );
       printf("myGlobal: %d\n", myGlobal );
       return 0;
    }
    You can only _not_ change a global variable if you define it as constant. Then you cannot change it after it has been initialized, but still, all functions can see it.

    Quzah.

  7. #22
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    yuneek

    having problems with this part; the line is too long to fit across screen; how do I continue assignment line onto next line but have compiler know that it is taking two lines for one assignment???

    NOTE: as it appears here, it looks fine, but on my screen where actual program is, it doesn't fit across so well

    Code:
    
    struct point center_of_r ( struct rectangle rect )
    {
        struct point center;
        center.x = rect.upper_left.x + ( rect.lower_right.x - rect.upper_left.x ) / 2;
        center.y = rect.upper_left.x + ( rect.lower_right.y - rect.upper_left.y ) / 2;
        return center;
    }
    Sue B.

    dazed and confused


  8. #23
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Except inside " " and ' ', wherever you have a space, you can also have a newline.

    So just insert newlines and indentation as you need them to make the code look nicely formatted.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #24
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    yuneek

    nevermind; got it to compile now


    Now my problem is that the center point is not being calculated correctly. And there is one problem. In some instances the
    center point could be in between whole integer numbers (eg. 4.5 or 6.5). Where do I typecast for a float so as to represent the values correctly??? In the local function, in the main??



    if for instance my upper_left coords are 2,7
    and my lower_right coordsd are 7,2
    the area is 25
    the center is 4.5, 4.5

    but it is returning center is 1 1

    Why? Where is problem??? And how do I fix...including typecasting??
    Sue B.

    dazed and confused


  10. #25
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    There are 3 things you need to do
    1. make a backup copy of working code

    2. Change your point structure like so
    Code:
    struct point {
        float x, y;
    };
    3. Change your input to read floats instead of ints
    Code:
    scanf( "%f %f", &r.upper_left.x, &r.upper_left.y );
    
    scanf( "%f %f", &r.lower_right.x, &r.lower_right.y );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #26
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    STILL RETURNING WRONG POINT COORDINATES

    Ok here's my revised code;
    I don't think I am allowed to change the members of struct point.
    Is there any other way to get floating-point variables to be returned in the
    FUNCTON: struct point center_of_r(struct rectangle rect);
    ????

    I didn't change anything here. I tried typecasting the assignments to center.x and center.y to float but that didn't work (and yes I changed the printf line to %5.1f to write a float )

    What else can I do here??


    Code:
    #include <stdio.h>
    
    struct point {
       int x, y;
    };
    
    struct rectangle {
       struct point upper_left, lower_right;
    } ;
    
    
    int area_of_r(struct rectangle rect);
    struct point center_of_r(struct rectangle rect);
    
    int main() {
    
      struct rectangle r;
      struct point center;
      int area;
    
      int coord_x, coord_y;
    
      printf( "input upper left corner of rectangle r "
               "as 0 0\n");
      scanf( "%d %d", &r.upper_left.x, &r.upper_left.y );
      printf( "input lower right corner of rectangle r  "
              "as 0 0\n");
      scanf( "%d %d", &r.lower_right.x, &r.lower_right.y );
    
      area = area_of_r(r);
      printf( "\nThe area of rectangle r is "
              "%d\n", area);
    
      center = center_of_r(r);
      printf("\nThe center point of rectangle r is "
             "%d %d\n\n",center.x, center.y);
    
      return 0;
    }
    
    int area_of_r ( struct rectangle rect )
    {
      int area;
      area = (rect.lower_right.x - rect.upper_left.x) *
             (rect.upper_left.y - rect.lower_right.y);
      return area;
    }
    
    struct point center_of_r(struct rectangle rect)
    {
        struct point center;
    
        center.x = ((rect.lower_right.x) +
                                (rect.upper_left.x))/2;
        center.y = ((rect.upper_left.y) +
                               (rect.lower_right.y))/2;
    
        return center;
    }
    
    Sue B.

    dazed and confused


  12. #27
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    Oh, the coordinates are correct now;
    forgot to change my subject line of last post
    sorry.
    Sue B.

    dazed and confused


  13. #28
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > I don't think I am allowed to change the members of struct point.
    In which case, you're sunk on the rounding problem - there is no easy way out.

    Before you try and solve it, you'd better make sure with your tutor that this is acceptable.

    I don't think it matters for the purpose of this exercise, which seems to be aimed at how to handle structures.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #29
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    can't typecast the varibles in function ??


    struct point center_of_r(struct rectangle rect)
    {
    struct point center;

    center.x = (float)((rect.lower_right.x) +
    (rect.upper_left.x))/2;
    center.y = (float)((rect.upper_left.y) +
    (rect.lower_right.y))/2;

    return center;
    }
    Sue B.

    dazed and confused


  15. #30
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    You can not sucessfully typecase a float return value if the lvalue an integer. The lvalue being the value on the left of the equal sign.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Again
    By christianne in forum C Programming
    Replies: 14
    Last Post: 04-06-2006, 12:39 AM
  2. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  3. Simple program structure.
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-15-2002, 04:36 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. structure program
    By prlove71 in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2002, 08:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21