array definitions

This is a discussion on array definitions within the C++ Programming forums, part of the General Programming Boards category; greetings, i am to write a definition and statement to construct a given array. 1. an array whose indices are ...

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

    Question array definitions

    greetings,

    i am to write a definition and statement to construct a given array.

    1. an array whose indices are the integers from 0 thru 99 and in which the value stored in each array element is the same as the indices in reverse order.

    Is my answer wrong???



    code:--------------------------------------------------------------------------------
    int array[100];
    for(int i = 0; i < 100; i--)
    array[i] = [i];
    --------------------------------------------------------------------------------
    "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
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    YES!! it is!

    Thankls but i corrected it


    Code:
    int array[100];
    for(int i = 0; i < 100; i--)
    array[i] = [i];
    Thanks anyways for ALL your help.


    "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

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    28

    Re: array definitions

    yeah, that's an infinite loop. it should be something like

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int array[100];
    	int garbage = 0; //just used to pause execution. you can take this out if you want.
    	int j = 0;
    
    	for (int i = 99; i > 0; i--)
    	{
    		j = (100 - i);
    		array[j] = i;
    		cout<< "Array["<<j<<"] is "<<array[j] <<endl;;
    		if (i%20 == 0)
    			cin>>garbage;
    	}
    	return 0;
    }
    EDIT: unless, of course, I misunderstood the question. which is possible.
    Last edited by Will; 11-08-2002 at 06:37 PM.

  4. #4
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    unless, of course, I misunderstood the question. which is possible
    NOPE! U got it! Thanks Will!
    cj
    "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

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    17

    array

    Can I write in this way:

    #include <iostream>
    using namespace std;

    int main()

    {
    int array[100];
    int garbage = 0;
    int j = 0;

    for (int i = 0; i < 99; i++)
    {
    j = (100 - i);
    array[i] = j;
    cout<< "Array["<<i<<"] is
    " << array[j]<<endl;

    if (i%20 == 0)
    cin>>garbage;
    }
    return 0;
    }

  6. #6
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Correct me if I'm wrong.
    Yeah this willl loop until the value of i becomes geater than what an integer can take, then you will get an error.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    17
    I thought the loop will stop when i is less than 99

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
      int array[100];
      int garbage = 0; 
      int j = 0;
    
      for (int i = 0; i < 100; i++)
     {
        j = (100 - i); //j will range from 100 to 1 as i will be 99 at max
        array[i] = j;
        cout << "Array[" <<  i << "] is" << array[j] << endl; //always put spaces between operators, etc.  It makes it much easier to read
    
       if (i%20 == 0)//nice trick to freeze the screen every 20 lines
       //except the first time, when it will be 21 lines.  
       cin >> garbage;
     }
     return 0;
    }
    Please read about use of code tags to retain indentation, etc., when posting to the board.

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    17
    So I was very close .
    Thanks for the reply and explanation.
    I love this Forum.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d array definitions
    By jsrig88 in forum C++ Programming
    Replies: 4
    Last Post: 03-25-2009, 03:18 AM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  3. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

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