Thread: newb question on ARRAYS

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    37

    newb question on ARRAYS

    hi.. I'm trying to learn how to use arrays with a for loop. I basically want to create an array of 10 integers and set the values from 1 to 10. Here is what i have so far but it doesn't even run..?? thanks in advance( keep in mind, i am a newbie so be gentle...) Also, how would i do this with a while loop?
    Code:
    #include <stdio.h>
    main()
    {
    int x;
    int array[20];
    for(int x=0; x<20; x++)
      cout<<array[x];
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    That's not setting the values of the array that's just outputting the empty array elements. Also, why are you including stdio but using cout?

    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
       int array[20];
       for (int x = 0; x < 20; x++) {
          array[x] = x;
          cout << array[x] << endl;
       }
    
       return 0;
    }
    Last edited by SlyMaelstrom; 11-16-2005 at 04:10 PM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    SlyMaelstrom, your code has an error. The for loop should go while x < 20, not <=.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    you shouldnt use x<=20 because then it would try and access an invalid element.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    12
    stdio.h is a C header file. Change it to

    Code:
    #include <iostream>
    Also, main should return an int.

    Beyond that, you define int x twice, one local to main, and one local to the for loop; only one is necessary.

    Onto the actual concept...

    All your for loop is doing is ACCESSING the elements, not SETTING them. To set an element of an array, you say something like:

    Code:
    array[x] = element;
    Where array is your array's name and element is a piece of data with the same type as your array.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    #include <stdio.h>
    main()
    {
    int x;
    int array[20];
    for(int x=0; x<20; x++)
      cout<<array[x];
    }
    1) 'cout<<' is defined in <iostream>, so you have to #include <iostream> not <stdio.h>. In addition, the modern rules of C++ require that the standard header file names not end in ".h".

    2) It's

    int main() ---> not main()

    and main() should have a return statement. Here is the program template all beginners should use:
    Code:
    #include <iostream> 
    using namespace std;
    
    int main()
    {
    	//Your code here
    
    	return 0;
    }
    3) Always initialize your variables. That means set them to a value when you declare them, for example:
    Code:
    int x = 0;
    If you don't know what value to set them to, then set them to 0. Otherwise, they can contain random junk values. To set all the elements of an array to 0, do this:
    Code:
    int array[20] = {0};
    4) Your array: array[20] is really like 20 variables: array[0], array[1], array[2].....array[19]. The numbers 0,1, 2,.....19 are called the index positions. For a variable to have a value, you have to "assign" the variable a value:

    array[0] = 10;
    array[15] = 3;

    5) For-loops can be used with great effect with arrays. For-loops have a variable that changes with each loop:
    Code:
    for(int i=0; i <10; i++)
    {
    
    }
    'i' increases by one after every loop. You can also make the index position of an array a variable:

    array[ i];

    As 'i' changes in the for-loop, it will change the index position in the array. In the for-loop, 'i' is initially set to 0, which corresponds to the first index position in an array(arrays start at index position 0--not index position 1). After the first loop,'i' is increased by 1 and then the loop executes again. The second time through the loop the int i = 0 part is skipped--that only executes the first time through the loop. As 'i' counts upwards, array[ i] will change from array[0] to array[1] to array[2], etc.

    It's very important to remember that since arrays start at 0, an array of size 20 will have index positions 0-19, and therefore array[20] is undefined.
    Last edited by 7stud; 11-16-2005 at 04:15 PM.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Daved
    SlyMaelstrom, your code has an error. The for loop should go while x < 20, not <=.
    Yeah my bad. Wasn't fully paying attention.
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    37

    thank you

    thanks to everyone for the thorough explanation... I have another question though... HOW would i do this same task using the while function for arrays? Thanks again for helping me understand.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The for loop has three parts, the initialization, the test and the increment. The initialization goes before the while loop runs, the test is the control of the loop, and the increment goes at the end of the loop block. Make those changes and you are done.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    37
    Quote Originally Posted by Daved
    The for loop has three parts, the initialization, the test and the increment. The initialization goes before the while loop runs, the test is the control of the loop, and the increment goes at the end of the loop block. Make those changes and you are done.

    I'm sorry Daved...but i'm not quite following...

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Think about the three parts.

    Initialize your variable
    Test the control at the beginning
    Increment variable at the end

    The syntax for a while loop is

    Code:
    while(condition) {}
    Now where would you put the other parts?
    Sent from my iPadŽ

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    37
    i think i got it to work... how does this look?

    Code:
    #include <iostream>
    #include <stdio.h>
    using namespace std;
    
    int main()
    {
    
       int array[20];
       int x = 0;
       while (x < 20)
       {
       x++;
          array[x] = x;
          cout << array[x] << endl;
      }
    
      cin.get();
    }

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Almost there. But, your program could easily crash because of one minor error.

    Remember that in a for loop, the increment part is done at the end of the loop block. If you are converting a for loop to a while loop, you want to do the same thing.

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    37
    do you mean like this?

    Code:
    #include <iostream>
    int main()
    {
       int array[20];
       int x = 1;
       while (x < 21)
       {
    
          array[x] = x;
          cout << array[x] << endl;
          x++;
      }
    
      cin.get();
    }
    the program started at 0 to 19...so i changed it to 1 and 21... Is this correct?

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    No, you didn't have to change all that stuff, you just had to make the change I mentioned.

    Remember that array indexes go from 0 to size-1. So because your array has a size of 20, the indexes you use should be 0 through 19. With both your two latest attempts, you attempt to use index 20, which will often lead to a crash.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about arrays
    By TomButcher in forum C++ Programming
    Replies: 3
    Last Post: 09-02-2005, 09:27 AM
  2. A question concerning character arrays
    By ellipses in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 08:24 PM
  3. basic question to Arrays
    By doneirik in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2005, 02:57 AM
  4. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  5. question on multiplying 2D arrays
    By 3kgt in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2003, 08:26 PM