help to display an array

This is a discussion on help to display an array within the C++ Programming forums, part of the General Programming Boards category; Im working on a program that will esentially open a file read it and output the info on a new ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    18

    help to display an array

    Im working on a program that will esentially open a file read it and output the info on a new file. The contents of the file is simply an array...so i start by declaring the array however when i want the C: window to display my output
    instead of getting my array i get 0012FF34..what gives?

    is it throwing me a random value instead of displaying my array or is it
    giving me the address

    thanx in advance

    im a newbie to C++ so please cope with my lack of knowledge

    juju

    Code:
    #include <iostream>
    
    using namespace std;
    int main()
    {
        
        int jane[4][3]= {{0,0,0},{0,1,1}, {1,0,1}, {1,1,0} };
        
    
    
    
    	cout << "the matrix is:\t" << jane << endl;
        
        
        return 0;
        
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Use nested loops, print each element.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    18
    so it would be something like:

    Code:
    .
    .
    .
    .
    
    int jane[4]={ A,B,C,D};
    
    cout << "the matrix is" << jane << endl;
    
    or
    
    cout << A<< endl;
    cout << B<< endl;
    .
    .
    .
    return 0;
    }
    juju

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    18
    I over simplified the array by giving it a simpe 1,2,3,4 input and it still gives me
    a similar address like output...

    juju

  5. #5
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    To access individual elements in an array you need to use something like cout << jane[element];

    So use a loop to loop through, incrementing a counter, to print out each element.

    Any tutorial on arrays you can find online would tell you how to do all this.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    18

    Talking

    thanx let me try that


    juju

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    18
    yes it actually holds for the simplified version...it should hold for the "more complex matrix" i initially though i had to mess with main or do something like:

    Code:
    void main(int n,int i,int j);

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    18
    well it gave me 4 adresses in matrix order but ill keep on moving it around to see if it works

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Nested loops, each element:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       int jane[4][3]= {{0,0,0},{0,1,1}, {1,0,1}, {1,1,0}};
    
       cout << "the matrix is:\n";
       for ( size_t i = 0; i < sizeof jane / sizeof *jane; ++i )
       {
          for ( size_t j = 0; j < sizeof *jane / sizeof **jane; ++j )
          {
             cout << jane[i][j] << ' ';
          }
          cout << '\n';
       }
       return 0;
    }
    
    /* my output
    the matrix is:
    0 0 0 
    0 1 1 
    1 0 1 
    1 1 0 
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    18

    Talking

    thanx dave..you didnt have to take the time and work on my program....

    i greatly appreciate it

    I owe you lunch

    juju

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. Array won't display new values?
    By Kespoosh in forum C++ Programming
    Replies: 9
    Last Post: 03-17-2003, 08:22 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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