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
1
Expert's answer
2013-02-13T09:40:07-0500
#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();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS