co-ordinates

This is a discussion on co-ordinates within the C++ Programming forums, part of the General Programming Boards category; ok i need to write a program to generate the co-ordinates of twenty five elements for a square shape. ( ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    21

    co-ordinates

    ok i need to write a program to generate the co-ordinates of twenty five elements for a square shape. ( 5 by 5 )

    i am also given the square as 1m by 1m.


    this is what I (indeed a noob) has so far. The second for won't work, im assuming because it reads the same variable, but im a beginner and not sure where to go from here! I welcome any help!!


    Code:
    #include <iostream.h>
    #include <conio.h>
    
    
    int main()
    
    {
    
    
       cout << endl << "Welcome" << endl << endl;
    
       int x;
       int y=1;
    
    
       for (x=1; y<6; y++ )
    
        {
        cout << "co-ordinates= " << x << y << endl;
             }
    
    
    
        for (x=2; y<6; x++ )
    
        {
        cout << "co-ordinates= " << x << y << endl;
             }
      
    
         getch();
         return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Because y is no longer lesser than 6. You need to set it back to 1. By the way, you can write the same code using only 2 for loops.

    Code:
    for(int x = 1; x < 6; x++)
        for(int y = 1; y < 6; y++)
            std::cout << "Co-ordinates = (" << x << ", " << y << ')';

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    21
    ty.

    now i need to modify it to read the program input. and to include

    no. of element
    length of shape
    width
    store output in a file called square - is this called dynamic allocation?


    i started writing...

    printf ("no of element = ")
    scanf("%f, & element")
    printf("length.. etc

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    now i need to modify it to read the program input. and to include

    no. of element
    length of shape
    width
    store output in a file called square - is this called dynamic allocation?
    Have you completed the original assignment before you start modifying it? What Desolation has shown is the heart of the program but not quite all of it: you'll need to mulitply/divide x and y with something to get the coordinates in centimetres or metres.

    In original program all values are literals: how many times the loops run and the factor by which you would multiply x and y. In the modified program, after you get the input, you'll need to use variables in these places.

    Writing to a file is not dynamic allocation. Just open a file before the loops and each iteration print the result to this file pretty much the same way it goes to the screen.

    i started writing...

    printf ("no of element = ")
    scanf("%f, & element")
    printf("length.. etc
    In C++ I'd rather use cout and cin, as you did earlier.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    21
    not sure if i actualy have to replace variables... what would you do now?

    PS. jus gotta get this done tonite i can learn more later, if anyone wouldn mind completing for me



    Code:
    #include <iostream.h>
    #include <conio.h>
    
    int main()
    
    
    {
    cout << endl << "Welcome" << endl << endl;
    
    
        float element, length, width;
    
    
    
       cout <<"No. of element = " ;
       cin >> "%f", & element  ;
       cout <<"Input Length =  ";
       cin>> "%f", & length;
       cout << "Input Width =      ";
       cin>> "%f", & width;
    
    
       
    
       for(int x = 1; x < 6; x++)
        for(int y = 1; y < 6; y++)
    
           cout << " = (" << x << ", " << y << ')';
    
    
    
           
    
    
          getch();
          return 0;
    
    
    
    
    }

    many thanks!!

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Despite taking input, you don't use it anywhere. Use them to determine how many times each of the loops should run.

    Also, I don't think displaying (1, 1)(1, 2) etc is what is expected of you. Otherwise the length of the sides (1 m) is absolutely irrelevant. The ouput should probably be more like: (0.0, 0.0)(0.0, 0.2)(0.0, 0.4) etc.

    And if the user wants the side of the square to be 3 m, that would be (0.0, 0.0)(0.0, 0.6)(0.0, 1.2) etc.

    By the way, this
    Code:
    for(int x = 1; x < 6; x++)
    is an awful way to say "loop five times". Do you see number 5 anywhere? Correct way would be
    Code:
    for(int x = 0; x < 5; x++)
    This would also take care of the fact that the upper left coordinates are 0-s.

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    21

    Smile

    hmm...

    Code:
    float element, length, width;
    
    
    
       cout <<"No. of element(1-25) = " ;
       cin >> "%f", & element  ;
       cout <<"Input Length =  ";
       cin>> "%f", & length;
       cout << "Input Width =      ";
       cin>> "%f", & width;
    
    
    
    
       cout << endl << "Welcome" << endl << endl;
    
    
       float ewidth,elength;
    
        ewidth=width*length/25  ;
        elength=length*width/25 ;
    
         for(ewidth; ewidth < 5; ewidth++)
          for (elength; elength < 5; elength++)
    
    
           cout << " = (" << ewidth << ", " << elength << ')';
    help

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    21
    Code:
    float ewidth,elength;
    
        ewidth=width/5;
        elength=length/5;
    
    
    
         for(ewidth; ewidth < 25; ewidth++)
          for (elength; elength < 25; elength++)
    
    
           cout << " = (" << ewidth << ", " << elength << ')';

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by soundwave
    hmm...

    Code:
     //Snip
       cout <<"No. of element(1-25) = " ;
       cin >> "%f", & element  ;
       cout <<"Input Length =  ";
       cin>> "%f", & length;
       cout << "Input Width =      ";
       cin>> "%f", & width;
    help
    cin is not scanf!

    try this instead -
    Code:
       cout <<"No. of element(1-25) = ";
       cin >> element;
       cout <<"Input Length =  ";
       cin >> length;
       cout << "Input Width =      ";
       cin >> width;

  10. #10
    Registered User
    Join Date
    Jan 2007
    Posts
    21
    k, this programs gettin there...

    how do i store output in a file?


    cheers

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by soundwave
    Code:
    float ewidth,elength;
    
        ewidth=width/5;
        elength=length/5;
    
    
    
         for(ewidth; ewidth < 25; ewidth++)
          for (elength; elength < 25; elength++)
    
    
           cout << " = (" << ewidth << ", " << elength << ')';
    This doesn't seem to be doing what is desired.

    It's just my guess that they want coordinates in meters from the upper left corner. Your description of the task is very vague.

    But if you do, how would you find how long is the edge of a small square in the large square? width_of_small_square = width_of_large_square / no_of_small_squares_in_a_row

    Then, how would you find the x coordinate of the n-th small square in a row?
    x = (n-1) * width_of_small_square

    (Using floats to control loops looks fishy, because they are imprecise. You could easily end up with the loop running one time more or less than wanted, because the sum that with normal mathematics would equal 25 might turn out 24.99999 in float calculations.)
    Last edited by anon; 01-07-2007 at 04:35 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. display a char at a particular co ordinates on screen
    By jas_atwal in forum C++ Programming
    Replies: 12
    Last Post: 02-03-2008, 01:21 PM
  2. Help With Creating Co - ordinates Program
    By stuckonc in forum C Programming
    Replies: 2
    Last Post: 06-07-2006, 09:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21