Printing identical scores using arrays in C++

Hello guys,

This is a program that uses arrays to find the highest, lowest, and identical scores that are entered by the user. However, the code does not print out the right answer for identical numbers (after the comment line). Please tell me what code I need to write for that part using arrays and for loop, and explain your logic to me.


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
  #include <iostream>

using namespace std;

int main(){
    
    // Input values: 4 3 3 2 8
    
    const int SIZE = 5;
    int max, min, identical, score[SIZE];
    
    cout << "Enter " << SIZE << " numbers: " << " ";
    cin >> score[0];
    max = score[0];
    min = score[0];
    identical = score[0];
    
    for (int i = 1; i < SIZE; i++){
        cin >> score[i];
        if (score[i] > max){
            max = score[i];
        }
        if (score[i] < min){
            min = score[i];
        }
        //-----------------BUG---------------------
        if (identical == score[i+1]){
            identical = score[i+1];
        }
        else if (identical != score[i+1]){
            identical = score[i+1];
            for (int a = 0; a < SIZE; a++){
                if (identical == score[i+1]){
                    identical = score[i+1];
                }
            }
        }
    }
    
    cout << endl;
    cout << "The highest score is: " << max << endl;
    cout << "The lowest score is: " << min << endl;
    cout << "The identical scores are: " << identical << endl;
    cout << "The scores and the number's differences from the highest number are: " << endl << endl;
    
    for (int i = 0; i < SIZE; i++){
        cout << score[i] << " off by " << max - score[i] << endl;
    }
    
    
    return 0;
}






If you want to run the code. Here is the automatic compiler link:
cpp.sh/3cb4c

Take a look at output, and see the part which says "The identical scores are: ". I want it to output the right answer based on what you inputed.
Last edited on
what's the definition of "identical score", for example ?
By score, I mean the numbers you enter inside the array using cin.

So, imagine we entered 4 3 3 2 8,

then identical score is expected to be 3, because it is repeated twice.
The else case on line 30 is wrong and should be removed.

