3 Replies - 694 Views - Last Post: 07 September 2018 - 12:50 PM Rate Topic: -----

#1 derodz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-September 18

how to print out 2d array of asteriks

Posted 06 September 2018 - 04:04 PM

my program only doesnt output a matrix:

#include <iostream>
#include <string>
#include<array>

using namespace std;

int main()
{
 
  
  int row,column;
  
  for(row=1;row<=8;row++){
  
  for(column=1;column<=8;column++)
{
    
    cout<<"*"<<endl;}
    
    cout<<'\n'<<endl;
}
return 0;}

This post has been edited by Martyr2: 06 September 2018 - 04:24 PM
Reason for edit:: Please use code tags in the future, thanks! [code]your code between[/code]

Is This A Good Question/Topic? 0
  • +

Replies To: how to print out 2d array of asteriks

#2 Martyr2   User is online

  • Programming Theoretician
  • member icon

Reputation: 5315
  • View blog
  • Posts: 14,185
  • Joined: 18-April 07

Re: how to print out 2d array of asteriks

Posted 06 September 2018 - 04:23 PM

You don't want to be printing an "endl" every time you print an asterisk. So leave it off. cout <<"*";

Then you probably don't want to print both an '\n' and endl. So just cout << endl;

That way each time you print an asterisk, it will print the next on the same line until it reaches the total number of columns. At that point it drops out of the inner for loop, prints the endl and then goes back to the top of the outer for loop to start the next line.

Let me know if that works out for you. :)
Was This Post Helpful? 2
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7328
  • View blog
  • Posts: 15,239
  • Joined: 16-October 07

Re: how to print out 2d array of asteriks

Posted 07 September 2018 - 04:50 AM

For the sake of clarity and sanity, let's format your code a little:
#include <iostream>

using namespace std;

int main(){
    // not here int row,column;
  
    // not 1 for(row=1;row<=8;row++){
    for(int row=0;row<8;row++) {
        for(int column=0;column<8;column++) {
            cout<<"*"<<endl;
        }
        cout<<'\n'<<endl;
    }
    return 0;
}



So, the endl inserts a newline. Indeed, cout<<'\n'<<endl is basically redundant, inserting two newlines.

Without the loops, just try this:
cout<<"1"<<endl;
cout<<"2"<<endl;
cout<<"3";
cout<<"4";
cout<<"5"<<endl;


See if that makes the issue a little clearer.
Was This Post Helpful? 2
  • +
  • -

#4 Programmer2004   User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 96
  • Joined: 25-October 17

Re: how to print out 2d array of asteriks

Posted 07 September 2018 - 12:50 PM

You did some mistakes in your code.

First, you print a new line after every '*' character, you have to remove endl.

Second thing is that you print double new line, because \n and endl are the same thing, you have to remove one of them.

Third thing is that on mistake you inserted "return 0" at the end of row loop, so the program will exit immediately after the first row is finished.

The last thing is that you forgot to close "main" function with } character.

This post has been edited by Programmer2004: 07 September 2018 - 12:51 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1