how do i make repeating numbers appear once

i read in a file and arrange the file to something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1
2
3
3
4
5
7
7
8
8
9
9
9
10
10
10
10


what i want is for all the numbers to appear only once. theres no 6 btw.
all i got right now for the code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	
	for (int x = 0; x < size; x++)
	{	
		for (int num = 1; num < 11; num++)
		{
			int count = 0;
			if (p[x] == num)
			{
				count++;
				cout << p[x] << endl;
				x = x + count;
			}	
		}
	}


the output for this is:

1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
7
8
9
9
10
10
The idea is simple. Create an array of numbers.

Now, when you read a number from file, check to see if it is in the array. If it is not, add it to the array. But if it is already in the array, do nothing.

You will be helped to keep the array sorted at all times -- but that is not strictly necessary. You could sort it afterward. Or, assuming your input is always sorted, you can avoid the sorting entirely and just check to see whether the last number is the same as the current number.

Good luck!
if the numbers are strictly in ascending order you could simply just read the first number in a temporary variable then start reading next numbers when you encounter a new number you output the number stored in the temporary variable then you assign the new encountered number to the temporary variable, and repeat those steps

1
2
3
4
5
6
7
8
9
10
11
int temp = p[0];
cout << p[0] << " ";
for(int i = 1; i < size; i++)
{
      if(p[i] != temp)
         {
                cout << p[i] << " ";
                temp = p[i];
         }
}
Last edited on
@g3nom3

thanks that worked. but uh... idk how to say this lol... theres a second part to this xD. originally i just wanted to make the numbers appear only once before i moved on(there was actually more numbers). but with the code that you gave, it doesnt really work with the second part.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
person              amount        amount2
1                       51              10
2                       105             43
3                       159             56
3                       150             19
4                       101             29
5                       215             98
7                       111             50
7                       222             108
8                       256             122
8                       206             120
9                       341             190
9                       336             200
9                       400             210
10                      250             123
10                      320             120
10                      230             100
10                      330             125


i was supposed to add the amount corresponding to the person. so for person 10, the total amount should be 1130 and the total amount2 should be 468.

sorry for not being specific xP. honestly i thought if i could just find a way to change the output layout, i can squeeze in the code to calculate the total. but i couldnt x( but thanks tho
That's actually a completely different question than the one you asked.

Assuming you need to remember what each person's total is, make some arrays for the total amounts. Initialize the amounts to zero:

1
2
int total_amounts[11] = {0};
int total_amount2s[11] = {0};

Now get input:

1
2
3
4
  int person;
  int amount;
  int amount2;
  while (cin >> person >> amount >> amount2)

For each input, add each amount to the corresponding person's position in each array.
Last edited on
i think the questions are quite similar...

this is what i had in mind: if i can get the code to print all the person# once, then i can add some code onto that loop to add all the corresponding amounts at the same time.

even though i didnt include this but i already read in from the file in my main code and called a function to arrange them from lowest to highest. now i want to call another function within the first function to add up the amounts corresponding to its person#.

sorry it took a little while to respond. im currently trying your array of totals and still havent gotten any further xd
Topic archived. No new replies allowed.