Linking arrays

Heloo,

I create a function for entry with ids. The code is below.Now I need create a function for enter with the markers by id.
Ex: id markers
12SA AABBCD
1345 BBABCC
How can I do that?

void ide(int nanim)
{

cout << "Enter the number of animals: ";
cin >> nanim;

string *id = new string[nanim];
for ( size_t i=0; i < nanim; i++ )
{
cout<< "\nEnter id anim "<< i+1 << ": ";

cin >> id[i];
}

for ( size_t i = 0; i < nanim; i++ )

{
cout << id[i] << endl;
}

}
Last edited on
You can use an array of type std::pair<std::string, std::string>. For example

std::pair<std::string, std::string> *id = new std::pair<std::string, std::string>[nanim];
for ( size_t i=0; i < nanim; i++ )
{
cout<< "\nEnter id anim "<< i+1 << ": ";

cin >> id[i].first;
cout<< "\nEnter marker anim "<< i+1 << ": ";
cin >> id[i].second;
}


for ( size_t i = 0; i < nanim; i++ )

{
cout << id[i].first << '\t' << id[i].second << endl;
}
I tried this code, it was compiled without errors but nothing happen when I run;

int main()
{ int nanim;
pair < string,string> *id= new pair<string,string>[nanim];
for ( size_t i=0; i < nanim; i++ )
{
cout<< "\nEnter id anim "<< i+1 << ": ";

cin >> id[i].first;
cout<< "\nEnter marker anim "<< i+1 << ": ";
cin >> id[i].second;
}


for ( size_t i = 0; i < nanim; i++ )

{
cout << id[i].first << '\t' << id[i].second << endl;
}

return 0;
}


Thank you!

First of all you did not initialize nanim. So the size of the array is uknown.

{ int nanim;
pair < string,string> *id= new pair<string,string>[nanim];

And secondly size of an array shall be a constant expression.
I do a code below...
But I need um array with ids and other array with genotype.
How can I return these ararys?

void * ide(int nanim, int nsnp)
{

cout << "Enter the number of animals: ";
cin >> nanim;
cout << "Enter the number of snps: ";
cin >> nsnp;

string *id = new string[nanim];
string *genotype = new string[nanim];
string snp= "";
for ( size_t i=0; i < nanim; i++ )
{
cout<< "\nEnter id anim "<< i+1 << ": ";
cin >> id[i];
for (size_t j=0; j<nsnp;j++)
{
cout<< "SNP "<< j+1<< " Anim "<< i+1 << ": ";
cin >> snp;
genotype[i]=genotype[i]+snp;
}

}
for ( size_t i = 0; i < nanim; i++ )
{

cout << "\n"<<id[i] << "\t "<< genotype[i]<< endl;
}

}
Last edited on
In this case it will be conveniently to use std::map instead of the array. For example


1
2
3
4
5
6
7
8
std::map<std::string, std::string> m;

m["1"] += "AA";
m["1"] += "BB";
m["2"] += "AB";
m["2"] += "AA";

for each ( const auto &p : m ) std::cout << p.first << ":\t" << p.second << std::endl;


Also take into account that maybe it is better to use as the key type size_t because as it is seen you are using non-negative numbers.
How can I do using make_pair?
If you are asking about std::map then I have not understood the question. I showed you an example of using std::map.

As for std::make_pair then it is simple

std::make_pair( "1", "AA" );
or

std::make_pair( std:;string( "1" ), std:;string( "AA" ) );
I tried the code below for return a array with id; but there was a error in int main

[Error] variable-sized object 'anim' may not be initialized...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[#include <iostream>
#include <string>
using namespace std;

[code]string* getArray(int nanim)
{
	cout << "Enter the number of animals: ";
	cin >> nanim;

	string *id = new string[nanim];
	for ( size_t i=0; i < nanim; i++ ) 
	{
		cout<< "\nEnter id anim "<< i+1 << ": "; 
		cin >> id[i];
		}
		for ( size_t i = 0; i < nanim; i++ ) 
		{
		cout << id[i] << endl; 
		}
	return (new string[nanim]);
}

int main()
{
	int n;
 	string anim[n]=getArray(n)]
Last edited on
As I said early the size of the array shall be a constant expression and you may not initialize an array with a pointer.
Last edited on
How can I do that using this function?
thank you! Sorry for basic questions...
Last edited on
He means something like this
1
2
const int SIZE = 1e9;
int array[ SIZE ];


Edit - Can you please use code tags? They are on the right side of the reply/edit box they look like
<>

You can also do something like
[code]//stuff[/code]
which looks like this
//stuff
Last edited on
I tried the code, but not worked;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
using namespace std;

string* getArray()
{	int nanim;
	cout << "Enter the number of animals: ";
	cin >> nanim;

	string *id = new string[nanim];
	for ( size_t i=0; i < nanim; i++ ) 
	{
		cout<< "\nEnter id anim "<< i+1 << ": "; 
		cin >> id[i];
		}
		for ( size_t i = 0; i < nanim; i++ ) 
		{
		cout << id[i] << endl; 
		}
	return id;
}

int main()
{
 	 int n;
	 cin>>n;
	string* anim[n]=getArray();
 	cout<<anim;
 	return 0;
}
Last edited on
25
26
27
28
int n;
cin >> n;
string* anim[n] = getArray();
cout << anim;


1) n is not a constant value, so you cannot declare *anim[n].
2) getArray returns a pointer to an array but *anim[n] is an array of pointers. Just use *anim.
3) You are displaying the memory address anim first points to, or &anim[0]. If you want to display the strings pointed to by anim, just use a for loop.
4) What is the point of variable n when the user has to reenter the number of animals in getArray()? I think you could do with eliminating everything in main except for a call to getArray and return 0. Plus, if you do this, change the return type of getArray to void and get rid of the return statement.
5) You never deallocate memory allocated by your getArray function.
Last edited on
Topic archived. No new replies allowed.