2d array print line numbers of char

Input:

1
2
3
4
1; ; ;
 ;1; ;
 ; ;1;
 ; ; ;


Ouput:

1
2
3
4
1=2=3=
 = = =
 = = =
 = = =


How to print the linenumber of the '1' on one line so the information is reordered?
You have to explain more.

Why does the example output have more than one row? Are '1' and ' ' the only possible values in the input? Can same column have '1' in more than one row? If so, what should the output have then?
When the input is:

1
2
3
4
1;2; ;
 ;1; ;
 ; ;1;
 ; ;2;


The output must be:

1
2
3
4
1=2=3=
 =1=4=
 = = =
 = = =


The numbers in the input can be '1' to '4' or ' '. The '2' in the input says that the information must be printed on the second line, and the linenumber where the '2' stands must be printed on this line (exactly on the column where it stands in the input). This is the first challenge. When you have this you can work further on it so it works as follows:

When the input is:

1
2
3
4
1; ; ;
 ;1; ;
 ; ;1;
 ; ;1;


The output must be:

1
2
3
4
1=2=3,4=
 = =   =
 = =   =
 = =   =


When the input is:

1
2
3
4
1;1; ;
 ;1; ;
 ; ;1;
 ; ; ;


The output must be:

1
2
3
4
1=1,2=3=
 =   = =
 =   = =
 =   = =
Ok, you have K columns. On row n of output, in column k, you do list (comma-separated) the rows of input that have value n in column k.

You obviously have to store the information in useful way.
Something crude would do for "exactly 3 columns and values 1-4". For example:
std::vector<int> data[5][3];
(That does have a surplus row just to avoid some index calculation.)

On row n, column k you would store a value x into data with data[x][k].push_back(n);
Now I have:

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
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>

int main ()
{

    
    std::vector< std::vector< int > > arr;
    std::ifstream infile("mozzarella.txt");
    std::string line;
    int places = 0;
    int lines = 0;
    std::string finger;
    for (int i = 0; i < 4; i++)
    {
        std::getline(infile, line);
        std::stringstream linestream(line);
        arr.resize(4);
        for (int j = 0; j < 3; j++)
        {
            getline(linestream,finger,',');
            int fingerint = atoi(finger.c_str());
            arr[i].push_back(fingerint);
        }
    }
    
    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            std::cout << arr[i][j] << ',';
        }
        std::cout << '\n';
    }
    
    return 0;
}


with output:

1
2
3
4
1,0,0,
0,1,0,
0,0,0,
0,0,1,


How do I have to accomplish a solution to the problem now?
1. You have only two dimensions, while you actually need three: a list of rows for each column-value pair.

2. You don't treat blanks specially, and hence get those 0's all over.
The program with 3 dimensions:

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
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>

int main ()
{

    
    std::vector< std::vector< std::vector< int > > > arr;
    std::ifstream infile("mozzarella.txt");
    std::string line;
    int places = 0;
    int lines = 0;
    std::string finger;
    for (int h = 0; h < 4; h++)
    {
        arr.resize(4);
        for (int i = 0; i < 4; i++)
        {
            std::getline(infile, line);
            std::stringstream linestream(line);
            arr[i].resize(4);
            for (int j = 0; j < 3; j++)
            {
                getline(linestream,finger,',');
                int fingerint = atoi(finger.c_str());
                arr[h][i].push_back(fingerint);
            }
        }
    }
    
    for (int h = 0; h < 4; h++)
    {
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                std::cout << arr[h][i][j] << ',';
            }
            std::cout << '\n';
        }
    }
    
    return 0;
}


When I try:

 
arr[fingerint][i].push_back(h);


I am getting a segmentation-fault. I do not understand your solution fully. Can you modify the program so it has requested output?
Why do you have a triple nested loop on the input? You only have 2D data table.

On output, you would not know how many elements (if any) the innermost vectors do have.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const int Rows {5};
const int Cols {3};
std::vector<int> data[Rows][Cols];

// fill data

for ( int row = 1; row < Rows; ++row ) {
  std::cout << "Value " << row << " occurs on rows: ";
  for ( int col = 0; col < Cols; ++col ) {
    for ( size_t x = 0: x < data.size(); ++x ) {
      if ( x ) std::cout << ',';
      std::cout << data[row][col][x];
    }
    std::cout << '=';
  }
  std::cout << '\n';
}
Topic archived. No new replies allowed.