loops

This is a discussion on loops within the C++ Programming forums, part of the General Programming Boards category; can some how this part of code to be replaced with a loop(for or while) Code: if(x==2) a[0]=(soberi(a[0],a[1])); if(x==3){ a[0]=(soberi(a[0],a[1])); ...

  1. #1
    Performer
    Join Date
    Jan 2007
    Location
    Macedonia
    Posts
    54

    Question loops

    can some how this part of code to be replaced with a loop(for or while)
    Code:
    if(x==2)
    a[0]=(soberi(a[0],a[1]));
    if(x==3){
    a[0]=(soberi(a[0],a[1]));
    a[0]=(soberi(a[0],a[1]));
    a[0]=(soberi(a[0],a[2]));
    a[0]=(soberi(a[0],a[3]));
    a[0]=(soberi(a[0],a[4]));
    }
    if(x==6)
    {
    a[0]=(soberi(a[0],a[1]));
    a[0]=(soberi(a[0],a[2]));
    a[0]=(soberi(a[0],a[3]));
    a[0]=(soberi(a[0],a[4]));
    a[0]=(soberi(a[0],a[5]));
    }
    if(x==7)
    {
    a[0]=(soberi(a[0],a[1]));
    a[0]=(soberi(a[0],a[2]));
    a[0]=(soberi(a[0],a[3]));
    a[0]=(soberi(a[0],a[4]));
    a[0]=(soberi(a[0],a[5]));
    a[0]=(soberi(a[0],a[6]));
    }
    if(x==8)
    {
    a[0]=(soberi(a[0],a[1]));
    a[0]=(soberi(a[0],a[2]));
    a[0]=(soberi(a[0],a[3]));
    a[0]=(soberi(a[0],a[4]));
    a[0]=(soberi(a[0],a[5]));
    a[0]=(soberi(a[0],a[6]));
    a[0]=(soberi(a[0],a[7]));
    }
    if(x==9)
    {
    a[0]=(soberi(a[0],a[1]));
    a[0]=(soberi(a[0],a[2]));
    a[0]=(soberi(a[0],a[3]));
    a[0]=(soberi(a[0],a[4]));
    a[0]=(soberi(a[0],a[5]));
    a[0]=(soberi(a[0],a[6]));
    a[0]=(soberi(a[0],a[7]));
    a[0]=(soberi(a[0],a[8]));
    }
    if(x==10)
    {
    a[0]=(soberi(a[0],a[1]));
    a[0]=(soberi(a[0],a[2]));
    a[0]=(soberi(a[0],a[3]));
    a[0]=(soberi(a[0],a[4]));
    a[0]=(soberi(a[0],a[5]));
    a[0]=(soberi(a[0],a[6]));
    a[0]=(soberi(a[0],a[7]));
    a[0]=(soberi(a[0],a[8]));
    a[0]=(soberi(a[0],a[9]));

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Something involving
    while ( i < x )
    perhaps?

    Or
    for ( i = 0 ; i < x ; i++ )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Performer
    Join Date
    Jan 2007
    Location
    Macedonia
    Posts
    54
    yes how to use for and while but i have problem to
    rest of the code i dont now what to write after for

  4. #4
    Performer
    Join Date
    Jan 2007
    Location
    Macedonia
    Posts
    54
    sorry about my english

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Dunno - maybe replace
    a[0]=(soberi(a[0],a[1]));
    with
    a[0]=(soberi(a[0],a[i]));

    Why are you assigning to the same location each time?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Performer
    Join Date
    Jan 2007
    Location
    Macedonia
    Posts
    54
    i dont now other way

  7. #7
    Performer
    Join Date
    Jan 2007
    Location
    Macedonia
    Posts
    54
    is this code is going to be same like that above
    Code:
    for(i=0;i<x;i++)
    a[0]=(soberi(a[0],a[i]));

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    More or less.

    Though it seems you should start at 1, not 0

    Also, your cases 2 and 3 don't seem to fit the pattern that the rest of them follow - is that just a typo on your part?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Performer
    Join Date
    Jan 2007
    Location
    Macedonia
    Posts
    54
    yes it's mistake in typing it's should be
    Code:
    if(x==3){
    a[0]=(soberi(a[0],a[1]));
    a[0]=(soberi(a[0],a[2]));
    }
    if(x==4)
    {
    a[0]=(soberi(a[0],a[1]));
    a[0]=(soberi(a[0],a[2]));
    a[0]=(soberi(a[0],a[3]));
    }
    if(x==5)
    {
    a[0]=(soberi(a[0],a[1]));
    a[0]=(soberi(a[0],a[2]));
    a[0]=(soberi(a[0],a[3]));
    a[0]=(soberi(a[0],a[4]));
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 12:18 AM
  3. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM
  4. for loops in C
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-15-2001, 05:09 PM
  5. exiting loops with ease?
    By KingRuss in forum Game Programming
    Replies: 3
    Last Post: 09-24-2001, 08:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21