Plotting sin x on an XY Plane with C++

In summary: You are making assumptions about the width and height of the character grid. You have 21 rows and 76 columns hard-coded. You don't check to see if you go off the edge in a few places. You don't check to see that your picture is centered.Before attempting to plot a sin(x) function, you should be able to plot a horizontal line. You should be able to plot a vertical line. You should be able to plot a dot. You should be able to plot a point at any (x,y) coordinate. You should be able to plot the character 'o' at any (x,y) coordinate. You should be able to plot more than one 'o' on your graph. You should be
  • #1
cstvlr
12
0

Homework Statement


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.

Homework Equations


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.
 
Physics news on Phys.org
  • #2
First things first, let's clean up that code.
Code:
#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:
    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.
 

Related to Plotting sin x on an XY Plane with C++

What is the purpose of plotting sin x on an XY plane with C++?

The purpose of plotting sin x on an XY plane with C++ is to visually represent the values of the sine function on a graph. This can help in understanding the behavior and patterns of the sine function, as well as its relationship with the x-axis.

What is the mathematical formula for sin x?

The mathematical formula for sin x is sin(x) = opposite/hypotenuse, where the opposite side is the side opposite to the angle x in a right triangle, and the hypotenuse is the longest side of the triangle.

How do I plot sin x on an XY plane with C++?

To plot sin x on an XY plane with C++, you will need to use a plotting library or function, such as the plot() function in the graphics.h library. You will also need to define the range of values for the x-axis and calculate the corresponding values for the y-axis using the sine function. These values can then be plotted on the graph.

What are the key features of a sine wave?

The key features of a sine wave include its amplitude, frequency, and period. The amplitude is the height of the wave, the frequency is the number of complete cycles in a given unit of time, and the period is the amount of time it takes for the wave to complete one full cycle.

Why is plotting sin x useful in real-world applications?

Plotting sin x can be useful in real-world applications as the sine function is a fundamental mathematical function that is used in various fields such as physics, engineering, and mathematics. It can help in modeling and predicting various natural phenomena, such as sound waves and alternating current in electrical circuits.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
998
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
878
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top