View Full Version : Graphs
Gibbsy
08-03-2004, 11:29 AM
Hi im trying to make a program to draw a vertical graph for the equation Y=X+10 using 1 dimentional character arrays. The graph should have axis with numbers. The program should ask for:
1. the X axis starting point
2. the number of steps
3. step-size
I have not got a clue where to start. Can anybody help me.
stuka
08-04-2004, 10:12 AM
Can you at least write some code to get started? No one here minds helping, but none of us should (and few will) do your homework for you. As for starting, I would recommend this:
1) Write the interface code - make sure you can get the information from the user (and verify that it's correct).
2) Calculate your points for your graph.
3) Figure out how to print to the screen (hint - a one dimensional char array is a string, or %s in printf terms ;) )
Gibbsy
08-09-2004, 06:26 PM
Still not getting anywhere with this graph. I've written a simple interface but i dont know how im supposed to get this info into a graph. Here's what ive got:
#include <stdio.h>
main()
{
int x, steps, size;
printf("Enter a value for X \n");
scanf("%d", &x);
printf("Enter the number of steps\n");
scanf("%d", &steps);
printf("Enter step-size\n");
scanf("%d", &size);
return 0;
}
Sorry if it sounds like im asking you to do all this for me but im new to all this stuff.
Gibbsy
Whiteknight
08-10-2004, 12:42 AM
are you supposed to do this graphically, or ASCII-graphically? the one is slightly harder then the other.
Gibbsy
08-10-2004, 08:04 AM
Not certain what u mean but i'll give you the specification i got.
Before high-resolution graphics displays became common, computer terminals were often used to display graphs of equations using only text characters. A typical technique was to create a vertical graph by spacing over on the screen, then displaying an '*'. This was done using a 1 dimentional charcter array.
Graphs usually have Y axis vertical but as only a 1 dimentional charcter array can be used this will have to be swapped around.
Write a program to display the equation Y=X+10
The program should ask for:
1. Starting value of X
2. the number of steps
3. step-size.
It should then repeatedly calculate the value for Y using the changing value of X. Normally, X increases by the step size for the number of steps specified. The program should display an '*' in each X column to denote the Y value, eg: if Y=10, then the 10th element of an 81 element character array should be set to '*' and then the string printed to the screen. All other elements should be a space character (" "), except the last, which should be the null character to make it a string. If the value of Y goes over 80 the program should put a '^' hat character in the 79th (second to last) element of the array. If the value goes under 0 (zero), then the program should put a 0 (zero) in the 0 (first) element of the array.
2) Add axis to the graph using
a) '-' (minus) character for the Y axis
b) '¦' (pipe) chracter for the X axis
c) '+' (plus) charcter in the lower left where they intersect at 0,0
3) Add numbers to the X and Y axis. The numbers on the X axis should be dictated by the user input.
Thats all ive got. If you can understand this better than me and could help me out that would be great. As i said im pretty new to all this and need all the help i can get. Cheers
Gibbsy
Whiteknight
08-10-2004, 09:54 AM
i've always heard that act of drawing pictures with text characters called "ASCII-graphics," and that is (i assume) what you are talking about.
I can't think of any way to draw a line in a 2D plane with only a 1D character array, I can--however--think of a way to draw such a line with a 1D integer array. I would set up the array so that the array subscripts would be the vertical X value (assuming we tilt the standard cartesian plane on it's side) and the array value would be the number of spaces between the X axis (left side of the screen) and the asterix (this would be the corresponding Y value).
by this method, X could be incremented graphically simply by writing a newline to the console (if you were hellbent on doing the whole thing with printf() statements) and Y is already in terms of X, so the resulting calculation would be very simple.
for(x=0; x <= SCREEN_MAX; x++)
{
y = x + 10;
ARRAY[x] = y;
}
and then a similar loop would print out a space for every y:
for(x = 0; x <= SCREEN_MAX; x++)
{
for(int i = 0; i <= ARRAY[x]; i++)
{
printf(" ");
}
printf("*");
}
these little snippets of code will print out the line up-sidedown, (and backwards i think) but could easily be altered to do whatever it is that you are looking to do.
Gibbsy
08-10-2004, 06:42 PM
Right tried that cant get it to work. Is this what you meant or is there something else i have to do to it.
#include <stdio.h>
main()
{
int x, y, i, steps, size, SCREEN_MAX;
printf("Enter a value for X \n");
scanf("%d", &x);
printf("Enter the number of steps\n");
scanf("%d", &steps);
printf("Enter step-size\n");
scanf("%d", &size);
for(x=0; x <= SCREEN_MAX; x++)
{
y = x + 10;
array[x] = y;
}
for(x = 0; x <= SCREEN_MAX; x++)
{
for(int i = 0; i <= array[x]; i++)
{
printf(" ");
}
printf("*");
}
return 0;
}
Gibbsy
Whiteknight
08-12-2004, 12:59 AM
in that program you never define the constant SCREEN_MAX, so that variable contains garbage during your FOR loop. consider using a macro to define SCREEN_MAX such as:
#define SCREEN_MAX 20
or you could use a const defintion:
const unsigned short int SCREEN_MAX = 20
SCREEN_MAX should be the number of lines in your console, either you should count them, or you should approximate the number. 20 is usually a good guess, but it all depends.
Gibbsy
08-13-2004, 11:03 AM
This is the code i've got so far:
#include <stdio.h>
#define SCREEN_MAX 20
main()
{
int x, y, i, steps, size;
float array[80];
{
printf("Enter a value for X \n");
scanf("%d", &x);
printf("Enter the number of steps\n");
scanf("%d", &steps);
printf("Enter step-size\n");
scanf("%d", &size);
for(x=0; x <= SCREEN_MAX; x++)
{
y = x + 10;
array[80] = y;
}
for(x = 0; x <= SCREEN_MAX; x++)
{
for(int i = 0; i <= array[80]; i++);
{
printf(" ");
}
printf("*");
}
return 0;
}
But I keep getting this when i compile it
"cwk2.c": E2188 Expression syntax in function main at line 26
"cwk2.c": W8019 Code has no effect in function main at line 26
"cwk2.c": E2379 Statement missing ; in function main at line 26
"cwk2.c": E2134 Compound statement missing } in function main at line 34
Gibbsy
Whiteknight
08-14-2004, 05:37 PM
well, if you count down to line 26 (assuming you maintain correct line numbering here) there is a semicolon (;) at the end of your FOR loop, that causes it to be basically nulified. the printf statement after that would then only execute once.
Gibbsy
08-14-2004, 08:23 PM
Got that part working now it was because there was an int in the for loop in line 26 that shouldnt be there.
Problem is now i can input the info and i just get lines of *'s eg:
* * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * *
What do i do to get single *'s going up in a line for Y=X+10
This is my code:
#include <stdio.h>
#define SCREEN_MAX 20
main()
{
int x, y, i, steps, size;
float chr_str[80];
{
printf("Enter a value for X \n");
scanf("%d", &x);
printf("Enter step-size\n");
scanf("%d", &size);
printf("Enter the number of steps\n");
scanf("%d", &steps);
for(x=0; x <= SCREEN_MAX; x++)
{
y = x + 10;
chr_str[80] = y;
}
for(x = 0; x <= SCREEN_MAX; x++){
for(i = 0; i <= chr_str[80]; i++){
printf(" ");
}
printf("*");
}
}
return 0;
}
Whiteknight
08-15-2004, 02:48 PM
okay, i'm only taking quick looks each time, but i can see what looks like a pretty big problem right off the bat:
the call "chr_str[80]" refers only to the 80th cell in the chr_str array. for instance on line 21 "chr_str[80] = y;" you are simply filling the last cell with each new value for y, and you arent storing any of them. here is the code with these errors fixed:
#include <stdio.h>
#define SCREEN_MAX 20
main()
{
int x, y, i, steps, size;
float chr_str[80];
{
printf("Enter a value for X \n");
scanf("%d", &x);
printf("Enter step-size\n");
scanf("%d", &size);
printf("Enter the number of steps\n");
scanf("%d", &steps);
for(x=0; x <= SCREEN_MAX; x++)
{
y = x + 10;
chr_str[x] = y; //changed this to point the the Xth member of the array
}
for(x = 0; x <= SCREEN_MAX; x++)
{
for(i = 0; i <= chr_str[x]; i++) //similar swap here too
{
printf(" ");
}
printf("*");
}
}
return 0;
}
i dont have a compliler here on this computer, so i cant test it, but i think it is now in better condition. try it out and tell us what kinds of errors you are getting.
Gibbsy
08-15-2004, 03:27 PM
Code was fine but now im just getting random *'s all over the screen. How do I make it all go in a stright line like this
*
*
*
*
*
*
and then draw the axis
Sorry about having you help me so much but as you can tell im pretty much hopeless. I'm getting really stressed now cos this has to be handed in tomorrow.
Gibbsy.
Gibbsy
08-17-2004, 12:56 PM
Right ive had a total rethink about this.
I've now got a set of axis. But i need a calculation loop to calculate Y=X+10. I will then put the results of the calculation into an array and that to make a print loop. This is what ive got now.
#include <stdio.h>
#define SCREEN_MAX 20
#define Y_AXIS 15
#define X_AXIS 17
#define Y_AXIS_VALUE 5
#define X_AXIS_VALUE 5
int i, j, steps, size;
int y_array[80];
main(void)
{
printf("Enter a value for X: ");
scanf("%d", &j);
printf("\n");
printf("Enter step-size: ");
scanf("%d", &size);
printf("\n");
printf("Enter the number of steps: ");
scanf("%d", &steps);
printf("\n");
/* traverse through the y-axis */
for (i = 0; i < Y_AXIS; i ++ ) {
/* traverse through the x-axis */
for (j = 0; j < X_AXIS; j ++ ) {
/* x = 0 */
if ( j == 0 ) {
/* draw the graph y-axis */
/* should also check if there is dot, when x
* is 0.
*/
printf("%7d\t|",((Y_AXIS-(i+1))*Y_AXIS_VALUE));
}
/* x > 0 */
else {
/* y-axis ( at 0 ) */
if ( i == Y_AXIS-1 ) {
/* draw the x-axis */
printf("___");
}
else {
/* this part draw the plotable area.
* therefore, should check if the current
* point, i.e x (represented by j) and
* y (represented by i), need to be plotted.
*/
printf(" ");
}
}
}
printf("\n");
}
/* print the X axis and value */
/* align the axis position to meet with the y-axis */
printf("\t ");
/* draw the marks that represent the x value at this
* position.
*/
for (j = 0; j < X_AXIS; j ++ ) {
printf("| ");
}
/* go to the next line and align */
printf("\n\t");
//print the X axis value;
for (i = 0; i < X_AXIS; i ++ ) {
printf("%2d ",(i * X_AXIS_VALUE));
}
printf("\n");
return getch();
}
Has any body got any idea's of how i can do this.
Gibbsy.
kryptech.net
08-17-2004, 04:33 PM
this is ucky.... you should use curses
vBulletin® v3.7.0, Copyright ©2000-2010, Jelsoft Enterprises Ltd.