-2

I have a 2d array, that row get name and column get age but I little bit missing on this.

I will leave a pseudo code.

#include <iostream>

int main()
{
    std::cout << "Hello World!\n";
    int a[4][1];
    int i, j;
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 1; j++)
        {
            a[i][j] = name, age;
        }
    }
}

should be returning somthings like:

a[0][0] = "joao",12
a[1][1] = "maria",22
a[2][2] = "jose",40
a[3][3] = "jose",50
JeJo
  • 30,635
  • 6
  • 49
  • 88
  • 5
    honestly I think you are misunderstanding the structure of a 2d array (you are using only the elements along the diagonal thats a waste of memory). The example of what you want to add looks like you rather want a `std::map` that has names as keys and the age as value – 463035818_is_not_an_ai Jan 15 '19 at 19:09
  • You can define a struct with 2 elements, name (string) and age (int) – Damien Jan 15 '19 at 19:10
  • 3
    *Stop* using C-style arrays *now*. Use `std::array` or `std::vector` instead. Also, please post a [mcve] - we can't tell what `name` or `age` are. – Jesper Juhl Jan 15 '19 at 19:12
  • `int a[4][1]` is not a 2D array. It's a 1D array. The number in `[]` indicates the number of elements. You probably want to declare it as `int a[4][2]`. – ilya1725 Jan 15 '19 at 19:12
  • 2
    This is an array of integers and you're expecting it to be able to handle strings? You need to set the type correctly. C++ is not a "do what I mean" type language, you need to dictate *exactly* what you want done. Making up C++ code and hoping it compiles is not a productive exercise. Get a good C++ reference book and build up your knowledge before assuming it works the way you want it to. This will save you considerable pain and suffering. – tadman Jan 15 '19 at 19:12
  • 6
    It sounds you want to learn from a [good book](https://stackoverflow.com/a/388282) in 1st place, instead of making arbitrary assumptions how the c++ programming language works. – πάντα ῥεῖ Jan 15 '19 at 19:12
  • 2
    `a[i][j] = name, age;` is functionally `a[i][j] = age;`. – François Andrieux Jan 15 '19 at 19:14
  • 1
    @FrançoisAndrieux Yeah, the comma operator trips up many people ;) – Jesper Juhl Jan 15 '19 at 19:16
  • _@CerraossoUC_ Go for what was mentioned in the 1st comment. You don't want a 2d array of `int` but a [std::map](https://en.cppreference.com/w/cpp/container/map) there. – πάντα ῥεῖ Jan 15 '19 at 19:22

1 Answers1

2

First off, your types are wrong. a has integers, and a name is not exactly an integer.

What a better solution would be (using STL) is to use a pair of a string and an integer (you could use a char for all it matters, unless you want people to be 2,147,483,000 years old).

int array_size = 4; // as an example
// the array, but now the type is of a pairing of string and integer.
std::array<std::pair<std::string, int>, array_size> a; 
a[0] = {"joao", 12}; // how to set a name 

Or whatever names you want.

Daniel
  • 854
  • 7
  • 18
  • Why not a `std::map` in 1st place? – πάντα ῥεῖ Jan 15 '19 at 19:23
  • @πάνταῥεῖ I could be wrong about this but my understanding was that std::map would require unique keys - in the example provided, there's two joses and the keys wold be identical. You *could* go the other way and make the ages the keys, but I'm assuming we don't know if the ages provided are guaranteed to be unique. A std::array allows you to have the same name/age several times. – Daniel Jan 15 '19 at 19:27
  • 1
    _"there's two joses"_ Well, then there's still [`std::multimap`](https://en.cppreference.com/w/cpp/container/multimap) for the rescue in such case. – πάντα ῥεῖ Jan 15 '19 at 19:29
  • That's valid, however a std::map still would not be a good solution. I didn't think of std::multimap. +1 for creative thinking, but my main issue with this is that we don't know if the array needs to be in a specific order. It seems to be a lot harder to insert the names and ages in order. My question to you is why not use std::array? It's a simple structure to use that's easy for someone new to c++ to pick up. – Daniel Jan 15 '19 at 19:33
  • Fair point. But all of these discussions are far beyond of the misconceptions shown in that question. – πάντα ῥεῖ Jan 15 '19 at 19:37