Thread: array question

  1. #1

    Unhappy array question

    i'm completely puzzled by a small problem,
    i would appreciate it if someone could help
    me out.

    outside main i have:

    const int n = 2;

    inside main i have:

    float qr;
    float y[n];

    then, a bit further down, initializing variables:

    qr = 0;
    y[1] = 1.;
    y[2] = 3.;

    when i did cout << qr i got 3 ?!
    when i didn't include y[2] i got 0 as i wanted. probably i'm
    overlooking something with arrays, but i can't seem to figure
    out what it is..... anyway thnx ia for helping me out,


  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    The index of an array starts with 0 and not with 1.
    qr = 0; 
    y[0] = 1.0f; 
    y[1] = 3.0f;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM