Graphing in the console


I don't know what the error in the sine wave and the quadratic are. It keeps returning 0xC0000005... What does this mean?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* sin example */
#include <stdio.h>      /* printf */
#include <math.h>       /* sin */
#include <iostream>
#include <windows.h>

#define R 167
#define C 43
#define PI 3.14159265
#define V (char)0xB3
#define H (char)0xC4
#define P (char)0xC5
#define D (char)250

using namespace std;

char graph[R][C];
int y=0, x=0;
double X=0, Y=0;


int graphe(int, int ,int, int);
void grath();

int main ()
{
    cout<<"This code only works properly in fullscreen mode."<<endl;
  for(int i=0; i<C; i++){
    for(int j=0; j<R; j++){
        graph[j][i]=' ';
        if(j==167){
            graph[j][i]=0;
        }
        if(j==84)
            graph[j][i]=V;
        if(i==21){
            graph[j][i]=H;
        }
        if(i==21&&j==84){
            graph[j][i]=P;
        }

    };
  }
  cout<<"1. Y=Ax+B\n2. Y=A(Sin(x))\n3. Y=Ax²+Bx+C"<<endl;
  int input1, a=0, b=0, c=0;
  cin>>input1;
  system("Cls");
  switch(input1){
  case 1:
    cout<<" separated by a space, enter in the values for A and B.\t EXAMPLE: 2 6 (enter)"<<endl;
    cin>>a;
    cin>>b;
    break;
  case 2:
    cout<<"please give the value for A"<<endl;
    cin>>a;
    break;
  case 3:
    cout<<"Wow. A quadratic. How original. I'm gonna need A, B, and C separated by spaces.\n EXAMPLE: 2 6 9"<<endl;
    cin>>a;
    cin>>b;
    cin>>c;
    break;
  default:
    cout<<"you suck. I hate you. Now you do not get anything.  lol XD"<<endl;
    return 0XD;
  }
  graphe(input1, a, b, c);
  return 0;
}
int graphe(int type, int a, int b, int c){
    if(type==1){
     for(int i=0; i<C; i++, X++){
        Y=a * X + b;
        y=Y; x=X;
        graph[y][x]=D;
     }
    }
    else if(type==2){
        for(int i=0, X=0; i<C; i++, X++){
            Y=a*(sin(X));
            x=X; y=Y;
            graph[y][x]=D;
        }
    }
    else if(type==3){
        for(int i=0; i<C; i++, X++){
        Y=a*(pow(X, 2))+b*X+c;
        y=Y; x=X;
        graph[y][x]=D;
        }
    }
    grath();
    return 1;
}
void grath(){
    for(int i=0; i<=C; i++, x++){
        for(int j=0; j<=R; j++){
            cout<<graph[j][i];
        }
        cout<<endl;
    }
}
0xC0000005 is an illegal address reference.

Line 98: You're indexing out of bounds. Line 98 should read:
for(int i=0; i<C; i++, x++)
Still does the same thing, same error, same place...
Wow. A quadratic. How original. I'm gonna need A, B, and C separated by spaces.
 EXAMPLE: 2 6 9

Process returned -1073741819 (0xC0000005)   execution time : 5.132 s
Press any key to continue.
Y=a*(pow(X, 2))+b*X+c;
y=Y; x=X;
graph[y][x]=D;

This is no good.
this is the same as saying
graph[a*x*x+b*x+c][x] = D.

first, D is not a sensible value in this loop. that should be Y
and second, the index into graph needs to be a positive integer in range,
which your equation is unlikely to provide.

its possible you want to fill a graph 2-d image, so it would be graph[y][x] but in this case, you need to ensure that y is in range, and you never do that. can you even support graphing where f(x) is negative answer?
Last edited on
D is the character for the dot... I need that for the points. I don't know how to limit Y and X, as I am still not very advanced.
if( f(x) > max_allowed) Y = (max_allowed) else Y = f(x);
if (f(x) < 0) Y = 0;
plot(Y) and it is safe now.


you can do the above a couple of ways. eg Y = std::min(Y, max_allowed)
which says y can be no larger than max_allowed. This basically hides the if statement, its doing the same thing.
Last edited on
Topic archived. No new replies allowed.