I'm trying to write a program that will associate the first column of numbers with the second column of numbers so I can easily sum the numbers in the second column.
As an example, 1 in the first column occurs twice, and I would need to add the values in the second column associated with the number 1, 35 + 32 = 67 for 1. There is 25 numbers in the first column.
#include <iostream>
#include <fstream>
#include <cstdlib>
usingnamespace std;
constint TYPES = 25;
ifstream infile;
ofstream outfile;
void read(int numbers[])
{
int sum = 0, i = 0;
infile.open("data.txt");
if (!infile)
{
cout << "Failed to open data file!";
cin.get();
exit(0);
}
infile >> numbers[0];
while (infile)
{
infile >> sum;
infile >> numbers[i];
i++;
}
}
int main()
{
int numbers[TYPES];
read(numbers);
cin.get();
return 0;
}
I've done something like this in perl before using hashes to associate keys with other data, but I'm not sure if c++ has this. Do I need to use a two-dimensional array for this? Thank you if you can help with this.
vector<pair<unsigned, unsigned> > array;
pair<unsigned, unsigned> element;
element.first = // first value
element.second = // second value
array.push_back(element);