HELP...new to C++, help with arrays

Discussion in 'C++' started by styles8687, Dec 5, 2007.

  1. styles8687

    styles8687 New Member

    Joined:
    Dec 2, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    Illinois
    if ur willing to help me understand one and multi-dimensional arrays plz reply to my email. styles8687@yahoo.com
     
  2. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    It doesn't work that way; this isn't 1-1.

    A one dimensional array is a list of items.
    int i[10]; declares an array of 10 ints which are accessed as i[0] through i[9].
    An example of a 1D array is a set of coordinates, for example int i[3] can store x,y and z components of a point in 3D space.

    A two dimensional array is a list of arrays.
    int i[10][10]; declares an array of 10 arrays of 10 ints which are accessed as i[0][0] through i[0][9], i[1][0] through i[1][9] and so on.
    If you're writing a program to play chess you might use a 2D array.

    An n-dimensional array simply extends the above to as many dimensions as you want, for example a five dimensional array would be int i[10][10][10][10][10];.

    Clear?
     
  3. mutantkeyboard

    mutantkeyboard New Member

    Joined:
    Nov 30, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    well man you have many tutorials on array theme :)
    as xpiotos has already said... array is just bunch of elements of some type put on one place...
    and as I saw xpiotos has also mentioned that calculation is starting from zero so it means array of 10 elements is going 0,1,2,3,4,5,6,7,8,9 if there would be 10 it would be 11 element array
    because every array contains N-1 numbers in itself...
    int x[5] allocates 5 elements of type integer
    someclassorstructname x[10] defines 10 elements of your own type
    same with pointers
    int *x = new int[5];
    this is dinamicaly allocated array of five elements
     
  4. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    Not quite. N-1 is the maximum index, but if you define int x[N] then it will contain N numbers. int x[3] defines three elements, x[0], x[1] and x[2].
     
  5. hkp819

    hkp819 New Member

    Joined:
    Dec 4, 2008
    Messages:
    59
    Likes Received:
    1
    Trophy Points:
    0
    What is a Multidimensional Array?

    A Multidimensional array is an array of arrays.

    How are Multidimensional Arrays represented in C++
    int Exforsys[3][4];
    int Exforsys[3][4][2];

    it is possible to handle multidimensional arrays of any number of rows and columns in C++ programming language. This is all occupied in memory. Better utilization of memory must also be made.

    Multidimensional Array Example:

    Code:
    #include <iostream.h>
    const int ROW=4;
    const int COLUMN =3;
    void main()
    {
       int i,j;
       int Exforsys[ROW][COLUMN];
       for(i=0;i<ROWS;i++)
       for(j=0;j<COLUMN;J++)
       {
       cout << "Enter value of Row "<<i+1;
       cout<<",Column "<<j+1<<":";
       cin>>Exforsys[i][j];
       }
       cout<<"\n\n\n";
       cout<< " COLUMN\n";
       cout<< " 1 2 3";
       for(i=0;i<ROW;i++)
       {
       cout<<"\nROW "<<i+1;
       for(j=0;j<COLUMN;J++)
       cout<<Exforsys[i][j];
    }


    The output of the above program is

    Enter value of Row 1, Column 1:10
    Enter value of Row 1, Column 2:20
    Enter value of Row 1, Column 3:30
    Enter value of Row 2, Column 1:40
    Enter value of Row 2, Column 2:50
    Enter value of Row 2, Column 3:60
    Enter value of Row 3, Column 1:70
    Enter value of Row 3, Column 2:80
    Enter value of Row 3, Column 3:90
    Enter value of Row 4, Column 1:100
    Enter value of Row 4, Column 2:110
    Enter value of Row 4, Column 3:120

    COLUMN
    1 2 3
    ROW 1 10 20 30
    ROW 2 40 50 60
    ROW 3 70 80 90
    ROW 4 100 110 120

    ...............................................................
     
  6. jsh

    jsh New Member

    Joined:
    Mar 18, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include<iostream.h>
    #include<conio.h>
    #define ROW 4;
    #define COLUMN 3;
    
    void main()
    {
      clrscr();
      int i,j,k;
      int Exforsys[ROW][COLUMN];
      cout<<".::Enter New Array::..\n";
      for (i=0;i<ROW;i++)
        for(j=0;j<COLUMN;j++)
           {
              gotoxy(5+(i*4),5+(j*2));
              cin>>Exforsys[i][j];
          }
       k=j+2;
       cout<<"\n---------\n";
       for (i=0;i<ROW;i++)
        for(j=0;j<COLUMN;j++)
           {
              gotoxy(5+(i*4),k+(j*2));
              cout<<Exforsys[i][j];
          }
    }
    

    1 10 20 30
    2 40 50 60
    3 70 80 90
    4 100 110 120
    -----------
    1 10 20 30
    2 40 50 60
    3 70 80 90
    4 100 110 120
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice