array question

This is a discussion on array question within the C++ Programming forums, part of the General Programming Boards category; i'm completely puzzled by a small problem, i would appreciate it if someone could help me out. outside main i ...

  1. #1
    Martijn
    Guest

    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,

    Martijn

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    The index of an array starts with 0 and not with 1.
    Code:
    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, 05:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM

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