-3

There’s a problem I have trouble solving. Shortly written, here’s how it goes:

There are N professors at a school, born in a certain year and you need to find the amount of years the professors share (if two professors share a year, it’s one year which is repeated).

Example: Input: (first line) 3 (second line) 1975 1960 1975 Output: (third line) 1

I’ve started somehow, and I have only managed to create the input:

int N;
cin >> N;
int array[N];

for (int i = 0; i < N; i++) {
     cin >> array[i];
}

Now, I don’t know how to continue, or use the information (years) the user has entered.

Edit: Although there might be better ways of solving this, I am just looking for a simple solution, or a code explaining how I can continue this program.

Edit: It should display the amount of years which repeat.

N.T.
  • 99
  • 6
  • 2
    You don't know how to continue for two reasons: variable length arrays, as shown in the code sample, is not standard C++. Furthermore, an array is the wrong approach. This should involve nothing more than simply a `std::unordered_map`, with the year being the key, and the value being the number of times this year occurs in the input. As such, this problem basically solves itself. Mission accomplished. – Sam Varshavchik Feb 24 '19 at 01:48
  • I don’t understand. I’m a beginner when it comes to programming, I started a few months ago. This was a problem which was given to me and I was told that it could be solved using an array. I was told that I was supposed to create another for-loop with another for-loop within it to make this work. If there’s a chance for anyone to explain that way of solving this, although it might not be the best one? – N.T. Feb 24 '19 at 01:59
  • I'd suggest ```std::set_intersection``` and as Sam said, you are allocating the array incorrectly, this will not compile; you should look into ```std::vector```. – nick Feb 24 '19 at 02:07
  • @nick I am aware that this might not be the best or the most correct solution. Since I am a beginner, this is how the problem was explained to me when it was given. I was just asked to continue it and find a way to make the code work. As far as I remember, it was some nested for-loops I should add, but I don’t remember exactly how. Could you help? – N.T. Feb 24 '19 at 02:14
  • @N.T. This line ```int array[N];``` will not compile, regardless of whether there is a better way or not. The compiler will not allow you to put a dynamic array onto the stack. You have the option of changing it to a ```std::vector``` which is the easy way, or using heap memory with ```int array = new int[N];```. But as it stands, it doesn't matter what you want, this code is not functional. – nick Feb 24 '19 at 02:15
  • Well, it seems, then, if that this is how it's explained, and that's how you told it should be, then whoever explained and told it to you would be the best source to ask for further advice and directions; because noone here knows specifically what information and directions were given to you. Your teacher, or instructor, or whatever the case may be, gets paid to teach and help their students, and this is their job, to help you. – Sam Varshavchik Feb 24 '19 at 02:15
  • @nick How should I change the code, and how will I be able to use the information the user enters? I am still a bit confused. – N.T. Feb 24 '19 at 02:17
  • @N.T. Look at this to address your memory allocation error: https://stackoverflow.com/a/25088877/9176689 – nick Feb 24 '19 at 02:20
  • If we assume that somehow this code works and somehow it’s so far great and awesome... Would there be a way for me to just make another for-loop which would check if there are two equal values entered by the user? – N.T. Feb 24 '19 at 02:23
  • And would it be okay if I changed int N; to const int N; ? – N.T. Feb 24 '19 at 02:47
  • @N.T. *I am just looking for a simple solution* -- We don't know what you mean by "simple solution". The simplest is `int teacher; std::unordered_set tset; for (int i = 0; i < N; ++i) { cin >> teacher; tset.insert(teacher); } std::cout << tset.size();` -- That is what basically is described by SamV. – PaulMcKenzie Feb 24 '19 at 15:48
  • @PaulMcKenzie By simple I meant code that as a **very** beginner I’d be able to understand, just a way to continue my already written code. I don’t understand the ‘unordered_set tset’ part. I apologize if I’m a bit too uninformed but I am really at the very beginning of programming. – N.T. Feb 24 '19 at 16:34
  • @N.T. An unordered_set is your "array", but does not store duplicates. Was that a hard concept, even for a beginner? You can insert items into the "set", and it will automatically not allow duplicates. That, IMO, is far more easier to comprehend (even if you're a beginner), than the solution being suggested of contorting `for` loops to figure out how many unique birth years there are. So what is produced after the set is populated? An "array" of unique values, and all you need to do is get the number of entries (`size()`). Problem solved. – PaulMcKenzie Feb 24 '19 at 18:30
  • And if not `set`, if you want to have a count, then `unordered_map` can be used, each item in the map contains a key (the year), and a value (the number of times the year appears). That is *not* an advanced concept -- read the description of these types, not the syntax. – PaulMcKenzie Feb 24 '19 at 18:39
  • *just a way to continue my already written code.* -- As mentioned, your code is already broken due to usage of invalid C++ syntax. So you're not learning C++ to be honest with you. You would use `std::vector`, not `array[N]`. But let's give you that start -- the "continuation of your code" would simply be: `std::unordered_set tset(array, array+N); std::cout << tset.size();`. – PaulMcKenzie Feb 24 '19 at 18:48
  • @PaulMcKenzie The compiler doesn’t show any errors when I’m compiling the code though. I’m confused. – N.T. Feb 24 '19 at 18:50
  • That is because you are compiling your code in non-ANSI mode. Try `-Wall -pedantic`. You are another victim of the g++ compiler defaulting to non-ANSI mode, and you thought you were writing valid C++ code when you were not. If you need proof, get the Visual Studio compiler and attempt to compile your code -- it will stop with an error. – PaulMcKenzie Feb 24 '19 at 18:54
  • Well, you could just change to `std::vector arr(N);`, and keep the rest of the syntax (it isn't a good idea to name a variable `array`, since `std::array` exists in C++). – PaulMcKenzie Feb 24 '19 at 18:59
  • @PaulMcKenzie What are vectors used for? – N.T. Feb 24 '19 at 18:59
  • 1
    A vector is a dynamic array -- basically what you're trying to achieve by using the invalid shortcut. But this should be covered in any good C++ book. – PaulMcKenzie Feb 24 '19 at 19:00

1 Answers1

0

In your program above, you were able to have the user input how many years they are entering as well as the years you are comparing. Now to get the output, you should use for loops to iterate through the array and compare the years.

For example: User entered: 1975 1960 1975 In your code this will be stored as: array[0] = 1975, array[1] = 1960, array[2] = 1975.

One option I would suggest would be: Iterate through the array and count the number of duplicates. If we find a higher count, then set that as the highest number of duplicates. Example: Read in 1975 and compare with each element if there are duplicates. If we find a duplicate, then increase the counter. If the counter is greater than the highest count, then that becomes the highest count. Repeat this process for the entire array.

Program:

int N;
cin >> N;
int A[N];
int highest_count = 0;
for (int i = 0; i < N; i++) {
    cin >> A[i];
}
for (int i = 0; i < N; i++) {
    int counter = 0;
    for (int j = i; j < N; j++) {
        if (A[i] == A[j] {
            counter++;
        }
        if (counter > highest_count) {
            highest_count = counter;
        }
    }
}
cout << highest_count;
A. Rey
  • 61
  • 3
  • I left a snippet of what would you need to do. Try to figure out how to implement it. – A. Rey Feb 24 '19 at 10:09
  • I have _tried_ to do something, it's not really working, but I feel like I am getting there. `int N; cin >> N; int A[N]; int counter = 0; for (int i = 0; i < N; i++) { cin >> A[i]; } for (int i = 0; i < N; i++) { for (int j = i; j < N; j++) { if (A[i] == A[j]) { counter++; } } } cout << counter; return 0; }` It doesn't turn out to work, though. – N.T. Feb 24 '19 at 10:48
  • Yes you are getting close to solving it. Just to understand the question more, are you just trying to display how many professors have one year in common? For example, let's say 2 professors share a year 1960 and 3 professors share a year 1975, are we supposed to display 3? Or do we display 2 and 3? – A. Rey Feb 24 '19 at 17:37
  • Well I should display the amount of years which are repeated as far as I can see... Here’s how the examples given to me go: Input: 3; 1975 1960 1975; Output: 1 (as in 1 year is repeated 1975) and another one Input: 5; 1980 1990 1980 1970 1990; Output: 2; (as in two years repeating - 1980 and 1990) – N.T. Feb 24 '19 at 17:50
  • So it looks like we want to display the highest number of years repeating? In that case, I added an update to your current program. The program has a variable highest_count to store the highest amount of duplicate years we find. The variable counter is just to count how many duplicates there are each iteration. Your task now is to find how to update the highest_count when we find a counter that is greater than the current highest_count. – A. Rey Feb 24 '19 at 20:31
  • It would be another if statement: if (counter > highest_count) highest_count = counter; – A. Rey Feb 26 '19 at 21:32
  • I edited the program. I suggest going through the code and trying to understand each line. – A. Rey Feb 26 '19 at 21:38