Hi, I've only learned c++ for about 2months, I've basically learned up to arrays, two and multiple dimensions but not in-depth. Currently reading through pointers.
I've managed to achieve a similar graph to what i am supposed to have.
What I'm getting.
[IMG]http://i109.photobucket.com/albums/n79/johnz13/graph1.jpg[/IMG]
Should look like.
[IMG]http://i109.photobucket.com/albums/n79/johnz13/graph.jpg[/IMG]
Problem I am having is trying to make it smaller/similar to what its supposed to be.
I was thinking of changing the variable of x in the formula y = 3*sin(2*pi*x/8) and then using a separate variable, in this case t, as a double.

const double pi = 3.141592654;
	int i, j, x, y;

	//dimensions
	const int row = 21;
	const int col = 76;

	char plot[row][col];

	for (j = 20; j > -1; j--){
		for (i = 1; i < col; i++){
			plot[j][i] = canvasBG;
			for (x = 1; x < 76; x++){
				y = 3.5*sin(2*pi*x*t/8)*4;
				plot[y][x] = '*';	
			}
			cout << plot[j][i];
		}
		cout << endl;
	}

I've also had difficulty trying to put the scale on the left and bottom side. My previous attempt only resulted in the screen scrolling down continuously.
My understanding is that row = 0 to 20 (21 is where the x-axis must be), column = 0. Then only numbers divided by 4 (allow 5 numbers to be displayed evenly) have the scale values displayed, and everything else just displays a ' '(space)/32 (ascii value).
I've been thinking of whether to use a pointer to manually input and display the scales.

I've been stuck on these 2 problems for a about 3 days now. Any suggestions?

Recommended Answers

All 13 Replies

You're so close! The problem is that the function is in the middle of two loops, and is being executed too many times. Watch what happens when you first fill the array with canvas, then run the function only 76 times, then plot the function:

const double pi = 3.141592654;
	int i, j, x, y;

	//dimensions
	const int row = 21;
	const int col = 76;

	char plot[row][col];

	for (j = 20; j > -1; j--){
		for (i = 1; i < col; i++){
			plot[j][i] = '.';
		}
		cout << endl;
	}

    for (x = 1; x < 76; x++){
    	y = 15*sin(2*pi*x/24);
    	plot[y][x] = '*';
        	
    }


    for (j = 20; j > -1; j--){
		for (i = 1; i < col; i++){			
			cout << plot[j][i];
		}
		cout << endl;
	}

I'll leave it to you to make the code elegant :)

I mean plot everything, not just the function.

Edit:
What I'm getting.
http://i109.photobucket.com/albums/n79/johnz13/graph1.jpg
Should look like:
http://i109.photobucket.com/albums/n79/johnz13/graph.jpg
-------------------------------------------------------------------------------------------
I've managed to fix it, and now i get similar results.
http://i109.photobucket.com/albums/n79/johnz13/result_graph.jpg

Problem now is that I have no clue on how to add the scales. I know that:
-for every 4 increments (initially y=0)(x always = 0)
-scale+1 (initially scale=0)
-everything else = ' ' (space) or ascii value 32.

You're so close! The problem is that the function is in the middle of two loops, and is being executed too many times. Watch what happens when you first fill the array with canvas, then run the function only 76 times, then plot the function:

I'll leave it to you to make the code elegant :)

Thanks for this, didn't notice the post till after I completed my result. I'll try it out thanks.

Edit: I tried it, problem with it is that it displays the canvas twice, i think it's preferred to be just displayed once.

Thanks for this, didn't notice the post till after I completed my result. I'll try it out thanks.

Edit: I tried it, problem with it is that it displays the canvas twice, i think it's preferred to be just displayed once.

So this is a sine-wave plotted against time?

