Search:
Forum
Beginners
How do you set i<= array length
How do you set i<= array length
Oct 4, 2012 at 11:50am UTC
vichu99
(10)
Hello, how do you stop the iteration when it has reached the array length?
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int b[]={1,2,3,4,5,6,7};
for(int i=0; i<=array.length; i++){ //instead of array.length?
cout <<b[i]<<", ";
int c;
cin>>c;
}
Last edited on
Oct 4, 2012 at 11:51am UTC
Oct 4, 2012 at 11:56am UTC
icegood
(63)
Universal way for constant arrays. Works for c also:
#define arrlength(x) sizeof(x)/sizeof(x[0])
because sizeof is rather also precompiled macros result will be as fast as possible because on compilation stage you will get exactly constant
Oct 4, 2012 at 12:01pm UTC
vichu99
(10)
Thanks, what do you write in the code?
Oct 4, 2012 at 12:05pm UTC
Chervil
(7320)
1
2
int
array[]={1,2,3,4,5,6,7};
const
int
length =
sizeof
(array) /
sizeof
(array[0]);
or possibly use
std::vector
instead.
Last edited on
Oct 4, 2012 at 12:21pm UTC
Topic archived. No new replies allowed.
C++
Information
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs