-4

i want to create a new array that include negative numbers from array t. My code is running,ı have no error but I get only 00000

#include "iostream"
using namespace std;
int main()
{
    int t[3][12]={{-2,7,6,13},{48,-5,1,-7},{16,-9,2,-9}};
    int i,j,k;
    k=0;
    int a[12];
    for(i=0; i<=2; i++)
    {
        for(j=0; j<=3; j++)
        {
            if(t[i][j]<0)
            {
                t[i][j]=a[k];
                k++;
                cout<<t[i][j];
            }
            
        }
        
    }
    cout<<endl;
}
  • 1
    What is the content of array `a` ? – Damien Jan 05 '23 at 09:22
  • What do you think happens at the `t[i][j]=a[k];` statement? – Manfred Jan 05 '23 at 09:23
  • 1
    Unrelated, but use ``, not `"iostream"` for the standard headers. – Quimby Jan 05 '23 at 09:23
  • Array `a` does not contain any initialized value. I guess your intention was to put the values to array `a'. – Nerdy Jan 05 '23 at 09:23
  • I don't see how the current duplicates have anything to do with this question. The problem is that OP have misunderstood how to do assignment. There are probably duplicates for that too though. – Ted Lyngmo May 26 '23 at 08:31
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl May 26 '23 at 09:03

1 Answers1

4

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';
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
Tom
  • 177
  • 7