Thread: Arrary & ForLoop

  1. #16
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Array indices go from 0 to array_size, but not including it. Your for loop tries to access your_array[your_array_size] because the loop condition is wrong. Can you see how to fix it?

    (And please replace every instance of 15 in your code with LIMIT, because that's what its for.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  2. #17
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    yeah, it should be counter<=14 in the for loop

  3. #18
    Registered User
    Join Date
    May 2008
    Posts
    24
    Oh, a comedian.

    Ok, let me see if I can do that. (be right back)

  4. #19
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by abh!shek View Post
    yeah, it should be counter<=14 in the for loop
    Not really. There is a perfectly meaningful constant LIMIT. 14, on the other hand is quite obscure magic value. Therefore the condition should be counter < LIMIT.

    The whole point of insisting on using LIMIT everywhere instead of the magic value 15 (or 14) is, that if you want to change the size of the array, the only thing to change would be the value of LIMIT.

    On the other hand, if you use magic numbers throughout the code you'd have to locate them all one by one. This would be particularly hard, if 15 could stand for more than one logical constant in the code (e.g there are two unrelated arrays, both initially sized 15, but now you'd want to change the size of just one). It is also harder, if there are other values, somehow related to 15 (like 14, which is 15-1).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #20
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    yep, thx.

  6. #21
    Registered User
    Join Date
    May 2008
    Posts
    24
    IDK

    What about:

    Code:
    for ( int x = 0; x < 15; x

  7. #22
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    aaaaabbbbbbbbbbaaaaaaarrrrrrrrrrrrrrrrrrrooo
    Code:
    for(counter=14;counter>=0;counter--)
    {
        cout<<array[counter]<<endl;
    
    }

  8. #23
    Registered User
    Join Date
    May 2008
    Posts
    24
    Thanks, but it's still not working:

    Code:
    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    
    
    int main()
    {
     int array[15]
    for (counter=0;counter=<15;counter++)
    {
    cout<<"please enter a number"<<endl;
    cin>>array[counter];
    }	
      
    for(counter=15;counter>=0;counter--)
    {
        cout<<array[counter]<<endl;
    
    }
    
      return 0;
    }

  9. #24
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Code:
    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    
    
    int main()
    {
     int array[15];
    for (counter=0;counter<15;counter++)
    {
    cout<<"please enter a number"<<endl;
    cin>>array[counter];
    }	
      
    for(counter=14;counter>=0;counter--)
    {
        cout<<array[counter]<<endl;
    
    }
    
      return 0;
    }

  10. #25
    Registered User
    Join Date
    May 2008
    Posts
    24
    Getting this error:

    error C2065: 'counter' : undeclared identifier

  11. #26
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by slimdime View Post
    Getting this error:

    error C2065: 'counter' : undeclared identifier

    You figure out yourself why you are getting this error. I'm busy making a cool virus that blows up computers, so I can't look into your program right now.
    Last edited by abh!shek; 05-15-2008 at 05:51 PM.

  12. #27
    Registered User
    Join Date
    May 2008
    Posts
    24
    OK. (running away very fast) Thanks.

  13. #28
    Registered User
    Join Date
    May 2008
    Posts
    24
    GOT IT!!

    Code:
    #include <stdafx.h>
    
    #include <iostream>  
      
    using namespace std;  
      
    const int LIMIT = 15;  
      
    int main()  
    {  
        int array[LIMIT];  
      
        for (int i = 0; i <= LIMIT; i++)   
        {  
            
            cout<<"please enter array member #" << i << ": ";  
             
            cin>>array[i];  
        }  
       
        for (int i = LIMIT; i >= 0; i--)  
            
            cout << "Array member #" << i << ": " << array[i] << endl;  
       
        system ("pause");  
        return 0;  
    }

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Index 15 doesn't exist (only 0 - 14 does).
    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.

  15. #30
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by slimdime View Post
    GOT IT!!

    0.00&#37; of that code is your own. You're just cross posting internet forums.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-09-2009, 06:17 PM
  2. Arrary with user defined size
    By GCNDoug in forum C++ Programming
    Replies: 8
    Last Post: 04-09-2008, 04:34 PM
  3. Arrary Size
    By Hexxx in forum C++ Programming
    Replies: 2
    Last Post: 04-17-2006, 12:21 PM
  4. Abot Arrary Sorting
    By FeNCinGeR in forum C++ Programming
    Replies: 1
    Last Post: 04-04-2006, 08:39 AM