A PHP Error was encountered

Severity: Warning

Message: fopen(/var/cpanel/php/sessions/ea-php56/PHPSESSIDn0vu09tugn8bf7caeebgc2jb7hsf7d1s): failed to open stream: No space left on device

Filename: drivers/Session_files_driver.php

Line Number: 174

How to skip some of the array elements in C++?
Q:

How to skip some of the array elements in C++?

0

How to skip some of the array elements in C++?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Consider the program: In this program each third element of the array is skipping.

#include <iostream>
using namespace std;

int main()
{
	int arr[10] = {1,2,3,4,5,6,7,8,9,10};
	int i;
	
	for(int i=0; i<10; i++)
	{
	  if((i+1)%3 == 0)  //If index is every third element
		continue;  //Continue
	  cout<<arr[i]<<" ";  //Print array element
	}
	
	return 0;
}

Output

1 2 4 5 7 8 10 

We are using % (modulus operator) to check every 3rd iteration. If it is, the continue will skip everything down in loop's scope and continue executing the next iteration. For all other elements it will print them.

Small trick, but might come handy in future. If you find this amazing, leave comments below.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Most popular and Searched C++ solved programs with Explanation and Output

Similar questions


need a help?


find thousands of online teachers now
C++ program to keep calculate the sum of the digit... >>
<< Example of declaring and printing different consta...

A PHP Error was encountered

Severity: Warning

Message: Unknown: Failed to write session data (user). Please verify that the current setting of session.save_path is correct (/var/cpanel/php/sessions/ea-php56)

Filename: Unknown

Line Number: 0