Line 27: if (score[i] == score[i-1]){ // Note: i-1 (otherwise it will go out of bounds)

EDIT:
This way you will find the last identical score only.
Last edited on
For example, if enter 4 3 3 2 2, what should the identical score be, 3 or 2?
@coder777 , yes I deleted the else, but it gives the wrong number.
Try 4 3 3 2 1, and it gives 4 as the identical score, but I want it to be 3.
It seems it is only considering the first element of the array.
Because identical = 4 at the beginning and


I simplified the program for you, do you know what approach is necessary now? The output gives the last element that is entered on the screen.

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
#include <iostream>
using namespace std;

int main() {
	
	//Reference: Problem Solving with C++ (pg. 381)
	
    const int SIZE = 5;
	int score[SIZE];


    cout << "Enter 5 scores: " << endl;    
	cin >> score[0];

	int identical;
	
	for (int b = 1; b < SIZE; b++){
	    cin >> score[b];
	}
	
	for (int a = 0; a < SIZE; a++){
	    identical = score[a];
    	for (int i = 1; i < SIZE; i++){
    	    //cin >> score[i];
    	    
    	    if (identical == score[i] && a!= i){
    	        identical = score[i];
    	    }
    	}	
	}
	cout << "The identical score is: " << identical << endl;

	return 0;
}



@LukeShen, yes that is the second stage of the problem. After getting it to work for atleast one number, then I need to find a way to see if I can have 3 and 2, BOTH as the identical score if I enter 4 3 3 2 2 as my input values.

I simplified the code above, still has a bug. Do you know what I should do next?
Last edited on
You think to complicated. Change your code as I told you:
1
2
3
4
5
6
7
8
9
10
11
12
13
    for (int i = 1; i < SIZE; i++){
        cin >> score[i];
        if (score[i] > max){
            max = score[i];
        }
        if (score[i] < min){
            min = score[i];
        }
        
        if (score[i] == score[i-1]){ //Change this to determine the last identical
            identical = score[i];
        }
    }


then I need to find a way to see if I can have 3 and 2
For this identical needs to be an array (or better vector).
use std::map to handle is simple,
see my code: http://ideone.com/N47raw
@coder777, I used my simplified program and I made it work, but it only works for one identical value. Ex. 4 4 3 3 2 , gives 3 and not 3 and 4.

How can I make identical an array and use it?



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
#include <iostream>
using namespace std;

int main() {
	
	//Reference: Problem Solving with C++ (pg. 381)
	
    const int SIZE = 5;
	int score[SIZE];


    cout << "Enter 5 scores: " << endl;    
	cin >> score[0];

	
	int identical = score[0];
	for (int b = 1; b < SIZE; b++){
	    cin >> score[b];
	}
	
	for (int a = 0; a < SIZE; a++){
	    
    	    //cin >> score[a-1];
    	for (int i = 1; i < SIZE; i++){
    	    
    	    
    	    if (score[a] == score[i] && a!= i){
    	        identical = score[i];
    	        //break;
    	    }
    	}
	}
	
	cout << "The identical score is: " << identical << endl;

	return 0;
}



@Luke Shen, Thanks for your program, but I could barely understand it because I have just started coding in C++. Its good to see a similar program does the same function as mine.
@LukeShen

Why so complicated? Version with map, see
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
  #include <iostream>
#include <map>

using namespace std;

int main(){
    
    // Input values: 1 1 2 3 3 4 5 5 2 6
    
    const int SIZE = 10;
    int max, min, identical, score[SIZE];
    std::map<int, int> identical_map;
    
    cout << "Enter " << SIZE << " numbers: " << " ";
    cin >> score[0];
    max = score[0];
    min = score[0];
    
    for (int i = 1; i < SIZE; i++){
        cin >> score[i];
        if (score[i] > max){
            max = score[i];
        }
        if (score[i] < min){
            min = score[i];
        }
    }
    for (int i = 0; i < SIZE; i++){
        ++(identical_map[score[i]]);
    }
    
    cout << endl;
    cout << "The highest score is: " << max << endl;
    cout << "The lowest score is: " << min << endl;
    cout << "The identical scores are: ";
    for (std::map<int, int>::const_iterator i = identical_map.begin(); i != identical_map.end(); ++i){
        if(i->second > 1)
          cout << i->first << " ";
    }
   cout << endl << "The scores and the number's differences from the highest number are: " << endl << endl;
    
    for (int i = 0; i < SIZE; i++){
        cout << score[i] << " off by " << max - score[i] << endl;
    }
    
    
    return 0;
}


Without map:
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
64
65
66
67
68
69
70
#include <iostream>
#include <map>

using namespace std;

int main()
{

  // Input values: 4 3 3 2 8

  const int SIZE = 10;
  int max, min, identical[SIZE], identical_count[SIZE], score[SIZE];
  int identical_size = 0;

  cout << "Enter " << SIZE << " numbers: " << " ";
  cin >> score[0];
  max = score[0];
  min = score[0];

  for(int i = 1; i < SIZE; i++)
  {
    cin >> score[i];
    if(score[i] > max)
    {
      max = score[i];
    }
    if(score[i] < min)
    {
      min = score[i];
    }
  }
  for(int i = 0; i < SIZE; i++)
  {
    bool is_found = false;
    for(int j = 0; j < identical_size; j++)
    {
      if(identical[j] == score[i])
      {
        is_found = true;
        ++(identical_count[j]);
        break;
      }
    }
    if(!is_found)
    {
      identical[identical_size] = score[i];
      identical_count[identical_size] = 0;
      ++identical_size;
    }
  }

  cout << endl;
  cout << "The highest score is: " << max << endl;
  cout << "The lowest score is: " << min << endl;
  cout << "The identical scores are: ";
  for(int i = 0; i != identical_size; ++i)
  {
    if(identical_count[i] > 0)
      cout << identical[i] << " ";
  }
  cout << endl << "The scores and the number's differences from the highest number are: " << endl << endl;

  for(int i = 0; i < SIZE; i++)
  {
    cout << score[i] << " off by " << max - score[i] << endl;
  }


  return 0;
}
@coder777
std::map sorts the keys automatically, so your map version program can be more simple.
The min score is identical_map.begin()->first.
The max score is identical_map.rbegin()->first.
@coder777
wow! thanks a lot, your code makes sense, I understand the syntax better. :)

@coder777 and @LukeShen, could you guys also tell me what do you mean by version with 'map' and version without 'map'?


Is there any way I can give you guys bonus/prize ? You guys deserve it after all this explanation and help.
what do you mean by version with 'map' and version without 'map'?
In the first code 'Version with map' I am using std::map (you may search for the word 'map') in the other version only plain arrays to accomplish the same thing. So there are several ways to do the same thing.

I suggest that you use a debugger and go step by step through the code and see what the variables contain. That is the best way to gain more understanding.

EDIT:
LukeShen wrote:
std::map sorts the keys automatically, so your map version program can be more simple.
You are right, but I didn't consider the min/max problem in miy code.
Last edited on
Awesome explanation, thanks again for the feedback.
Topic archived. No new replies allowed.