2-D Array Problem!!

I have an assignment where I have to read from an input file into a two-dimensional array.The input file has about 208 lines of numbers, two numbers to a line. The first number is the salesman ID, the second number is how much he made for a sale. There are 11 different salesmen, and their ID will show up multiple times in the input file.We are supposed to read it into an array with 11 rows and 2 columns.My problem is when I read from the file, how do I only add the 11 different id numbers and keep a total of how much they sold? Here is a code snippet of how I tried:
int ary[11][2];
int i = 0;
float id, charge;
inFile >> id >> charge;
while (inFile)
{
if (id != ary[i][0])
{
ary[i][0] = id;
ary[i][1] = charge;
}
i++;
inFile >> id >> charge;
}

When I try to output the array it is not correct. Someone please help!! Thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int ary[11][2];
int i = 0;
float id, charge;
bool found;
inFile >> id >> charge;
while (inFile)
{
found=false;
for(int x=0;x<=i; x++){
    if(id==ary[x][0]){
          ary[x][1]+=charge;
          found=true;
    }
}
if(found==false){
     ary[i][0]=id;
     ary[i][1]=charge;
     i++;
}
inFile >> id >> charge;
}
Edit: Please use pyschoder solution as his is more elegant and compact than mine.

I believe this is an assignment and you cannot use STL containers like vector, map etc correct ? It is also strange we hard-code to 11 rows. Anyway below is code snippet for reference. It is not good coding design but at least it get the work done.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

int getArrayFree(int ary[11][2],int salesmanID) {
    bool found=false; int index=0;
    for(int i=0; i<11; i++) {
      for(int j=0; j<1; j++) {
        if (ary[i][j] == -1) {
          index = i; found = true; break;
        }
      }
      if (found) break;
    }
    return index;
}

int inArray(int ary[11][2],int salesmanID) {
    bool found=false; int index=0;
    for(int i=0; i<11; i++) {
      for(int j=0; j<1; j++) {
        if (ary[i][j]==salesmanID) {
          index = i; found = true; break;
        }
      }
      if (found) break;
    }
    return found ? index : -1;
}

int main() {
  int salesmanID, charge;
  int ary[11][2] = {
    {-1,0},
    {-1,0},
    {-1,0},
    {-1,0},
    {-1,0},
    {-1,0},
    {-1,0},
    {-1,0},
    {-1,0},
    {-1,0},
    {-1,0}
  };
  ifstream ifs("test.txt",ifstream::in);
  while (ifs >> salesmanID >> charge) {
   int index = inArray(ary,salesmanID);
   if (index != -1) {
     ary[index][1]+=charge;
   } else if (index == -1) {
     index = getArrayFree(ary,salesmanID);
     ary[index][0] = salesmanID;
     ary[index][1] = charge;
   }
  }
  ifs.close();

  for(int i=0; i<11; i++)
    cout << "salesmanID: " << ary[i][0] << " Charge: " <<ary[i][1] << "\n";

  return 0;
}
Last edited on
Topic archived. No new replies allowed.