Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,156 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,117 people online right now. Registration is fast and FREE... Join Now!




Square grid

 
Reply to this topicStart new topic

Square grid, looping problems

burfy1982
17 Dec, 2007 - 05:56 AM
Post #1

New D.I.C Head
*

Joined: 17 Dec, 2007
Posts: 3


My Contributions
I am trying to write a program with a loop in a loop, to plot X/Y coordinates for a 5 by 5 grid but i can't get the looping correct, can anyone help me?
#include <stdio.h>
#include <conio.h>
void main ()
{
int x, y;
clrscr ();

for (x=0; x<=5; x++)
{
for (y=0; y<=5; y++)
{
printf ("\t%d",x);
printf ("\t%d",y);
}
printf (" \n");

}
getch ();
}
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Square Grid
17 Dec, 2007 - 06:24 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Sort of close. Think about what you're doing. You want to print out y lines, x length. Your inner loop does the x length part.

CODE

#include <stdio.h>
int main () {
    int x, y;
    
    for (y=0; y<5; y++) {
        // you're at the start of your line
        // put the tab here
        printf ("\t");
        // if you like, make a note that this is y line
        printf ("%d: ",y);
        for (x=0; x<5; x++) {
            printf ("%d",x);
        }
        // we're drawn our line, end it
        printf("\n");
    }
}


Hope this helps.

User is offlineProfile CardPM
+Quote Post

burfy1982
RE: Square Grid
17 Dec, 2007 - 06:46 AM
Post #3

New D.I.C Head
*

Joined: 17 Dec, 2007
Posts: 3


My Contributions
Thanks for your reply,but i am trying to write this program so that i am able to print on the screen like this: 0,0 0,1 0,2 etc
1,0 1,1 1,2 etc
2,0 2,1 2,2 etc

So its a 5 by 5 grid with 25 elements.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Square Grid
17 Dec, 2007 - 07:17 AM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Still not following. This
CODE

#include <stdio.h>
int main () {
    int x, y;
    for (y=0; y<5; y++) {
        for (x=0; x<5; x++) {
            printf ("(%d,%d),",x,y);
        }
        printf("\n");
    }
}


Produces this:
CODE

(0,0),(1,0),(2,0),(3,0),(4,0),
(0,1),(1,1),(2,1),(3,1),(4,1),
(0,2),(1,2),(2,2),(3,2),(4,2),
(0,3),(1,3),(2,3),(3,3),(4,3),
(0,4),(1,4),(2,4),(3,4),(4,4),


Which is, um, 25 elements?

If you want to replace printf ("(%d,%d),",x,y); with printf ("X"); you get this:
CODE

XXXXX
XXXXX
XXXXX
XXXXX
XXXXX


Which is a happy little square. I guess I don't understand your expectations of square, sorry.

User is offlineProfile CardPM
+Quote Post

burfy1982
RE: Square Grid
17 Dec, 2007 - 07:33 AM
Post #5

New D.I.C Head
*

Joined: 17 Dec, 2007
Posts: 3


My Contributions
No thanks for your help i am trying to learn C on my own, so every little bit of help is greatful.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 11:20PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month