# Answer to Question #24281 in C++ for adrian thomas

Question #24281

Write a C++ function that stores all even numbers in a 2-D (4*3) array into a1-D array.Both the array declared globally

Expert's answer

#include <conio.h>

#include <iostream>

using namespace std;

int matrix[4][3] = { {8, 17, 5},

{1, 4, 22},

{9, 77, 30} };

int vector[12] = {0};

void main()

{

int lastIndex = 0;

/* Searching for even numbers in the matrix

adding them to the 1D-array (vector) */

for (int n = 0; n < 4; n++)

for (int m = 0; m < 3; m++)

if (matrix[n][m] % 2 == 1)

vector[lastIndex++] = matrix[n][m];

/* Print vector elements (until the end or zero elements are met */

for (int c = 0; c < 12 && vector[c]; c++)

cout << vector[c] << endl;

_getch();

}

#include <iostream>

using namespace std;

int matrix[4][3] = { {8, 17, 5},

{1, 4, 22},

{9, 77, 30} };

int vector[12] = {0};

void main()

{

int lastIndex = 0;

/* Searching for even numbers in the matrix

adding them to the 1D-array (vector) */

for (int n = 0; n < 4; n++)

for (int m = 0; m < 3; m++)

if (matrix[n][m] % 2 == 1)

vector[lastIndex++] = matrix[n][m];

/* Print vector elements (until the end or zero elements are met */

for (int c = 0; c < 12 && vector[c]; c++)

cout << vector[c] << endl;

_getch();

}

## Comments

## Leave a comment