Since waves will have an amplitude (maximum height) and a frequency (number of cycles per second). amplitude and frequency will be constants. y will be a function of time, amplitude, and frequency, using time as the variable. Or you can create a variable theta based on time and frequency and derive y based on theta and amplitude. Or you can calcuate y using theta and the fact that cosine squared plus sine squared have to equal 1, then multiply by amplitude. There are a variety of ways to do the math. Figure out the values of y based on t and stick them in an array.

const int AMPLITUDE = 4;
const double FREQUENCY = 1.0 / 12.0; // 1 cycle every twelve seconds
const int TOTAL_TIME = 60; // seconds

int y[TOTAL_TIME+1]; // rounded y values
for (int t = 0; t <= TOTAL_TIME; t++)
{
    // calculate theta based on t and FREQUENCY
    // calculate/round off y[t] using trigonometry based on AMPLITUDE and theta.
}

// now do the plotting
for (int t = 0; t <= TOTAL_TIME; t++)
{
    for (int i = AMPLITUDE; i >= -AMPLITUDE; i--)
    {
        // compare y[t] to i.  If equal, display '*'.  If not, display '.'
    } 

    // display newline
}

This is one way to make a fairly generic graph. Simply change the constants and only the constants when you want a different graph. or if you have a different equation, change the y[] calculation part.

The display part is pretty much always the same.

[EDIT]
My display is actually backwards. The outer loop should range from AMPLITUDE to -AMPLITUDE and the inner loop should span across time since time is horizontal and the inner loop is always the horizontal element. My bad.

So this is a sine-wave plotted against time?

Since waves will have an amplitude (maximum height) and a frequency (number of cycles per second). amplitude and frequency will be constants. y will be a function of time, amplitude, and frequency, using time as the variable. Or you can create a variable theta based on time and frequency and derive y based on theta and amplitude. Or you can calcuate y using theta and the fact that cosine squared plus sine squared have to equal 1, then multiply by amplitude. There are a variety of ways to do the math. Figure out the values of y based on t and stick them in an array.

This is one way to make a fairly generic graph. Simply change the constants and only the constants when you want a different graph. or if you have a different equation, change the y[] calculation part.

The display part is pretty much always the same.

[EDIT]
My display is actually backwards. The outer loop should range from AMPLITUDE to -AMPLITUDE and the inner loop should span across time since time is horizontal and the inner loop is always the horizontal element. My bad.

Thanks, although its not against time, just merely plotting a math function.
Sorry, forgot to mention that it was a math function. The double t is actually another variable to match the ratio of the scale to the array column. That is the max horizontal axis value/max scale value -> 75/15 = 5, so 1 value on the scale is equivalent to about 5 columns in the two dimensional array. therefore, 1 column = 0.2 = t.

The horizontal axis should be pretty easy. Just make your 2D array a little bigger (one more row), then loop through and fill the last row array with incrementing numbers and spaces. I can help you tweak it once you have given it a shot. For the vertical axis, you can do the same thing, but loop through the other dimension.

Thanks, although its not against time, just merely plotting a math function.
Sorry, forgot to mention that it was a math function. The double t is actually another variable to match the ratio of the scale to the array column. That is the max horizontal axis value/max scale value -> 75/15 = 5, so 1 value on the scale is equivalent to about 5 columns in the two dimensional array. therefore, 1 column = 0.2 = t.

OK. Same concept though. One value of y for every x. x is calculated on a range. Set up an array of values the size of your x range. Calculate y for each x and store it in that array. Again, my plot code was backwards regarding the inner/outer loop. You'll have some maximum and minimum x and maximum and minimum y and you'll have a nested loop to display, using those minimums and maximums as the loop ranges. You can either use a 2-dimensional array as you did originally or you can use a 1-dimensional as I did and do the comparisons to decide which character to print.

As far as the scaling goes, that's when your maximum and minimum values come in:

for (int y = Y_MAX; y >= Y_MIN; y--)
{
    cout << y << " "; // y scale
    for (int x = X_MIN; x <= X_MAX; x++)
    {
        // display a character
    }
}

// display  x scale here using loop.  Parameters are X_MIN to X_MAX.

