Thread: trouble with arrays returning false values

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    54

    trouble with arrays returning false values

    I am trying to give an array some values and print them out ..

    I initialise the elements to zero first .. after that I put values in .. then try print them out ..

    the code compiles and runs .. Im getting the wrong values though so if anyone could spot where the error is I would appreciate it thanks ..

    this is my code.

    Code:
    #include<iostream>
    
    using namespace std;
    
    int main( int argc, char **argv){
    
      long double arrayA[1];
      long double arrayB[1];
      long double arrayC[1];
    
      // initialise all elements in array to zero and check this happens
    
      arrayA[0] = 0;
      arrayA[1] = 0;
    
      cout<< "initial arrayA" << "\n" << arrayA[0] <<"\n";
      cout<< "initial arrayA" << "\n" << arrayA[1] <<"\n";
    
      arrayB[0] = 0;
      arrayB[1] = 0;
    
      cout<< "initial arrayB" << "\n" << arrayB[0] <<"\n";
      cout<< "initial arrayB" << "\n" << arrayB[1] <<"\n";
    
      arrayC[0] = 0;
      arrayC[1] = 0;
      
      cout<< "initial arrayC" << "\n" << arrayC[0] <<"\n";
      cout<< "initial arrayC" << "\n" << arrayC[1] <<"\n";
         
      // that worked out fine so now put values in array and check that they are 
      // ok ..
     
      arrayA[0] = 3;
      arrayA[1] = 6;
    
      cout<< "mid arrayA" << "\n" << arrayA[0] <<"\n";
      cout<< "mid arrayA" << "\n" << arrayA[1] <<"\n";
    
      arrayB[0] = 9;
      arrayB[1] = 12;
    
      cout<< "mid arrayB" << "\n" << arrayB[0] <<"\n";
      cout<< "mid arrayB" << "\n" << arrayB[1] <<"\n";
    
      arrayC[0] = 15;
      arrayC[1] = 18;
        
      cout<< "mid arrayC" << "\n" << arrayC[0] <<"\n";
      cout<< "mid arrayC" << "\n" << arrayC[1] <<"\n";
        
      // that worked ok final check to make sure
    
      cout<< "last arrayA" << "\n" << arrayA[0] <<"\n";
      cout<< "last arrayA" << "\n" << arrayA[1] <<"\n";
      
      cout<< "last arrayB" << "\n" << arrayB[0] <<"\n";
      cout<< "last arrayB" << "\n" << arrayB[1] <<"\n";
    
      cout<< "last arrayC" << "\n" << arrayC[0] <<"\n";
      cout<< "last arrayC" << "\n" << arrayC[1] <<"\n";
      
      return(0);
    }
    and a sample run returns these values

    initial arrayA
    0
    initial arrayA
    0
    initial arrayB
    0
    initial arrayB
    0
    initial arrayC
    0
    initial arrayC
    0
    mid arrayA
    3
    mid arrayA
    6
    mid arrayB
    9
    mid arrayB
    12
    mid arrayC
    15
    mid arrayC
    18
    last arrayA incorrect value here
    12
    last arrayA
    6
    last arrayB and incorrect value here
    18
    last arrayB
    12
    last arrayC
    15
    last arrayC
    18


    thanks again any help appreciated al.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    26
    If you need arrays of length two you need to declare them like
    Code:
    long double arrayA[2];
    long double arrayB[2];
    long double arrayC[2];

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    54
    thank you

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    More generally, if you declare an array as

    T arr[N];

    then valid indexes range through 0 ... N-1 (observe, not N!).
    What you were doing is called undefined behavior. I suggest you start using std::array instead of C-style arrays.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Just as a side not - instead of doing this:
    cout<< "initial arrayA" << "\n" << arrayA[0] <<"\n";
    I would put the variable name and value on the same line to make reading the debug output easier:
    Code:
    std::cout<< "initial arrayA[0] = " << arrayA[0] << std::endl;
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. returning false as a stream?
    By shiverbug in forum C++ Programming
    Replies: 2
    Last Post: 12-08-2009, 10:53 PM
  2. returning values from a function...
    By roaan in forum C Programming
    Replies: 1
    Last Post: 08-03-2009, 06:54 PM
  3. Returning Values in C
    By cuba06 in forum C Programming
    Replies: 6
    Last Post: 11-21-2007, 01:23 AM
  4. Why is FillConsoleOutputAttribute returning false?
    By manofsteel972 in forum Windows Programming
    Replies: 0
    Last Post: 09-29-2004, 06:03 PM
  5. Returning Values
    By incognito in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2002, 09:31 AM