Brackets are your friends.
Code:int numbers[] = { 1, 1, 2, 4, 4, 5, 7, 8, 8, 9 }; int i, j; for(i=0; i<10; i++) { for(j=i+1; j<10; j++) { if(numbers[i] == numbers[j]) cout << "Duplicate values are: " << numbers[i] << endl; } }
Can be closed, I'm an idiot
Last edited by BooYa; 10-14-2009 at 10:29 AM.
Brackets are your friends.
Code:int numbers[] = { 1, 1, 2, 4, 4, 5, 7, 8, 8, 9 }; int i, j; for(i=0; i<10; i++) { for(j=i+1; j<10; j++) { if(numbers[i] == numbers[j]) cout << "Duplicate values are: " << numbers[i] << endl; } }
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
Brackets aren't needed here, i just don't understand how these for loops work.
I'm pretty damn sure you need brackets... and even if you don't you should add them anyway, it makes it easier for people to read your code.
But anyway I see your problem. Your making j = i +1. You should make j=0. That was say the second for loop will be able to look through the entire array to find a match for element [i].
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
you can also add cout statement to make it more clear.
Code:int numbers[] = { 1, 1, 2, 4, 4, 5, 7, 8, 8, 9 }; int i, j; for(i=0; i<10; i++) { cout << "i is now" << i <<endl; for(j=i+1; j<10; j++) { cout << "j is now" << j <<endl; if(numbers[i] == numbers[j]) cout << "Duplicate values are: " << numbers[i] << endl; } }
BooYa (10-14-2009)
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
Code:for(int i = 0/*Init I as 0*/; i<10/*if I is below 10*/;i++/*increment it*/)
Ah we-a blaze the fyah, make it bun dem!
BooYa (10-14-2009)
Hmm never mind... I just didn't understand how the second for loop would work inside the first and why j was set to i+1, but i understand now after trying those cout statements from laka
Ok...if you want me to... and thanks lalakiljip.
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
BooYa (10-14-2009)