does any one know how to Print the contents of the array on even index??

Recommended Answers

All 8 Replies

Please specify which language you are working with so this thread can be moved to the correct forum.

Please specify which language you are working with so this thread can be moved to the correct forum.

Im working in C++

There are a couple different ways to do it. Both will require loops. What are the even numbers? 0,2,4,6,8, etc...

Can you think of a way to generate a similar series to use as a sequence of array indexes?

There are a couple different ways to do it. Both will require loops. What are the even numbers? 0,2,4,6,8, etc...

Can you think of a way to generate a similar series to use as a sequence of array indexes?

Im not talking about the even numbers stored in array,,But im saying that the numbers which are stored on even index of array could be displayed?

Im not talking about the even numbers stored in array,,But im saying that the numbers which are stored on even index of array could be displayed?

Think before you speak. Thats exactly whats he talking about. Fir you need to know
how to generate even numbers such as {0,2,4...}. Then you can use those numbers as indices to your array.

commented: Thank you :) +3

Think before you speak. Thats exactly whats he talking about. Fir you need to know
how to generate even numbers such as {0,2,4...}. Then you can use those numbers as indices to your array.

If i use "if" condition so that if it is true for even numbers at the indices then it will print the value stored in them,otherwise not....will this work? or can i use pointers in some way?

If i use "if" condition so that if it is true for even numbers at the indices then it will print the value stored in them,otherwise not....will this work? or can i use pointers in some way?

Using if and modulo to determine even/odd is one of the 2 ways. You can use pointers, but you still need to generate the indexes by one of the 2 ways.

Are you familiar with the compound assignment operators, such as "x += y"? You don't always have to increment your index by 1 at the end of your loop. You can increment it by other values when appropriate.

#include <iostream>
using std::cout;
using std::endl;
using std::cin;

int main() {
  cout << "This will show all even numbers from 0 to 10." << endl;
  for (int i = 0; i <= 10; i += 2) {
    cout << i << " ";
  }
  cin.get();
  return 0;
}

Using if and modulo to determine even/odd is one of the 2 ways. You can use pointers, but you still need to generate the indexes by one of the 2 ways.

Are you familiar with the compound assignment operators, such as "x += y"? You don't always have to increment your index by 1 at the end of your loop. You can increment it by other values when appropriate.

#include <iostream>
using std::cout;
using std::endl;
using std::cin;

int main() {
  cout << "This will show all even numbers from 0 to 10." << endl;
  for (int i = 0; i <= 10; i += 2) {
    cout << i << " ";
  }
  cin.get();
  return 0;
}

Thanks Fbody

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.