Thread: Re-Learning

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    19

    Re-Learning

    Hi, I have a quick question on some old code I have that I am going through ( I haven't done any programming for about 10 months)

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int array[10];
    	int x;
    
    	for(x = 0; x < 10; x++)
    	{
    		cout << array[x] << endl;
    	}
    
    	
    
    	return 0;
    }
    I cannot for the life of me figure out what is wrong with the program.

    I do not receive any errors, but the output is 10 values of
    -858993460.

    What am I doing that is completely wrong?

    Thanks, from a n00b re-learning C++.

    (I plan on looking back on my old books and the tutorials on this site, but I just couldn't see what was wrong.)

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    39
    ignore this post - me dumb!

    Cheers

    Alex
    Last edited by macman; 04-17-2005 at 02:00 PM.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    I am storring x (ranges from 0 - 9) into the integer array array[10].

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    39
    KK..i think ive got it...

    u need summit like this:-

    Code:
    #include <iostream>
    using namespace std;
    
    int main () {
    	
    	int array[10], x;
    	
    	for(x=0; x < 10 ; x++) array[x] = x;
    	
    	//displays array
    	for(x=0; x < 10; x++)
    		cout  << array[x] << "\n";
    	
    
    	
    	
    	
    	
        return 0;
    }
    cheers

    Alex
    Last edited by macman; 04-17-2005 at 02:01 PM.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    Thanks.

    Is there a simpler way? I thought I remembered shorter ways to make an array.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    39
    Yeh..there probably is..i dunno tho..

    to be honest..my code isnt complex..

    neways

    Hope I helped..

    cheers
    Alex

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    I know it was't complex...

    I just need to get back into programming.

    Thanks.

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    Quote Originally Posted by Kramer55
    Thanks.

    Is there a simpler way? I thought I remembered shorter ways to make an array.
    You can initialize when you declare.

    Code:
    int array[10] = {0,1,2,3,4,5,6,7,8,9};

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    39
    yeah..that would work..

    Its just depends on the amount of intergers..
    mine works well for large numbers..but hey..theres a choice now

    Cheers

    Alex

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    Just one question, what is the reason for

    Code:
    	for(x=0; x < 10 ; x++)
    	{ 
    		array[x] = x;
    	}
    Does that give the first value of x (0) in the for loop and assign it to x in the array[x] = x where that x is then used in the next for loop?

    If so, that seems almost like a pointer.

  11. #11
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
     	Just one question, what is the reason for
    
    Code:
    
    	for(x=0; x < 10 ; x++)
    	{ 
    		array[x] = x;
    	}
    This effectively does this:

    array[0]=0
    array[1]=1
    array[2]=2
    .
    .
    .
    array[9]=9


    so if you were to test something like this:

    cout<<array[2];

    the output would be '2'.

    Go read up on the tutorial for arrays in this site me thinks.


  12. #12
    Registered User
    Join Date
    Mar 2005
    Posts
    39
    Quote Originally Posted by Kramer55
    I am storring x (ranges from 0 - 9) into the integer array array[10].
    the code that I gave u..gives exactly what u wanted..

    array[0]=0
    array[1]=1
    array[2]=2


    so i agree with treenef.. Some revision is needed me thinks..but thats cool..we are all here to learn. I would suggest biting into a good c++ book..with projects n stuff..

    Neways

    Good luck with your revision..and on your path to mastering c++!

    Cheers

    Alex

  13. #13
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    Another quick question, what is the point of having size_t in this code?

    I know it returns the size, but what is the point of having the size?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main () {
        
        int array[10];
        for ( size_t i = 0; i < 10 ; ++i ) { 
            array[i] = i;
            cout << array[i] << endl;  // or just cout << i << endl;
        }
    }
    Thanks.

  14. #14
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    The reason it's doing that is because you did not define the contents of the array, so you can't expect it to print out 0-9 or something like that.

  15. #15
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Is that a typedef? A class name?

    I'm going to assume a typedef, which is a really funky typedef unless it's going to be used alot.

    When you have this code:
    Code:
    for(int i = 0; i < i < 10; i++)
    {
    
    }
    It will iterate 10 times, or more specifically, every time i is less than the value 10, and greater than or equal to the value of zero. Sometimes people confuse the "byte size" of a variable, with the "max-value" of a variable.

    Say you have the code..
    Code:
    #include <iostream>
    #include <conio.h>
    
    int main()
    {
      char *name;
      std::cout << "Enter in name\n" 
                    << "> ";
        std::cin >> name;
      for(char *c = name; *c < sizeof(name); c+=sizeof(char))
      {
    	  std::cout << *c;
      }
      getch();
    return 0;
    }
    This is an example of iterating through a sequence of variables by way of the size of the variable. Most often you'll be dealing with the size of a variable when you're working with basic optimizations and pointers.

    An alternate way of doing it would be to just use ints, as in my first example.

    Typedef'ing size_t only serves to confuse someone that looks at your code, is size_t relating to the size of the variable in bytes? In indices? Or is it just some confusing naming convention?
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Machine Learning with Lego Mindstorms
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 01-30-2009, 02:34 PM
  2. Best Approach for Learning
    By UCnLA in forum C Programming
    Replies: 5
    Last Post: 03-21-2008, 02:35 AM
  3. Need Help On a Simple Bank Program
    By oobootsy1 in forum C# Programming
    Replies: 9
    Last Post: 08-08-2005, 10:51 AM
  4. Learning Rate Of C++
    By Krak in forum C++ Programming
    Replies: 27
    Last Post: 01-29-2003, 01:53 PM