hello mate, in order for me to help you i need to see the whole c++ code.

Just make your 2D array a little bigger (one more row), then loop through and fill the last row array with incrementing numbers and spaces. I can help you tweak it once you have given it a shot. For the vertical axis, you can do the same thing, but loop through the other dimension.

The array must be 21, 76. the extra 1 in the row and column are for the scales.
Here's my attempt:

for (j = 21; j > -1; j--){
		if (j == 20){
			i = 0;
			plot[j][0] = '4';
			cout << plot[j][i];
		}
		else if (j == 15){
			i = 0;
			plot[15][0] = '3';
			cout << plot[j][i];
		}
		else if (j == 10){
			i = 0;
			plot[j][i] = '2';
			cout << plot[j][i];
		}
		else if (j == 5){
			i = 0;
			plot[j][i] = '1';
			cout << plot[j][i];
		}
		else if (j == 0){
			i = 0;
			plot[j][i] = '0';
			cout << plot[j][i];
		}
		else
		{
			i = 0;
			plot[j][i] = ' ';
		}
	}

Problem with it is that I get unwanted symbols at the top.
http://i109.photobucket.com/albums/n79/johnz13/scalewithgraph.jpg
In the picture I placed the loop after it assigns the '.' and before it calculates the function.
Also lost on whereabouts to put the loop i.e before the plotting the function, or after, or anywhere else?
Note: I am using the layout suggested by tony.
I edited it so that it displays once, wasn't thinking last night as i was tired.

As far as the scaling goes, that's when your maximum and minimum values come in:

for (int y = Y_MAX; y >= Y_MIN; y--)
{
    cout << y << " "; // y scale
    for (int x = X_MIN; x <= X_MAX; x++)
    {
        // display a character
    }
}
// display  x scale here using loop.  Parameters are X_MIN to X_MAX.

Currently, I'm attempting to change my scale attempt into the for loop you suggested, as writing the code for the horizontal axis will be lengthy.

Here's my latest attempt.
http://i109.photobucket.com/albums/n79/johnz13/scalewithgraph2.jpg
Still have the problem with the top part of the graph.
Now have problems with the horizontal axis scale display past the values 9.

Note: If it's no problem, could the codes be sent via PM.
PM me and I will send the recent code via PM for those trying to help, thanks.

The array must be 21, 76. the extra 1 in the row and column are for the scales.
Here's my attempt:

for (j = 21; j > -1; j--){
		if (j == 20){
			i = 0;
			plot[j][0] = '4';
			cout << plot[j][i];
		}
		else if (j == 15){
			i = 0;
			plot[15][0] = '3';
			cout << plot[j][i];
		}
		else if (j == 10){
			i = 0;
			plot[j][i] = '2';
			cout << plot[j][i];
		}
		else if (j == 5){
			i = 0;
			plot[j][i] = '1';
			cout << plot[j][i];
		}
		else if (j == 0){
			i = 0;
			plot[j][i] = '0';
			cout << plot[j][i];
		}
		else
		{
			i = 0;
			plot[j][i] = ' ';
		}
	}

Problem with it is that I get unwanted symbols at the top.
http://i109.photobucket.com/albums/n79/johnz13/scalewithgraph.jpg
In the picture I placed the loop after it assigns the '.' and before it calculates the function.
Also lost on whereabouts to put the loop i.e before the plotting the function, or after, or anywhere else?
Note: I am using the layout suggested by tony.
I edited it so that it displays once, wasn't thinking last night as i was tired.

This code looks like it has nothing to do with the top. The top scaling all has to happen before you get here, so this gives no clue to the problem. According to the picture, this part works fine, so it looks like you are posting the part that works, not the part that doesn't work. Could be wrong, but post the whole program each time. It's short enough. That way we don't have to go back to the earlier posts to see the missing code.

[EDIT]
Are you trying to put the horizontal scale at the bottom or the top?
[/EDIT]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.