Thread: Selection statements are changing the values of my variables!!

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It shouldn't be initialized. "oranges[3]" is not a valid array index, and cannot be used other than for a comparison against that location.


    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Remember, if you declare an array as
    Code:
    int array[5];
    the valid indexes are from 0 to 4, not from 1 to 5.

    Re-read Salem's post.
    Quote Originally Posted by Salem
    > for(i=1;i<=3;i++)
    All arrays start at 0, not 1
    Your code has already gone wrong when it tried to use oranges[3]

    for ( i = 0 ; i < 3 ; i++ )
    Would be the correct loop here.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #18
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    It must be initialized.

    Quote Originally Posted by quzah
    It shouldn't be initialized.

    Quzah.
    If initialize oranges with diffrent values like one time
    oranges[0] = 999;
    oranges[1] = 999;
    oranges[2] = 888;

    and another
    oranges[0] = 999999;
    oranges[1] = 999999;
    oranges[2] = 999999;

    You get diffrent results.

  4. #19
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    one thing that nobody else picked up on so i'll answer it - there
    is a way to get the modulus of a number: there is a function in
    stdlib.h called abs () (absolute value) which takes an
    integer argument and returns is modulus, or for a more far
    reaching version there is fabs () which is defined in math.h
    and handles floats. Just so you know.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You're not supposed to post in threads older than 2 weeks.
    there
    is a way to get the modulus of a number: there is a function in
    stdlib.h called abs () (absolute value) which takes an
    integer argument and returns is modulus
    No, *abs() return a positive number. That is, if the number is positive, it is returned unchanged, and if it is negative it is negated. It's the same as this (or the macro below):
    Code:
    int sameabs(int x) {
        return x > 0 ? x : -x;
    }
    Modulus doesn't come into it.
    or for a more far
    reaching version there is fabs () which is defined in math.h
    and handles floats.
    Actually, it takes a double and returns a double, but floats work with it too. Or you can write a macro:
    Code:
    #define ABS(x) ((x) > 0 ? (x) : -(x))
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem on - Selection on which item to purchase
    By karipap in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 06:19 AM
  2. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  3. Effecient Selection Statements?
    By 031003b in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2007, 06:08 AM
  4. selection problem
    By Ken JS in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 09:47 PM
  5. assign values to charechter variables
    By simhap in forum C++ Programming
    Replies: 5
    Last Post: 10-07-2001, 07:55 PM