Results 1 to 10 of 10
  1. #1
    BooYa's Avatar
    Join Date
    Apr 2009
    Gender
    male
    Location
    hre
    Posts
    111
    Reputation
    10
    Thanks
    19

    A for() loop in a for() loop

    Can be closed, I'm an idiot
    Last edited by BooYa; 10-14-2009 at 10:29 AM.

  2. #2
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    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

  3. #3
    BooYa's Avatar
    Join Date
    Apr 2009
    Gender
    male
    Location
    hre
    Posts
    111
    Reputation
    10
    Thanks
    19
    Brackets aren't needed here, i just don't understand how these for loops work.

  4. #4
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    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

  5. #5
    lalakijilp's Avatar
    Join Date
    Jan 2008
    Gender
    male
    Posts
    310
    Reputation
    9
    Thanks
    53
    My Mood
    Blah
    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;
                  }           
        }

  6. The Following User Says Thank You to lalakijilp For This Useful Post:

    BooYa (10-14-2009)

  7. #6
    BooYa's Avatar
    Join Date
    Apr 2009
    Gender
    male
    Location
    hre
    Posts
    111
    Reputation
    10
    Thanks
    19
    Quote Originally Posted by why06 View Post
    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].
    lol read my question, the code works fine but i simply dont understand how it works

  8. #7
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    Quote Originally Posted by BooYa View Post
    lol read my question, the code works fine but i simply dont understand how it works
    So you don't know how a for loop works?

    "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

  9. #8
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    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!

  10. The Following User Says Thank You to Hell_Demon For This Useful Post:

    BooYa (10-14-2009)

  11. #9
    BooYa's Avatar
    Join Date
    Apr 2009
    Gender
    male
    Location
    hre
    Posts
    111
    Reputation
    10
    Thanks
    19
    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

  12. #10
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    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

  13. The Following User Says Thank You to why06 For This Useful Post:

    BooYa (10-14-2009)

Similar Threads

  1. [HELP]"for" loop
    By Drake in forum C++/C Programming
    Replies: 4
    Last Post: 02-20-2011, 05:26 AM
  2. 2 for(;;) loops
    By maarten551 in forum Call of Duty Modern Warfare 2 GSC Modding Help/Discussion
    Replies: 5
    Last Post: 10-26-2010, 09:28 AM
  3. Hacks for mmorpg?
    By suppaman in forum General Game Hacking
    Replies: 6
    Last Post: 10-17-2010, 11:04 AM
  4. [Source]FOR Loops
    By HypnoticBabeTrap in forum C++/C Programming
    Replies: 13
    Last Post: 12-27-2009, 06:20 PM
  5. Too Ownage For Words.
    By Flawless in forum Art & Graphic Design
    Replies: 8
    Last Post: 12-28-2005, 04:55 PM

Tags for this Thread