1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: C++ question

  1. May 8, 2010 #1
    1. The problem statement, all variables and given/known data
    Hi, I'm writing a c++ code which is about making an xy plane with dots and then using an array an plot a math function with a character. the functions i need to plot are sin x, sqrt x and a polynomial. so far I attempted to make a function for plotting sin x, but I always get it wrong, so I was wondering if anyone here help me out. also if i can have a clue of how i could go about the other function. thanks.


    2. Relevant equations



    3. The attempt at a solution

    #include <iostream>
    #include <cmath>
    using namespace std;

    const int Row = 21;
    const int Col = 76;
    const double PI = 3.14159;

    int main()
    {
    char A[Row][Col];



    for (int x=0 ; x < Row ; x++)
    {
    for (int y=0 ; y < Col ; y++)
    {
    A[x][y] = '.';

    }
    }
    for (int i=0 ; i < Row ; i++)
    {
    for (int j=0 ; j < Col ; j++)
    {
    cout << A[j];
    }
    cout << "\n";
    }

    for(int q = 0; q < Row; q++)
    {
    for(int w = 0; w < Col; w ++)
    {
    w = 3.5 sin((double)PI* q);
    A[q][w] = 'o';


    }
    }

    for( int s = ( Row - 1); s >-1 ; s--)
    {
    for( int t = 1; t<Col; t++)
    {
    cout<< A;
    cout<<endl;
    }
    }



    return 0;
    }

    sin x would be plotted showing 'o' on the dots.
     
  2. jcsd
  3. May 8, 2010 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    First things first, let's clean up that code.
    Code (Text):
    #include <iostream>
    #include <cmath>
    using namespace std;

    const int Row = 21;
    const int Col = 76;
    const double PI = 3.14159;

    int main()
    {
       char A[Row][Col];

       for (int x=0 ; x < Row ; x++) {
          for (int y=0 ; y < Col ; y++) {
             A[x][y] = '.';
          }
       }

       for (int i=0 ; i < Row ; i++) {
          for (int j=0 ; j < Col ; j++) {
             cout << A[i][j];
          }
          cout << "\n";
       }

       for (int q = 0; q < Row; q++) {
          for(int w = 0; w < Col; w ++) {
             w = 3.5 sin((double)PI* q);
             A[q][w] = 'o';
          }
       }

       for (int s = ( Row - 1); s >-1 ; s--) {
          for (int t = 1; t<Col; t++) {
             cout<< A;
             cout<<endl;
          }
       }

       return 0;
    }
    I put your code between a [ code ] and [ /code ] markers (sans the spaces) and I indented it.


    There are several things wrong with your code. Some of the problems are, in order of severity,
    • w = 3.5 sin((double)PI* q);
      This won't even compile, and if it did, it would not do what you wanted. The sine of n*pi is identically equal to zero for all integer n.

    • for(int w = 0; w < Col; w ++) { w = ... }
      You shouldn't be looping here, for one thing. For another, changing a loop index variable in the body of a loop is a very bad idea. If that assignment statement was changed to w = 3.5 * sin((double)PI* q); you would have an infinite loop here.

    • cout<< A;
      You are printing the whole picture here, Col times over. You don't want to print A[t], either. Your rows don't have a null character '\0' to terminate the string.

    • You have rows and columns mixed up. You want the graph to appear across the page. Your x axis should be horizontal, y vertical. You have this backwards in many places.

    • You have a ton of different loop variables. The reason for doing something like for (int loop_index = ...) {loop body} is that the loop_index only has scope in the for and the body of the loop. There is nothing wrong with using the same index variable in multiple loop that are syntactically separate from one another. In fact, it is a good idea to reuse index variables. For example,
      Code (Text):
      for (int irow = 0; irow < Row; ++irow) {
         do_something ();
      }
      do_some_other_stuff ();
      for (int irow = 0; irow < Row; ++irow) {
         do_something_else ();
      }
      Here irow represents a row index in both loops -- so use the same name.

    • x++
      This is a minor nit. In C the idiom is to prefer the post-increment operator. For example, for (i=0; i < limit; i++). In C++ the idiom is to prefer the pre-increment operator, for (i=0; i < limit; ++i). It doesn't make a big difference here. It will when you move on to more advanced topics such as iterators over Standard Template Library Objects. It is a get idea to get in the habit of always using pre-increment unless you truly do mean to use the post-increment operator.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook