Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I wanna graph y-axis increaseing from left to right, x-xis increasing from up to down.

Also try to plot the graph of this equation

a * pow(x, 5) + b * pow(x, 4) + c * pow(x, 3) + d * pow(x, 2) + e * x + f;

given an range from x1 to x2

C++
#include<stdio.h>
#include<stdlib.h>

/*
input:
10 -5 2 8 1 4
1 20
5
*/

int main() {
	int a, b, c, d, e, f;
	int x = 0;
	int y;
	printf("please input six coefficients\n");
	scanf_s("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f);

	int x1, x2;
	printf("please input range of x1-x2\n");
	scanf_s("%d%d", &x1, &x2);

	//xt won't be used for now
	int xt;
	printf("please input x value 0f tangent point\n");
	scanf_s("%d", &xt);

	int arr[1000][8000];
	int i, j;
	for (i = 0; i < x2 - x1 + 1; i++) {
		for (j = 0; j < 80; j++) {
			arr[i][j] = 0;
		}
	}

	for (i = 0; i < x2 - x1 + 1; i++) {
		x = i + x1;
		
		y = a * pow(x, 5) + b * pow(x, 4) + c * pow(x, 3) + d * pow(x, 2) + e * x + f;
		j = y - (-39);
		arr[i][j] = 1;
	}

	for (i = 0; i < x2 - x1 + 1; i++) {
		for (j = 0; j < 80; j++) {
			if (arr[i][j]) {
				printf("*");
			}
			else {
				printf(" ");
			}
		}
		printf("\n");
	}

	return 0;
}


What I have tried:

I wanna graph y-axis increaseing from left to right, x-xis increasing from up to down.
Posted
Updated 15-Nov-20 4:45am
v2
Comments
Rick York 15-Nov-20 12:34pm    
I recommend you adapt your code to accept its input from a text file. It's only a small extension from what you have now and will make it much easier for you to work with your program. Instead of entering nine values every time you can just give it the name of an input file. Once you take this step I am certain you will be very glad that you did.

You can use sscanf to extract the values from a string that you read from the file with fgets.

Quote:
What is going on with this code?

Lets guess that the points are not where you think they should, use the debugger ans whatch your code perform.
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
CPallini 16-Nov-20 2:10am    
My 5.
Patrice T 16-Nov-20 2:27am    
Thank you
It looks you intended to use arr as a screen buffer, but in the end the code handles it incorrectly (it should find the ranges for both x and y and then scale them appropriately to the screen buffer)
Try
C
#include <float.h>
#include <stdio.h>

#define scanf_s scanf

#define SCREEN_BUFFER_ROWS  100
#define SCREEN_BUFFER_COLS   80

void show_screen_buffer(char * screen_buffer, size_t rows, size_t cols );

int main()
{
  int a, b, c, d, e, f;

  printf("please input six coefficients\n");
  scanf_s("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f);
  printf("%d %d %d %d %d %d\n", a,b,c,d,e,f);
  int x1, x2;
  printf("please input range of x1-x2\n");
  scanf_s("%d%d", &x1, &x2);
  printf("%d %d\n", x1, x2);

  int xt;
  printf("please input x value 0f tangent point\n");
  scanf_s("%d\n", &xt);
  printf("%d\n", xt);

  double ymax, ymin;
  double xmax, xmin;
  ymax = DBL_MIN;
  ymin = DBL_MAX;

  xmax = x2;
  xmin = x1;

  int row, col;

  int i;
  for ( i = 0; i < (x2-x1); ++i)
  {
    double x = i + x1;
    double y = f + e * x + d * (x*x) + c * (x*x*x) + b * (x*x*x*x) + a * (x*x*x*x*x);
    if ( ymin > y) ymin = y;
    if ( ymax < y) ymax = y;
  }

  int screen_buffer[SCREEN_BUFFER_ROWS] [SCREEN_BUFFER_COLS] = {0};

  for ( i = 0; i < (x2-x1); ++i)
  {
    double x = i + x1;
    double y = f + e * x + d * (x*x) + c * (x*x*x) + b * (x*x*x*x) + a * (x*x*x*x*x);
    row = (SCREEN_BUFFER_ROWS -1) * (x -xmin) / (xmax-xmin);
    col = (SCREEN_BUFFER_COLS -1) * (y -ymin) / (ymax-ymin);
    screen_buffer[row][col] = 1;
  }

  for (row= 0; row < SCREEN_BUFFER_ROWS; ++row)
  {
    for (col=0; col < SCREEN_BUFFER_COLS; ++col)
      putchar( screen_buffer[row][col] ? '*': ' ');
    putchar('\n');
  }

  return 0;
}


With your suggested inputs, it produces, on my system:
*




*




*




*




*





*




*




 *




 *




   *





     *




       *




           *




                 *




                        *





                                 *




                                             *




                                                            *




                                                                               *
 
Share this answer
 
Comments
Patrice T 15-Nov-20 12:55pm    
+5
JacksonSteel 15-Nov-20 22:19pm    
in the end the code handles it incorrectly (it should find the ranges for both x and y and then scale them appropriately to the screen buffer)

What are you talking about?
and why add those min max?
CPallini 16-Nov-20 2:09am    
If you want to represent a curve on the screen then you must scale it appropriately to fit to the screen dimensions. min and max are used to such a purpose.
You need to use the debugger to see what your results are. My tip this that the result are out of range for your output. Try at first some textual output to see the values.

Your big array is strange and you cant set it zero with
C++
int arr[1000][8000] = {0};
 
Share this answer
 
Comments
CPallini 16-Nov-20 2:10am    
My 5.
If you're going to "print" a chart, the "outer for loop" should be the "y" dimension, not "x"; x is the inner loop (when it comes to printing the array).

Print every column ... one row at a time. Top down.
 
Share this answer
 
v2
Comments
JacksonSteel 15-Nov-20 22:20pm    
hell no,
y axis is horizontal

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900