I have a list of numbers and I want to increase by a unit the value of tot only if the element of the list is different from every element before it.
Basically I'm counting how many element the list has, if we don't consider the duplicates.
My code increases the count every time the function is different from at least 1 element.
How can i fix it?
My code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int i;
int j = 0;
int tot = 0;
int n = 11;
int list[12] = {0, 8, 2, 12, 6, 0, 4, 8, 3, 2, 3, 0};
puts("[MEMBERS COMPARED AND INDEXES]");
for (i = 1; i < n; i++)
{
for (j = i - 1; j > 0; j--)
{
printf("\n %d %d %d %d\n", list[i], list[j], i, j);
//printf("\n %d %d \n", i,j);
if (list[i] != list[j])
tot++;
}
}
puts("[TOT]");
printf("\n %d", tot);
}
The code with this sample array should produce a total of 7, but it's 42.
Complete output:
[MEMBERS COMPARED AND INDEXES]
2 8 2 1
12 2 3 2
12 8 3 1
6 12 4 3
6 2 4 2
6 8 4 1
0 6 5 4
0 12 5 3
0 2 5 2
0 8 5 1
4 0 6 5
4 6 6 4
4 12 6 3
4 2 6 2
4 8 6 1
8 4 7 6
8 0 7 5
8 6 7 4
8 12 7 3
8 2 7 2
8 8 7 1
3 8 8 7
3 4 8 6
3 0 8 5
3 6 8 4
3 12 8 3
3 2 8 2
3 8 8 1
2 3 9 8
2 8 9 7
2 4 9 6
2 0 9 5
2 6 9 4
2 12 9 3
2 2 9 2
2 8 9 1
3 2 10 9
3 3 10 8
3 8 10 7
3 4 10 6
3 0 10 5
3 6 10 4
3 12 10 3
3 2 10 2
3 8 10 1
[TOT]
42
totunconditionally on each outer-loop iteration, and then decrement it again via the inner loop in the event that you find a match. Be sure also to break from the inner loop after finding one match, else multiple matches will mess up your count.int tot = 0;