Thread: number of columns and lines in my array

  1. #16
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    I didn't mean a new line, i meant a newline, as in '\n' or std::endl, just like Salem also suggested.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  2. #17
    Registered User
    Join Date
    Feb 2013
    Posts
    54
    I just tried what jimblumberg told me to do but it doesn't work any more. Please could someone give me a piece of code that would help me understand because honestly, i still can't see how to make an array of 6 lines and 6 columns displayed filled with zero on the console, even if i try everytime applying the advices you give me

  3. #18
    Registered User
    Join Date
    Feb 2013
    Posts
    54
    Quote Originally Posted by Neo1 View Post
    I didn't mean a new line, i meant a newline, as in '\n' or std::endl, just like Salem also suggested.
    What is '\n' , is this what you call a newline (sorry english is not my native language)? And where do i have to put it in my code?

  4. #19
    Registered User
    Join Date
    Feb 2013
    Posts
    54
    If someone would give me the simple code that displays an array of 6 lines and 6 columns filled with zero i would greatly appreciate it

  5. #20
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    This seems to work for me:

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
       const int size = 26;
       int marray[size][size] = {{0}};
       for(auto i : marray)
       {
          for(auto j = 0; j < size; ++j)
             cout << i[j] << " ";
          cout << endl;
       }
       return 0;
    }

    Jim
    Last edited by jimblumberg; 03-09-2013 at 03:34 PM.

  6. #21
    Registered User
    Join Date
    Feb 2013
    Posts
    54
    The problem has now been resolved !! Thank you everybody for your help !
    I just had to add a
    cout << ENDL.
    in my outter loop of my function printOutArray as Neo1 told me, but i have to tell my error to all the readers who are also beginning in programming (in C++) like me : i didn't want to put a
    cout << ENDL.
    in my outter loop of my function printOutArray because in my opinion cout<< endl equals "go to the next line", and yet putting an endl is just like sending a character which indicates the CONSOLE to go to the next line (on the console), HOWEVER it DOES NOT mean that you increment the line, and i wrongly thought that it was like incrementing the line (so why increment the line twice at each way in the loop?). This was a wrong way of thinking.

    Here is my code that now works, and makes an array of 6 lines and 6 columns :
    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 << endl;
            }
    return largeArray[6][6];
    }
    int main()
    {
        float largeArray [6][6];
        initArray(largeArray);
        printOutArray(largeArray);
    }

  7. #22
    Registered User
    Join Date
    Feb 2013
    Posts
    54
    Sorry i meant a
    cout << endl ;

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The problems have not been solved. See post #13.
    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.

  9. #24
    Registered User
    Join Date
    Feb 2013
    Posts
    54
    OK i don't have to return my array in this exemple, so is this code right ? (i deleted
    return largeArray[6][6];
    :
    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 << endl;
            }
    }
    int main()
    {
        float largeArray [6][6];
        initArray(largeArray);
        printOutArray(largeArray);
    }
    Moreover, can someone tell me if my indentation is good (or not)?
    Thank you Elysia

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you are not going to return an array, this is fine.
    Indentation is acceptable. Rule is that for every opening {, you use another indentation level (+ n spaces or a tab, depending of your preferences). Keep it consistent.

    EDIT: Missed the float return type in printOutArray...
    Last edited by Elysia; 03-11-2013 at 01:21 PM.
    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. #26
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Moreover, can someone tell me if my indentation is good (or not)?
    In my opinion it could use some improvement. One of the biggest things you need to work on is being consistent. Look at the following snippet, and notice how in the first function you have your brace on the same line as the definition, yet main() has the opening brace on a separate line. Either is acceptable, but be consistent. Also consistent placement of where you place the ending brace in your control structures. I normally place the ending brace at the same indent level as control statement.

    Code:
    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 << endl;
       }
    }
    int main()
    {
       float largeArray [6][6];
       initArray(largeArray);
       printOutArray(largeArray);
    }
    The only other issue is that your function is still wrong. You told the compiler you would be returning a float, but you aren't returning anything. If you don't want to return anything then the function should be defined as a "void", otherwise return the proper type of variable, never lie to the compiler.

    Jim

  12. #27
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by jimblumberg View Post
    If you don't want to return anything then the function should be defined as a "void", otherwise return the proper type of variable, never lie to the compiler.
    It WILL find out and it WILL get back on you with incomprehensible template errors later on!
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  13. #28
    Registered User
    Join Date
    Feb 2013
    Posts
    54
    OK. Finally, the code with 0 errors is :
    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;
            }
        }
    
    
    
    
    }
    
    
    
    
    void 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 << endl;
            }
    }
    int main()
    {
        float largeArray [6][6];
        initArray(largeArray);
        printOutArray(largeArray);
    }

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