Column

Write your question here.
So im trying here to solve an exercise by using files but i don't get why the site of our university does not approve of it. So i have two integers, N which is the numbers of rows and M the number of columns. Every column is consisted of
numbers ( which start from the smallest to the bigger one from left to right. Also the numbers can be very large so i want my program to accept numbers till billion and they start from 1. The output should contain several lines. Each line must contain two natural numbers separated by a space - the number of columns and the number of lines in which this number occurs. Numbers of columns should go in ascending order. Here are two examples: Input in kare.dat

3 4
1 2 3 4
3 5 6 7
1 2 3 5
Output in kare.sol

1 2
2 2
3 3
4 1
5 2
6 1
7 1
Input in kare.dat

3 3
1 1 1
4 4 6
7 9 9
Output in kare.sol

1 1
4 1
6 1
7 1
9 1
So this is my program which for some reason does not work.
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
61
62
63
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
	long long arr1[100][100],  arr2[100];
	vector < long  long > myvector;
	int N;
	long long M;
	int P = N * M;
	int count = 1;
	int number = 0;
	ifstream dat;
	ofstream sol;
	dat.open("kare.dat");
	dat >> N >> M;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			dat >> arr1[i][j];
		}
	}
	dat.close();
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			myvector.at(j + i) = arr1[i][j];
		}
	}
	
	for (int i = 0; i < P; i++) {
		for (int j = 0; j < P; j++) {
			if (myvector.at(j)  > myvector.at(j+1)) {
				swap(myvector.at(j), myvector.at(j+1));
			}
		}
	}
	int e = 0;
	for (int i = 0; i <= P; i++) {
		if (myvector.at(i) == myvector.at(i + 1)) {
			count++;
		}
		else {
			arr2[e] = count;
			e++;
			count = 1;
		}
	}
	number = myvector.size();
	for (int i = 0; i < number; i++) {
		if (myvector.at(i) == myvector.at(i + 1)) {
			myvector.erase(myvector.begin() + (i + 1));
			i = i - 1;
			number = number - 1;
		}
	}
	sol.open("numb.sol");
	for (int i = 0; i < number; i++) {
		sol << myvector.at(i) << " " << arr2[i] << endl;
	}
	sol.close();
    return 0;
}
Last edited on
> but i don't get why the site of our university does not approve of it.
¿did you ever try to run it?


> The output should contain several lines. Each line must contain two natural
> numbers separated by a space - the number of columns and the number of lines
> in which this number occurs.
¿what? ¿what number?


Lost in translation, I guess.
Last edited on
my program which for some reason does not work.

That's not a very helpful explanation of your problem. What happens?

i don't get why the site of our university does not approve of it

Not sure that I believe that; your code is (presumably) in a file after all. Sounds like you might be unable to find and/or read your input file. Put
if ( !dat ) { cout << "Can't open file"; return 1; }
after line 16 and make sure that you can actually read the file.

To be honest, I can't work out what your code is trying to do, and there are some issues in your description of the problem. However, from the sample input and output it looks like:
"At the end, output each integer present in the array and the number of lines it occurs in".
Please confirm.

There's obviously a significant issue with duplicates - see the second example.

If my interpretation is correct, here's a suggestion (after you've checked that you can open the data file).
- Have a map<int,int> to hold the distinct elements and the cumulative number of lines they are present in.
- Go through the data file row by row.
- On each row, use a set<int> to hold the distinct integers.
- After assembling this set of distinct integers, compare each element with those in the map. Add one to the line count for each element already in the map, or a new element with line count 1 if not already in the map.
- Clear the set before going to the next row.


It's very different from your code, so if you want to use your code instead ... please put some comments in to explain what it is trying to to do.
> i don't get why the site of our university does not approve of it
>> Not sure that I believe that; your code is (presumably) in a file after all.
a source code may be compiled to obtain an executable file
later, that program may be executed and its output compared against the expected output.
Topic archived. No new replies allowed.