Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
int abc[]={5,8,9};
int i;
for(i=0;i<= ;i++)
cout<<abc[i];


my qestion what value i write in condition
Posted
Updated 11-Dec-12 3:49am
v2
Comments
Eugen Podsypalnikov 11-Dec-12 9:49am    
Try it :) :
for (i = 0; i < _countof(abc); i++) {
//..
}
VISH_a_CODE 11-Dec-12 10:03am    
I think you should try to declare the array first, then try to initialize for loop.

Ex:
int abc[10]={5,8,9};
int i;
for(i=0;i<=5;i++)
cout<<abc[i];

I tried it. So try it out....:)
Sergey Alexandrovich Kryukov 11-Dec-12 11:44am    
This is an incorrect question. As always, first you need to tell what do you want to achieve. Depending on that -- could be different expressions.
--SA

You have 3 items in array, in this case, index can be 0, 1 or 2.
You can do the following test :
C++
for(i=0;i<=2;i++)

or
C++
for(i=0;i<3;i++)

or
C++
for(i=0;i<sizeof(abc)/sizeof(abc[0]);i++)


the last one can only be used because the array size of abc is set at the definition of the variable (implicitely in this case, explicite will be "int abc[3];").
It cannot be used with those:
C++
int NbItems(int abc[]) { return sizeof(abc)/sizeof(abc[0]);}
int* abc; abc = new int[3];
 
Share this answer
 
v2
I think you can use
C++
int nSize = sizeof(abc)/sizeof(abc[0]);

so the code is look like below
C++
#include <iostream.h>
int main()
{
    int abc[]={5,8,9};
    int i;
    int nSize = sizeof(abc)/sizeof( abc[0] );
    for(i=0; i < nSize ;i++ )
    {
        cout<<abc[i];
    }
    return 1;
}

try it
http://stackoverflow.com/questions/2773328/how-to-find-the-size-of-integer-array
 
Share this answer
 
v2
for(i=0;i<sizeof(abc)/sizeof(abc[0]);i++)
 
Share this answer
 
Comments
ThatsAlok 12-Dec-12 2:24am    
don't know who voted one for you! however my 5pt will square it off
C#
#include <iostream>
using namespace std;
int main()
{
    int abc[]={5,8,9};
    int i;
    int nSize = sizeof(abc)/sizeof( abc[0] );
    for(i=0; i < nSize ;i++ )
    {
        cout<<abc[i]<<endl;
    }
    return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900