Thread: Array question on my simple program?

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Wink Array question on my simple program?

    Greetings,
    I am not recieving any errors but my program keeps getting 0 for my answer.
    What do you get is my ? From my computations should i not get an array of numbers from this code?
    Check it out, pls!
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    	const int LITTLE = 6,
    		      MEDIUM = 10,
    			  BIG = 128;
    
    	int i, j, n = 9,
    		temp,
    		number[MEDIUM] = { 99, 33, 44, 88, 22, 11, 55, 66, 77};
    
    	char ch,
    		letterCount[BIG];
    
    	typedef double LittleDouble[LITTLE];
    
    	LittleDouble value;
    
    int main() 
    {
    	for (i = 1; i < LITTLE; i += 2)	
    	{
    		value[i - 1] = double(i) / 2.0;
    		value[i] = 10.0 * value[i - 1];	
    	}
    
    		cout <<number[0]<<nl<<number[1]<<nl<<number[2]<<nl
    		 <<number[3]<<nl<<number[4]<<nl<<number[5]<<nl
    		 <<number[6]<<nl<<number[7]<<nl<<number[8]<<nl
    		 <<number[9]<<nl<<nl;
    	return 0;}


    It works now!!!
    Thanks!
    Last edited by correlcj; 10-21-2002 at 11:07 AM.
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  2. #2
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    EDIT: Whoops... no, I was wrong
    Last edited by BMJ; 10-21-2002 at 10:47 AM.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    for clarity sake I would do this:

    const int LITTLE = 6;
    const int MEDIUM = 10;
    const int BIG = 128;

    I'm not sure if the keyword const will apply to MEDIUM and BIG the way you have it. Maybe, maybe not.
    ----------------------------------------------------------------------

    I would try changing this:

    typedef double LittleDouble[LITTLE];

    to this:

    typedef double[LITTLE] LittleDouble;

    -------------------------------------------------------------------------
    return 0;

    is the appropriate way to terminate int main().

    Heaven only knows how the compiler will evalute

    return value[i];

    -------------------------------------------------------------------------
    i is declared globally, so it will never be out of scope. Well... not in this namespace anyway.

  4. #4
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    your right elad.... my bad

  5. #5
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Hello1

    Heaven only knows how the compiler will evalute
    I doen't, i have compiled no errors.
    I thought maybe it would list like so:
    number[0] = 0,
    number[1] = 0
    ...
    number[6] = 3,
    number[7] = 3'
    ...
    number[9]=0
    end!

    but it seams as if i have to type it in manually e.g;
    cout << number[7] or
    cout << number[3] for my answers.

    How can I correct it so I can print them 0 - 9?
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  6. #6
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    I got it to work now guys. I edited my post to correct the error I made. Thanks for your help! I couldn't have done it without your input!!
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    .
    .
    .
    
    for (i = 1; i < LITTLE; i += 2)	
    	{
    		value[i - 1] = double(i) / 2.0;
    		value[i] = 10.0 * value[i - 1];	
                                    cout << value[i];
    	}
    
    	
    return 0;
    }//end main

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Well, your cout has to be in the loop, not outside. Ad elad is right, you can't predict what value you'll get with return value[i], since i has no defined value at that point, and may not even be an integer.
    If your processing was done in another function called by main, you could then return value[i].
    Code:
    .
    .
    .
    double DoTheMath(int j);   // declare function
    
    int main()
    {
       for ( i = 1; i <  Little; i +=2)
            {
                cout << DoTheMath(i);
            }
       return 0;
    }
    
    double DoTheMath( int j )  // implement function
    {
       // do the processing
       return value[j];
    }
    Truth is a malleable commodity - Dick Cheney

  9. #9
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    Well, your cout has to be in the loop, not outside. Ad elad is right, you can't predict what value you'll get with return value[i], since i has no defined value at that point, and may not even be an integer.
    Your right, the both of you!
    But I don't get the right answer that way. The way I have shown above somehow gives me the correct answers,
    Can you explain why, pls?
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    in the for loop you are incrementing by 2 with the += 2. Therefore i will only be 1, 3, 5 since LITTLE is a constant with the value of 6 and i starts at 1, is incremented by 2 and must be less than 6.

    if you are changing the numbers in the array value with the loop, but not using the changes at all, and just outputting the information in the array called numbers I wonder why you bother with the loop at all, unless this is just for practice writing loops, etc. At the moment I see no connection between the arrays called value and the arrays called number except that they occur in the same program.

  11. #11
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    At the moment I see no connection between the arrays called value and the arrays called number except that they occur in the same program.
    It's a typo!
    I am working on 4 programs at once and got confused. SORRY, elad!
    Thanks for your input!
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  12. #12
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    As a side note, I'd suggest using functions similar to my example above. Main should be used mostly for calling other functions. Why? When your programs get bigger, it'll be much easier to write and maintain code when it's a collection of small functions, each of which only do one small thing. It also allows your code to be re-used more easily. I have to maintain code that was written w/o documentation and with functions that do lots of contorted stuff.
    Sometimes you can't get around it, but whenever you can, break your code into the simplest functions you can.
    Truth is a malleable commodity - Dick Cheney

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  2. simple Array question.
    By Fredir in forum C++ Programming
    Replies: 6
    Last Post: 10-06-2007, 07:12 AM
  3. multidimensional array function - simple question
    By boltz in forum C++ Programming
    Replies: 6
    Last Post: 05-23-2005, 04:24 PM
  4. simple array question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2002, 10:43 PM
  5. A simple array question
    By frenchfry164 in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2001, 04:13 PM