Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. For Loop & Array Issue.

For Loop & Array Issue.

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structureshelptutoriallearning
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mike Certini
    wrote on last edited by
    #1

    I am in the process of learning how to use arrays. To be more specifc, how to modify the code of "for loops" to learn different ways to work with arrays. Listed below is a basic program where I am attempting to learn how to modify a two dimensional array. I would like to place a 1 in the first dimension and a 2 in the second dimension. The issue I do not understand is why my for loop is advancing to array element 2. Shouldn't the elements updated through the loop be [0] and [1]? Shouldn't my for loop stop when it encounters the for statement test condition <=1? What is happening is that the statement advances to element [2]. Do I have a correct understanding of the for loop? Advice is much appreciated.

    #include <iostream>
    using std::endl;
    using std::cout;
    using std::cin;

    int initialize[2][2]; //Initialize rows / columns
    int row;
    int col;

    int main()
    {
    // Two rows
    for(row=0;row<=1;row++) //Populate rows last.
    {
    //initialize[row][col] = 2;
    // Two columns
    for(col=0;col<=1;col++) //Populate columns first.
    {
    //cout << initialize[i][col] << "1";
    initialize[row][col] = 1;
    cout << "Row ""Col "<< row << col << initialize[row][col] << '\n';

    		}
    	}
    }
    
    L 1 Reply Last reply
    0
    • M Mike Certini

      I am in the process of learning how to use arrays. To be more specifc, how to modify the code of "for loops" to learn different ways to work with arrays. Listed below is a basic program where I am attempting to learn how to modify a two dimensional array. I would like to place a 1 in the first dimension and a 2 in the second dimension. The issue I do not understand is why my for loop is advancing to array element 2. Shouldn't the elements updated through the loop be [0] and [1]? Shouldn't my for loop stop when it encounters the for statement test condition <=1? What is happening is that the statement advances to element [2]. Do I have a correct understanding of the for loop? Advice is much appreciated.

      #include <iostream>
      using std::endl;
      using std::cout;
      using std::cin;

      int initialize[2][2]; //Initialize rows / columns
      int row;
      int col;

      int main()
      {
      // Two rows
      for(row=0;row<=1;row++) //Populate rows last.
      {
      //initialize[row][col] = 2;
      // Two columns
      for(col=0;col<=1;col++) //Populate columns first.
      {
      //cout << initialize[i][col] << "1";
      initialize[row][col] = 1;
      cout << "Row ""Col "<< row << col << initialize[row][col] << '\n';

      		}
      	}
      }
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      A loop such as for(row=0;row<=1;row++) should be read like this: 'Set row to 0, and then keep adding 1 to row until row is no longer less or equal to 1.' So what happens is this: First iteration (row is now 0): Is row <= 1? Yes; execute the code in the loop's body and when that's done, add 1 to row. Second iteration (row is now 1): Is row <= 1? Yes; execute the code in the loop's body and when that's done, add 1 to row. Third iteration (row is now 2): Is row <= 1? No; execute the code after the loop. So although row is equal to 2 áfter the loop, row is always less or equal to 1 within the loop.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups