Click here to Skip to main content
11,644,286 members (71,507 online)
Rate this: bad
good
Please Sign up or sign in to vote.
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 11-Dec-12 3:40am
ALIWAZ407
Edited 11-Dec-12 3:49am
v2
Comments
Eugen Podsypalnikov at 11-Dec-12 9:49am
   
Try it :) :
for (i = 0; i < _countof(abc); i++) {
//..
}
VISH_a_CODE at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You have 3 items in array, in this case, index can be 0, 1 or 2.
You can do the following test :
for(i=0;i<=2;i++)
or
for(i=0;i<3;i++)
or
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:
int NbItems(int abc[]) { return sizeof(abc)/sizeof(abc[0]);}
int* abc; abc = new int[3];
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I think you can use
int nSize = sizeof(abc)/sizeof(abc[0]);
so the code is look like below
#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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

for(i=0;i<sizeof(abc)/sizeof(abc[0]);i++)
  Permalink  
Comments
ThatsAlok at 12-Dec-12 2:24am
   
don't know who voted one for you! however my 5pt will square it off
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

#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;
}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 570
1 jyo.net 484
2 Afzaal Ahmad Zeeshan 413
3 DamithSL 308
4 Sergey Alexandrovich Kryukov 276
0 OriginalGriff 795
1 DamithSL 611
2 Mika Wendelius 494
3 jyo.net 484
4 Afzaal Ahmad Zeeshan 404


Advertise | Privacy | Mobile
Web01 | 2.8.150731.1 | Last Updated 11 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100