The error is in t[i][j]=a[k]
, it should be a[k] = t[i][j]
.
You are however not using a
so the cout<<t[i][j];
doesn't really give you any verification that you've filled a
correctly.
You've also defined the inner array of t
to have 12
elements, but you only explicitly initialize and use 4
of them. By changing t
to int t[3][4]
you can simplify the program by using range-based for
loops and by printing out the initialized values in a
after the outer loop you'd get a verification that you assigned the correct values to a
.
Example:
#include <iostream>
int main() {
// You only use 4 elements in the inner array, not 12:
int t[3][4] = {{-2, 7, 6, 13}, {48, -5, 1, -7}, {16, -9, 2, -9}};
// This calculates the correct size of `a` even if you change the
// number of elements in `t`:
int a[sizeof t / sizeof(int)];
unsigned k = 0;
for(int(&inner)[4] : t) { // a range-based for loop, easier written as
// for(auto& inner : t)
for(int value : inner) { // another range-based for loop
if (value < 0) {
a[k] = value;
++k;
}
}
}
// Verify that the values in a [0,k) are correct:
for(unsigned i = 0; i < k; ++i) {
std::cout << a[i];
}
std::cout << '\n';
}