looping help

This is a discussion on looping help within the C++ Programming forums, part of the General Programming Boards category; assuming cols=2 and rows=3 ([0,0][0,1] [1,0][1,1] [2,0][2,1]) Code: int i=0; cout<<"cols="<<cols<<endl; cout<<"rows="<<rows<<endl; cout<<endl; do{ j=0 cout<<"Number at position "<<i<<","<<j<<" is ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    looping help

    assuming cols=2 and rows=3
    ([0,0][0,1]
    [1,0][1,1]
    [2,0][2,1])

    Code:
    int i=0;
       cout<<"cols="<<cols<<endl;
       cout<<"rows="<<rows<<endl;
       cout<<endl;
       do{
          j=0
          cout<<"Number at position "<<i<<","<<j<<" is ";
          matrix[i][j]=readFloat("",true);
          j++;
          if(j=cols)
          {i++;}
          else
          {cout<<"Number at position ("<<i<<","<<j<<") is ";
          matrix[i][j]=readFloat("",true);}
          }while(j<=cols);
    how can i edit this little bit of code to loop back to the beginning, i know i need to have j restart at 0, thats why the j=0 in inside the code and not out. Do i need to put a do loop inside the loop to take care of the i++ part of the code?

    I have been coding straight for pretty much the better part of 2 day, and i'm hoping a fresh and/or more experienced set of eyes can help give me some insight as to what i have to do.

    thanks

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    i revised my code to
    Code:
    int j;
       int i=0;
       cout<<"cols="<<cols<<endl;
       cout<<"rows="<<rows<<endl;
       cout<<endl;
       do{
          j=0;
          cout<<"Number at position "<<i<<","<<j<<" is ";
          matrix[i][j]=readFloat("",true);
          do{
          j++;
          if(j=cols)
          {i++;}
          else
          {cout<<"Number at position ("<<i<<","<<j<<") is ";
          matrix[i][j]=readFloat("",true);}
          }while(j!=cols);
          }while(i!=rows);
    and am not getting outputs for the first column
    [has value][no value]
    [has value][no value]
    [has value][no value]

    small progress, but knowing my luck, i may have taken a step backwards

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    revised yet again, new code is
    Code:
    int j;
       int i=0;
       cout<<"cols="<<cols<<endl;
       cout<<"rows="<<rows<<endl;
       cout<<endl;
       do{
          j=0;
          cout<<"Number at position "<<i<<","<<j<<" is ";
          matrix[i][j]=readFloat("",true);
          while(j<cols)
          {j++;
          cout<<"Number at position ("<<i<<","<<j<<") is ";
          matrix[i][j]=readFloat("",true);
          }
          i++;
          }while(i<rows-1);
       
       i=0;
       j=0;
       
       do{
          j=0;
          cout<<matrix[i][j]<<" ";
          while(j<cols)
          {j++;
          cout<<matrix[i][j]<<" ";
          }
          i++;
          cout<<endl;
          }while(i<rows-1);
    only problem is now instead of outputting data like
    [1][2]
    [3][4] it is outputting data like [1][2][3]
    [5][6] it is outputting data like [4][5][6]

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You print the first element in every row twice (once before the loop and once in the loop). (You also appear to not print the last row at all, since the loop on i stops way early.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getlines and looping
    By Blanked in forum C++ Programming
    Replies: 5
    Last Post: 12-01-2009, 07:26 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 07:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 02:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 12:01 AM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:12 AM

Tags for this Thread


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