Outputting Points between numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pythonnoob
    New Member
    • Oct 2007
    • 9

    Outputting Points between numbers

    Im new to programming, expecially c++. I started off in python but really just know the basics.

    My question is, how would i go about writing a program that would output all of the numbers (whole numbers) on a plane between X and Y, saw 0,0 and 10,10?

    I can get my code to write the basics from 0-10 with a while loop, but only for 1 axis.

    [code=cpp]
    #include<iostre am>


    using std::cin;
    using std::cout;

    int main()
    {
    int upper_left;
    int upper_right;
    int lower_left;
    int lower_right;

    cout<<"Enter Upper Left ";
    cin >>upper_left;

    cout<<"Enter Upper Right ";
    cin >>upper_right ;

    cout<<"Enter Lower Left ";
    cin>>lower_left ;

    cout<<"Enter Lower Right ";
    cin>>lower_righ t;





    while (upper_left <= lower_left)
    {
    cout << upper_left <<"," << upper_right <<" ";
    upper_left++;
    }
    [/code]
    im not sure where to go from here? How would i move along the other axis.

    Im sorry if this isnt clear, i have been up extremely late the past few nights and am running on little sleep. I would appreciate any help or pointers here. Thanks
    Last edited by sicarie; Feb 27 '08, 03:29 AM. Reason: Code tags
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    If I could just take a minute to rephrase what you are asking, make sure I have the question clear - you want to take a group of coordinates (more than one set of x,y pairs), and print them each out on the console (not drawn using some graphics library)?

    If that is correct, I would recommend using two for loops to iterate through your grid. (The following is in pesudocode) As for loops go, you're going to want:

    Code:
    for_loop_for_horizontal                                 // starting left, going right
       for_loop_for_vertical                                // starting top, going down
           check to see if indicies match any coordinates
           but if not don't forget to print out a " " space char
    You could put in various time-saving checks - like a check in the beginning of each horizontal to see if there is a coordinate, and if not, just jump to the next line, etc... There are probably other ways to do it, but I don't know any off the top of my head (at least none involving other libraries).

    Or did I completely mis-read your question?

    Comment

    • pythonnoob
      New Member
      • Oct 2007
      • 9

      #3
      thanks for your reply. I want to find the numbers between between 2 sets of ordered pairs. So between say -3,5 and 5,-3 would be

      -3, 5 -2, 5 -1, 5 0, 5 1, 5 2, 5 3, 5 4, 5 5, 5
      -3, 4 -2, 4 -1, 4 0, 4 1, 4 2, 4 3, 4 4, 4 5, 4
      -3, 3 -2, 3 -1, 3 0, 3 1, 3 2, 3 3, 3 4, 3 5, 3
      -3, 2 -2, 2 -1, 2 0, 2 1, 2 2, 2 3, 2 4, 2 5, 2
      -3, 1 -2, 1 -1, 1 0, 1 1, 1 2, 1 3, 1 4, 1 5, 1
      -3, 0 -2, 0 -1, 0 0, 0 1, 0 2, 0 3, 0 4, 0 5, 0
      -3,-1 -2,-1 -1,-1 0,-1 1,-1 2,-1 3,-1 4,-1 5,-1
      -3,-2 -2,-2 -1,-2 0,-2 1,-2 2,-2 3,-2 4,-2 5,-2
      -3,-3 -2,-3 -1,-3 0,-3 1,-3 2,-3 3,-3 4,-3 5,-3

      The code i currently have will give me the first line. I dont know how to proceed from there

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by pythonnoob
        any ideas here guys?
        Dude, you posted your question half an hour ago. Patience is a virtue.

        Comment

        • pythonnoob
          New Member
          • Oct 2007
          • 9

          #5
          And sorry for that last post. I meant to edit that out, i realized that was extremely impatient; expecially for how unclear i may have been.

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by pythonnoob
            And sorry for that last post. I meant to edit that out, i realized that was extremely impatient; expecially for how unclear i may have been.
            Ugh, that's a really ugly looking input file. Are you manually inserting those? That will get nasty if you try to read them in from file and parse each one, due to the space after the comma. You will have to set some sort of "b_commaFou nd" boolean flag so that you know when you've hit a pair or not. And you won't be able to cin, you'll have to parse the line out.

            Also, are there clear constraints to how you print these out? As in, do you need them in a normal, |_ - shaped x,y coordinate graph? Or are you just messing around and wondering how you can print them out?

            I told you how you could print them out on the console. If you were looking for the L-shaped graph, you could reverse the horizontal, start it counting from the "top" that way it would logically appear correct, but it would write top to bottom.
            Last edited by sicarie; Feb 27 '08, 03:55 AM. Reason: Back to the file, "that will get nasty if..."

            Comment

            • pythonnoob
              New Member
              • Oct 2007
              • 9

              #7
              Those numbers are meant to be the output, not the input. The program i am trying to generate will take (x1,y1) and (x2.y2) and find every possible point that exists between them.

              For input -3,5 5,-3 I would expect an output of:
              -3, 5 -2, 5 -1, 5 0, 5 1, 5 2, 5 3, 5 4, 5 5, 5
              -3, 4 -2, 4 -1, 4 0, 4 1, 4 2, 4 3, 4 4, 4 5, 4
              -3, 3 -2, 3 -1, 3 0, 3 1, 3 2, 3 3, 3 4, 3 5, 3
              -3, 2 -2, 2 -1, 2 0, 2 1, 2 2, 2 3, 2 4, 2 5, 2
              -3, 1 -2, 1 -1, 1 0, 1 1, 1 2, 1 3, 1 4, 1 5, 1
              -3, 0 -2, 0 -1, 0 0, 0 1, 0 2, 0 3, 0 4, 0 5, 0
              -3,-1 -2,-1 -1,-1 0,-1 1,-1 2,-1 3,-1 4,-1 5,-1
              -3,-2 -2,-2 -1,-2 0,-2 1,-2 2,-2 3,-2 4,-2 5,-2
              -3,-3 -2,-3 -1,-3 0,-3 1,-3 2,-3 3,-3 4,-3 5,-3

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #8
                Originally posted by pythonnoob
                Those numbers are meant to be the output, not the input. The program i am trying to generate will take (x1,y1) and (x2.y2) and find every possible point that exists between them.

                For input -3,5 5,-3 I would expect an output of:
                -3, 5 -2, 5 -1, 5 0, 5 1, 5 2, 5 3, 5 4, 5 5, 5
                -3, 4 -2, 4 -1, 4 0, 4 1, 4 2, 4 3, 4 4, 4 5, 4
                -3, 3 -2, 3 -1, 3 0, 3 1, 3 2, 3 3, 3 4, 3 5, 3
                -3, 2 -2, 2 -1, 2 0, 2 1, 2 2, 2 3, 2 4, 2 5, 2
                -3, 1 -2, 1 -1, 1 0, 1 1, 1 2, 1 3, 1 4, 1 5, 1
                -3, 0 -2, 0 -1, 0 0, 0 1, 0 2, 0 3, 0 4, 0 5, 0
                -3,-1 -2,-1 -1,-1 0,-1 1,-1 2,-1 3,-1 4,-1 5,-1
                -3,-2 -2,-2 -1,-2 0,-2 1,-2 2,-2 3,-2 4,-2 5,-2
                -3,-3 -2,-3 -1,-3 0,-3 1,-3 2,-3 3,-3 4,-3 5,-3
                Okay, I did read your question wrong. Sorry about that. Okay, that looks pretty easy, it's a modification of what I posted. You just start with the array index of the "higher," and iterate down through it, printing out each index, which will be each coordinate.

                Comment

                Working...