Thread: number of columns and lines in my array

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    54

    number of columns and lines in my array

    I don't understand why my code doesn't display an array of 6 lines and 6 columns as i wish.

    My code displays an array with 10 columns and 3 lines.
    Than you in advance for the help.


    Here is my code :
    Code:
    void initArray(float largeArray[5][5])
    {
        for (int x=0; x < 5; x++)
            {
            for (int y=0; y<5; y++)
                {
                largeArray[x][y] = 0;
                }
            }
    
    
    }
    
    
    float printOutArray(float largeArray[5][5])
    {
        for (int x = 0; x < 5; x++)
            {
                for (int y = 0; y < 5; y++)
                    {
                        cout << largeArray [x] [y]<<'\t';
    
    
                    }
    
    
            }
    return largeArray[5][5];
    }
    
    int main()
    {
        float largeArray [5][5];
        initArray(largeArray);
        printOutArray(largeArray);
    }

  2. #2
    Registered User
    Join Date
    Feb 2013
    Posts
    54
    Sorry, I forgot to say hello, so Hi everybody !! Any help would be greatly appreciated even if i know it may be some classic beginner error

  3. #3
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    You never actually change the line, so it just prints all array members on a long line (seperated by tabs), and i'm guessing that line is too long for your console, so it wraps around a few times. Your inner loop in the print function is correct, but in the outer loop you want to print a newline after printing each column.

    Also your print function returns the same array as it was passed, which seems odd, and you don't actually use this return value either. Why not just make it return void like the other function?
    Your indentation needs work, it's slightly inconsistent.

    Other than that, it looks fine to me.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    54
    Quote Originally Posted by Neo1 View Post
    You never actually change the line, so it just prints all array members on a long line (seperated by tabs)
    Ok i understand, however i'm changing the line when i do this :
    Code:
       for (int x=0; x < 5; x++){
    So could you tell me how to do correctly change the line because i can't see how to do
    Also your print function returns the same array as it was passed, which seems odd, and you don't actually use this return value either. Why not just make it return void like the other function?

    i'm doing this because in the function i will use just next to the function printOutArray i will need the array largeArray

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    54
    Does anyone have a solution to change correctly the line?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I don't understand why my code doesn't display an array of 6 lines and 6 columns as i wish.
    Is it because it's only a 5x5 array?

    You could try putting
    cout << endl;
    somewhere in your print function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    54
    i changed it, i put 6 everywhere i had 5 in order to have 6 lines and 6 columns, but the result is exactly the same.
    i also tried to put an endl by typing this :
    Code:
    cout << largeArray [x] [y]<<'\t'<<endl;
    but it doesn't work anymore because then i get only one column on my console

    Could someone give me the solution to this problem? i just want to have an array of 6 lines and 6 columns as i wish but i can't do it

  8. #8
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    You seem to be misunderstanding me and Salem. Let's look at your print function:

    Code:
    float printOutArray(float largeArray[5][5])
    {
        for (int x = 0; x < 5; x++)
        {
            for (int y = 0; y < 5; y++)
            {
                cout << largeArray [x] [y]<<'\t';
            }
            //HERE
        }
        return largeArray[5][5];
    }
    I added a comment inside your outer loop, at this point you have finished printing a whole column of the array, and you are ready to print the next line. If you want the next line to appear below the one you just printed, you have to print a newline at this point.

    Edit: Also, notice how nice your code looks when the indentation is fixed.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You are also accessing your array out of bounds with the following statement:
    Code:
        return largeArray[5][5];
    Arrays in C/C++ start at zero and stop at size - 1. So in this case the valid index values would be 0,1,2,3,4 but not 5.

    Jim

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why are you returning anything from the function anyway, when you don't use it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Feb 2013
    Posts
    54
    i try to understand what you Neo1 are explaining me, so i do this :
    Code:
    #include <iostream>#include <iomanip>
    #include <cstdlib>
    
    
    using namespace std;
    
    
    void initArray(float largeArray[6][6]){
        for (int x=0; x < 6; x++){
                for (int y=0; y<6; y++){
                      largeArray[x][y] = 0;
            }
        }
    
    
    }
    
    
    float printOutArray(float largeArray[6][6]){
        for (int x = 0; x < 6; x++){
    
    
                for (int y = 0; y < 6; y++){
                        cout << largeArray [x] [y]<<'\t';
                    }
               cout << largeArray [x] [y]<<'\t';
            }
    return largeArray[6][6];
    }
    int main()
    {
        float largeArray [6][6];
        initArray(largeArray);
        printOutArray(largeArray);
    }
    But it doesn't work anymore. What did you mean by "print a new line at this point"? In my code above i added a new print at this point but i must have added the wrong thing.
    Moreover, is my indentation better now?

  12. #12
    Registered User
    Join Date
    Feb 2013
    Posts
    54
    Elysia, to answer your question, my code is not finished, i return it because i will use it later as i said

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Are you trying to return an array? Because you are just returning a single element that is out-of-bounds.
    I also recommend you to use std::array (requires C++11, see signature):

    std::array<std::array<T, N>, M> myarray;
    <==>
    T myarray[N][M];
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Feb 2013
    Posts
    54
    i try to understand what you Neo1 are explaining me, so i do this :
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    
    using namespace std;
    
    
    void initArray(float largeArray[6][6]){
        for (int x=0; x < 6; x++){
                for (int y=0; y<6; y++){
                      largeArray[x][y] = 0;
            }
        }
    
    
    }
    
    
    float printOutArray(float largeArray[6][6]){
        for (int x = 0; x < 6; x++){
    
    
                for (int y = 0; y < 6; y++){
                        cout << largeArray [x] [y]<<'\t';
                    }
               cout << largeArray [x] [y]<<'\t';
            }
    return largeArray[6][6];
    }
    int main()
    {
        float largeArray [6][6];
        initArray(largeArray);
        printOutArray(largeArray);
    }


    But it doesn't work anymore. What did you mean by "print a new line at this point"? In my code above i added a new print at this point but i must have added the wrong thing. Moreover, is my indentation better now?








  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You are still accessing your array out of bounds in that return statement. Plus that is not how you return the entire array, which you actually don't need to do anyway. You are passing a pointer to the array to this function already so when you make changes to that array the changes will be reflected in the calling function.

    You may also want to consider initializing the array when you declare it instead of calling this function.

    Code:
     const int size = 5;
    int myarray[size][size] = {0}; // Declare and initialize all elements to zero.
    You should also stop using all the "Magic Numbers" use a global const int to use for all the array sizes. Then you will only need to change the size in one place.


    Jim
    Last edited by jimblumberg; 03-09-2013 at 10:22 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 02-28-2013, 08:24 AM
  2. User defined number of columns?
    By tvollick in forum C Programming
    Replies: 2
    Last Post: 09-25-2012, 07:35 PM
  3. sum rows & columns in array
    By ronenk in forum C Programming
    Replies: 7
    Last Post: 06-20-2004, 04:16 AM
  4. Number of words multiplied by the number of lines in Linux
    By sjalesho in forum Linux Programming
    Replies: 2
    Last Post: 11-20-2003, 03:25 PM
  5. Number of columns in ListControl
    By MPSoutine in forum Windows Programming
    Replies: 3
    Last Post: 03-28-2003, 02:29 PM