Click here to Skip to main content
Sign Up to vote bad
good
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:40
ALIWAZ392
Edited 11 Dec '12 - 3:49

Comments
Eugen Podsypalnikov - 11 Dec '12 - 9:49
Try it :) : for (i = 0; i < _countof(abc); i++) { //.. }
VISH_a_CODE - 11 Dec '12 - 10:03
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:44
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

4 solutions

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  
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  
for(i=0;i<sizeof(abc)/sizeof(abc[0]);i++)
  Permalink  
Comments
ThatsAlok - 12 Dec '12 - 2:24
don't know who voted one for you! however my 5pt will square it off
#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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 8,376
1 OriginalGriff 6,571
2 CPallini 3,533
3 Rohan Leuva 2,703
4 Maciej Los 2,234


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