I want to assign three roll numbers to three names but using only one array.. Please tell me how can i do that in c++. I think it is done using struct and arrays
-
3Please show what you've tried. – Adam Martin Jul 12 '16 at 15:32
-
I am student of software engineering in college and i don't know much about arrays... i know simple and multidimensional arrays but i don't know how to show roll nums to students using one array...i can do it by using two arrays.. – Anony Developer Jul 12 '16 at 15:36
-
You need to explain what you mean by roll numbers. Show some examples of what you're trying to map, and then show an example of how you would do it using two arrays. – Adam Martin Jul 12 '16 at 15:38
-
1Please put clarifications/sample output in the question instead of in comments. – Adam Martin Jul 12 '16 at 15:41
-
what i mean by roll numbers are just numbers like 10,23,50 etc.. i want output like: Adam John David 50 30 20 now i want this using only one array.. if I'd do this by two arrays then i'd define two array, one of char[3][10] and one of int[3]... char array will have names and int array will have numbers of those names.. – Anony Developer Jul 12 '16 at 15:42
-
by using two arrays I'd do like this:: int main(){ char names[3][10]; int numbers[3]; names={adam,david,john}; numbers={10,20,30}; cout << names << endl; cout << numbers << endl; return 0; } – Anony Developer Jul 12 '16 at 15:49
-
@AnonyDeveloper You should probably read a basic C/C++ programming book/tutorial first. Product types are simple elements of programming that should be covered in the very first chapters. – KABoissonneault Jul 12 '16 at 15:51
2 Answers
You can make an array of std::pair. Prefer std::vector over raw arrays in c++, also highly prefer std::string over just an array of char.
#include <iostream>
#include <utility> //std::pair
#include <string>
#include <vector>
using namespace std;
int main()
{
//Raw array approach
pair<string, int> nameNumbersArr[3];
nameNumbersArr[0] = make_pair("Adam", 50);
nameNumbersArr[1] = make_pair("John", 30);
nameNumbersArr[2] = make_pair("David", 20);
//Vector approach
vector<pair<string, int>> nameNumbersVec;
nameNumbersVec.push_back(make_pair("Adam", 50));
nameNumbersVec.push_back(make_pair("John", 30));
nameNumbersVec.push_back(make_pair("David", 20));
return 0;
}
You can access the different parts of a pair using .first or .second
Example:
cout << nameNumbersVec[2].first << ' ' << nameNumbersVec[2].second << endl;
will print
David 20

- 1,536
- 3
- 17
- 32
It sounds like you're trying to make a map. In C++, std::map
is a built-in, so something like this may be what you're looking for:
#include <map>
#include <string>
std::map<int, std::string> stdNames;
stdNames[10] = "Bob";
stdNames[23] = "John";
stdNames[50] = "Mary";
Now if you do not have access to built-ins, you can implement a mapping structure yourself. std::map
is usually implemented as a red-black tree, though you can use an avl-tree or any other kind of self-balancing binary tree for the same purpose.
Now it sounds like you're fairly early in your degree, so you may not be comfortable implementing these yourself. In that case, you may want to create a Record
struct, and have an array of those.
e.g. (typdefs/structs)
#define MAX_LEN 100
typedef struct {int rollNumber;
char name[MAX_LEN];} Record;
Record studentRecords[3];
Note that in this approach is really C not C++, and requires care around name lengths. Julian's answer shows you a C++ approach for this kind of result.
It really does sound like you need to read a couple of books from here though: The Definitive C++ Book Guide and List

- 1
- 1

- 1,188
- 1
- 11
- 24
-
2
-
From the OPs comments I figured they were really using C, but then they used `cout`... I could remove it, will wait to see what they add from the comments into their question. – Adam Martin Jul 12 '16 at 15:58