Solved

Scatter Graph

Posted on 2004-08-15
43
302 Views
Last Modified: 2010-04-15
Hi Im trying to make a graph in the C programming language for Uni Coursework.

The graph should plot the equation Y=X+10 using these inputs
1. original value for X
2.step size
3. Number of steps

Using *'s for the Y value all other elements should be a " "
the axis should use ¦ (vertical) - (horizontal) + (origin)

I am only allowed to use 1D Arrays and strings.
This means that the X and Y axis will have to be swapped around
If Y goes over 80 then the program should put a ^ character in the 79th element of the array
If Y goes under 0 then the prgram should put a 0 charcter in the 0 element of the array
0
Comment
Question by:Magibwan
  • 21
  • 19
43 Comments
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
Maqibwan,

Obviously, we cannot do your coursework for you. You will get more response from us if you give us some code you have written. We can then suggest changes you could make to get it to work or improve it.

I would suggest your first challenge to be to key in an example output. Would something like:

+-------------------------------------------
|           *
|            *
|             *
...

be about what you are looking for?

If so you can clearly see that you must first print the Y axis, then, for as long as you want, print each X axis line as a '|' followed by the right number of spaces and a '*'.

Good luck.

Paul
0
 

Author Comment

by:Magibwan
Comment Utility
Here is the code ive tried to get to work so far:

#include <stdio.h>
#include <conio.h>

#define SCREEN_MAX 20
#define Y_AXIS 10
#define X_AXIS 10
#define AXIS_VALUE 5

void main()
{
  int x, y, i, j, k, 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;
    }

    for(x = 0; x <= SCREEN_MAX; x++)
    {

      for(i = 0; i <= chr_str[x]; i++)
      {
        printf(" ");
      }
      printf("*");
    }
   /* traverse through the y-axis */
   for (j = 0; j < Y_AXIS; j ++ ) {
      /* traverse through the x-axis */
      for (k = 0; k < X_AXIS; k ++ ) {
         /* x = 0 */
         if ( k == 0 ) {
            /* draw the graph y-axis */
            /* should also check if there is dot, when x
             * is 0.
             */
            printf("%7d\t_|",((Y_AXIS-(i+1))*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 (j = 0; j < X_AXIS; j ++ ) {
      printf("%2d ", (i * AXIS_VALUE));
   }
   printf("\n");
  }
return getch();
}

andrew
0
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
Sorry Andrew. Our E-Mail has been down and has only just come back up. I've therefore got loads of work to catch up on so I dont think I'll be able to help any more today. I am sure others will be able to help and will get to you soon. I'll check in again tomorrow in case they dont.

I might help to detail exactly what doesnt work with this code but please stress that this is Coursework so people don get into trouble by helping you in the wrong way.

Good luck.

I'll check back tomorrow morning.

Paul.
0
 
LVL 7

Expert Comment

by:aib_42
Comment Utility
First of all,
> void main()
incorrect, use "int main(void)" instead.

Second of all,
the block containing most of main from beneath the declarations to return getch(); is inneccessary, you could get rid of your second ever '{' character and the matching '}'.

Analyzing the output of your program and comparing it to what you should get, you should be able to deduce the errors:
1) There is a very long line with *'s and spaces, and the axis come afterwards.
2) The x-axis' label points are misaligned with their labels.
3) The axis are mislabeled (with the same number)
4) You cannot see if the output of the graph ('*') itself is correct.
5) #4 cannot be fixed without fixing #1 first.

So there you have it. You have to fix #1 first, and then work on the others. Think about your 'printing' capabilities. Can you draw the axis on the screen, then take the cursor back up and draw the graph inside? Or, do you have to print line-by-line?
If first, you should concentrate on drawing the axis around the graph or the graph around the axis. Of course, the graph itself should be properly aligned, you do not want all of the *'s on one line.
If second, you should print the graph also while printing the axis. (HINT: You should have _one_ loop transversing the screen lines from the start to the end, _once_.)
0
 

Author Comment

by:Magibwan
Comment Utility
I've done the first bit obviously thats easy but ive fiddled around with things with it for ages and nothings really working. You cant give me some clues of how to solve the things you've pointed out to me. I'm just a begginer at all this programming lark so i havn't really got a clue what to do, this is really my last hope to get something done about it. Obviously i dont want you to do it all for me but if you could find it in your heart to help me out a bit more that would be great.

Andrew.
0
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
Andrew,

Try posting what you've got now and the output it generates.

Paul
0
 

Author Comment

by:Magibwan
Comment Utility
What ive got here is the axis without the actual graph bit added on.

#include <stdio.h>
#include <conio.h>

#define SCREEN_MAX 20
#define Y_AXIS 10
#define X_AXIS 10
#define AXIS_VALUE 5

int main(void)
{
  int y, i, j, steps, size;
  float chr_str[80];
  {
    printf("Enter a value for X \n");
    scanf("%d", &j);

    printf("Enter step-size\n");
    scanf("%d", &size);

    printf("Enter the number of steps\n");
    scanf("%d", &steps);

      {
        printf(" ");
      }
      printf("*");
    }
   /* 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))*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 (j = 0; j < X_AXIS; j ++ ) {
      printf("%2d ", (i * AXIS_VALUE));
   }
   printf("\n");

return getch();
}

How do i get the graph onto the axies.
0
 

Author Comment

by:Magibwan
Comment Utility
I've now fixed the X axis misalign at the top. That was because the plot i tried to make was interfeing with it. But the Y axis has the wrong numbers. I presume this is because I havn't worked out how to plot the equation yet. I need Y to equal X+10 for a number of steps. Do you have any idea's of how I can do that.

This is my code now:

#include <stdio.h>
#include <conio.h>

#define SCREEN_MAX 20
#define Y_AXIS 10
#define X_AXIS 10
#define AXIS_VALUE 5

int main(void)
{
  int i, j, steps, size;
  float chr_str[80];
  {
    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\n\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))*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 (j = 0; j < X_AXIS; j ++ ) {
      printf("%2d ", (i * AXIS_VALUE));
   }
   printf("\n");

return getch();
  }
}
0
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
Andrew,

It is important to try to post the output to your program if you can. Many of us, being experts, are at work and therefore cannot take the time to compile and run your program to see what you are seeing. If we can see your output it is then often very quick for one of us to see what the problem is and point out where you should look.

Paul
0
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
Andrew,

If you dont know how, tell us what system you are running under and we'll tell you.

Paul
0
 

Author Comment

by:Magibwan
Comment Utility
The output looks something like this.

 45|
 40|
 35|
 30|
 25|
 20|
 15|
 10|
 05|
 00|_______________________________________
       !      !      !      !     !     !     !     !     !     !     !    !
      75   75    75   75   75  75   75  75   75  75   75  75
0
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
Excellent! Looks good so far.

To get the '*'s to print I think you need to change this bit:

              /* 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("   ");

Clearly, you are only printing a space when you should, and the comment confirms it, be printing either a '*' or a ' '. Have a go at that next. It should look like:

              /* 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.
                */
              // Is our current position (j, i) a good place for a '*'
              if ( .... )
              {
               printf("*");
              }
              else
              {
               printf(" ");
              }

The second problem is that the 'x' values are all at 75! Look at this code:

   //print the X axis value;
   for (j = 0; j < X_AXIS; j ++ ) {
      printf("%2d ", (i * AXIS_VALUE));
   }
   printf("\n");

It is deciding the value to be printed from 'i' but the loop is changing 'j'.

Paul
0
 

Author Comment

by:Magibwan
Comment Utility
Right now ive got this:

45|
40|
35|
30|
25|
20|
15|
10|
05|
00|_______________________________
      !     !     !     !     !     !     !     !     !     !
     0     5    10  15  20   25  30   35  40   45

Dont know how to check for the 1st part you gave me.
I'm not sure if (i) has a value yet because it has to equal X+10 and should be in an array. Do you have any clues as to how to do that.
0
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
Excellent!

You seem to imply that you were asked to build an array for the values. Check your notes to see if you are expected to use an array. If that is the case, and you seem to have started that process with:

 float chr_str[80];

Then to begin with, you need a loop that fills this array with the values you want.

You should, however, be able to generate the '*' without an array because you know exactly where you are in terms of x,y coordinates at each point when you need to know. You are 'j' characters along the 'x' axis and 'i' characters along the 'y' axis. Multiply each one by the scale of that axis like you do when you print the axis marker labels and you've got a 'real' x,y coordinate. All you need to decide is if y == x + 10. You are so nearly there I am finding it really hard to avoid doing it for you.


Incidentally, you dont need:

 float chr_str[80];
 { // <----You dont need this
...
 } // <----Or this (the matching one) but they wont cause any problem.

Paul
0
 

Author Comment

by:Magibwan
Comment Utility
My specification does require me to build a 1D array so i can just fill in the *'s where i want them they have to come from the array. If you want to just write it for me be my guest, if you're allowed to that is.
0
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
Andrew,

I'm not allowed to write it for you :). And I wouldnt want to, you'd then be more likely to fail your practicals and I wouldnt want that on my mind. Good try though.

OK! First things first! The name chr_str does not seem too descriptive, nor does it really need to be a float. Perhaps call it 'fx' as in 'f(x)' or 'y_array' or something like that. Make it an integer array because X+10 will always be an integer on your graph and fill it with the value of 'y' for each of the values of 'x' you are interested in. Your code already has something like that for the X axis printing so you should be able to do this. Try to do that next and post the code when you are done.

Its 3:00pm here and I knock off at 5:30 so if I stop responding in a couple of hours I'll be back tomorrow. If that is the case and you get stuck with this problem, I've noticed another requirements you are not meeting. Namely:

>>the axis should ... + (origin)

Hang in there, you're nearly there!

Paul
0
 

Author Comment

by:Magibwan
Comment Utility
I'm in england anyway so it's same time here. So basicly are u saying i need an array of *'s and the program should just miss out the *'s for the ones i dont need eg:

X=2 so and step size is 2  and the number of steps is 10 so Y= 12, 14, 16, 18, 20, 22, 24, 26, 28 and 30.

So the program should miss out *'s for 1-11, 13, 15, 17, 19, 21, 23 and so on.

Or have I got what ur saying totally wrong.
0
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
You want to know, at each i,j in your loop, where on the Y axis the '*' should be. I'd use the array to pre-calculate the row number that the '*' should be at each column so all you need to do in your loop is see if array[j] is equal to 'i'. Once you decide that, the contents of the array become a matter of the correct calculation.

For example, one point on your graph should clearly be:

45|
40|
35|
30|
25|
20|
15|
10|*
05|
00|_______________________________
      !     !     !     !     !     !     !     !     !     !
     0     5    10  15  20   25  30   35  40   45

so clearly array[0] should probably be 10 but remember that you are running j from 0 upwards so you might need to use something like array[... - j] when you finally print it out. Its up to you how much calculation you put in the print loop and how much you put in the pre-calculation loop.

See where I am going?

I'd probably want to use:

if ( i == array[j] )
{
 printf...
}
else
{
 printf...
}

and do ALL the calculation outside the print loop. This is good programming practice anyway, not to mix calculation in with your print loop except perhaps scaling and/or rotation etc.


Paul
0
 

Author Comment

by:Magibwan
Comment Utility
Is this the right sort of code for the calculation loop:

for ( j = 0; j < steps; j++){
  i = j + 10 + size
  printf("i")
 
And should the print loop be something like this:

if ( i == array[j] )
{
printf("*");
}
else
{
printf(" ");
0
 

Author Comment

by:Magibwan
Comment Utility
realized that's not gonna work. How about this:

while ( counter = 0; counter <= steps; counter++){
 i = j + 10 + size

And

if ( i == array[j] )
{
printf("*");
}
else
{
printf(" ");
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
>>while ( counter = 0; counter <= steps; counter++){

wouldnt this be 'for (...'.

>> i = j + 10 + size

you want to fill in the array dont you?

>>if ( i == array[j] )
>>{
>>printf("*");
>>}
>>else
>>{
>>printf(" ");

getting there.

Paul
0
 

Author Comment

by:Magibwan
Comment Utility
Right tried putting that into the code and i got this:

70   |           |      |      |      |      |      |      |     |     |     |      |      |       |       |      |      |      |
      0  5  10 15    20    25    30    35    40    45   50   55   60    65     70     75    80    

What the heck went wrong:

#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 y, i, j, steps, counter, 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");

for (counter = 0; counter <= steps; counter ++){
  y = j + 10 + size;
  scanf("%s", & y);
   /* 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.
                */
               if (y == y_array[y]){
               printf("*");
            }
            else{
              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();
  }
  }
}
  }
0
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
>> for (counter = 0; counter <= steps; counter ++){
>>  y = j + 10 + size;
>>  scanf("%s", & y);
>>   /* traverse through the y-axis */

Why are you trying to print the graph 'steps' times?

Remember that any loop operates on the whole block. You only need to put an entry in the array.

Look at this bit of code:

   //print the X axis value;
   for (i = 0; i < X_AXIS; i ++ ) {
      printf("%2d ",(i * X_AXIS_VALUE));
   }

and work out what it does.

Paul
0
 

Author Comment

by:Magibwan
Comment Utility
That would print the X axis value, which for this would be Y so that means i should put the if (y == y_array[y]){ printf("*"); in this bit right. But where does the calculation loop go?
0
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
I am suggesting that you use something like this for the calculation loop which goes at the beginning. Use the loop structure but replace the printf with something like:

y_array[i] = ...;

Paul
0
 

Author Comment

by:Magibwan
Comment Utility
Something like this

for (counter = 0; counter <= steps; counter ++){
y_array[i] = j + size + 10;

Maybe i should explain what im trying to calculate. I'm trying to get it to calculate j + size + 10 for the number of steps ive got is this right or have i gone totally wrong.
0
 

Author Comment

by:Magibwan
Comment Utility
Hello whats going on. Sorry but i've got to get this in today.
0
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
And I'm trying to get you to use:

   //Build the array.
   for (i = 0; i < X_AXIS; i ++ ) {
      y_array[i] = (i * X_AXIS_VALUE) + 10; // This may not be the correct calculation but it is a start.
   }
0
 

Author Comment

by:Magibwan
Comment Utility
Ok but how do i get that to print on the graph. Do i do this at the bottom after

//print the X axis value;
   for (i = 0; i < X_AXIS; i ++ ) {
      printf("%2d ",(i * X_AXIS_VALUE));
   }

Or what
0
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
Thats the bit you do at the end to print the X axis. That is working so leave it alone.

The code I just gave you should go at the start after the values have been entered to populate the array. Then, where you choose the '*' or ' ' to print, use the contents of the array to help you decide.

I'm sorry but I've got way too much to do today. I hope you crack it before handin time.

Good luck.

Paul
0
 

Author Comment

by:Magibwan
Comment Utility
Thanks for helping so much but its a bit annoying being so close to getting it done and not getting there ive tried just about every place to print the array but doesnt seem to want to put it on the axis. I'll keep going though.
Thanks again

Andrew.
0
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
Have you got a debugger? It is often much easier to see what is going wrong when you step the the code watching what the variables are doing.

Paul
0
 

Author Comment

by:Magibwan
Comment Utility
yeah but there's nothing acctually wrong with the code it just wont do what i want it to do. Do you have any idea of how i can plot the array on the graph i'm getting a bit desperate.
0
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
Put that code in at the start to populate the array.

Run your debugger and stop the code at the point where you must decide whether to printa '*' or a ' '.

At this point, look at what is happening with i, j and the array[i] value. You should be able to tell where to do the '*' by looking at your output.

Paul
0
 

Author Comment

by:Magibwan
Comment Utility
I cant get the debugger to do anything. The program apperars to just take the inputs and the draw the axis and values.

This is the code ive got at the minute:

#include <stdio.h>

#define Y_AXIS 17
#define X_AXIS 17
#define X_AXIS_VALUE 5
#define Y_AXIS_VALUE 5
int main(void)
{
  int y, i, j, steps, size;
  int y_array[80];
  {
    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\n");
    for (i = 0; i < X_AXIS; i ++ ) {
         y_array[i] = (j + size) + 10;
       }

   /* 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+3))*Y_AXIS_VALUE));
         }
         /* x > 0 */
         else {
            /* y-axis ( at 0 ) */
            if ( i == Y_AXIS - 3 ) {
               /* 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();
}
}
0
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
You've still got:

               /* 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("   ");
 
This will ALWAYS print spaces in the graph. What happened to the:

if ( i == array[j] )
{
printf("*");
}
else
{
printf(" ");
}

Good luck!

Paul.
0
 

Author Comment

by:Magibwan
Comment Utility
Now i get this output

   70_|











         |     |      |      |       |      |       |      |       |     |      |      |       |     |     |    |     |
         0     5     10    15     20    25     30    35    40    45    50    55    60   65   70   75   80

Dont know whats going on:

        #include <stdio.h>

#define Y_AXIS 17
#define X_AXIS 17
#define X_AXIS_VALUE 5
#define Y_AXIS_VALUE 5
int main(void)
{
  int y, i, j, steps, size;
  int y_array[80];
  {
    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\n");
    for (i = 0; i < X_AXIS; i ++ ) {
         y_array[j] = (j + size) + 10;
       }

   /* 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+3))*Y_AXIS_VALUE));
         }
         /* x > 0 */
         else {
            /* y-axis ( at 0 ) */
            if ( i == Y_AXIS - 3 ) {
               /* draw the x-axis */
               printf("___");
            }
       for (i = 0; i < Y_AXIS; i ++ )
                if ( i == y_array[i] )
    {
    printf("*");
    }
    else
    {
    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();
}
}
  }
0
 

Author Comment

by:Magibwan
Comment Utility
Hi ive sent in what i had but id like to know the proper answer so if u've got time can you give me a hand.
0
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
Look at this, look at the comments, please try to understand.

#include <stdio.h>

#define Y_AXIS 17
#define X_AXIS 17
#define X_AXIS_VALUE 5
#define Y_AXIS_VALUE 5
int main(void)
{
      int y, i, j, steps, size;
      int y_array[80];
      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\n");
      for (i = 0; i < X_AXIS; i ++ ) {
            y_array[j] = (j + size) + 10;
            // **** Lets look at what the array looks like because I think it should be SOMETHING LIKE (j*size)+10.
            printf("%d,", y_array[j]);
      }
      printf("\n\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+3))*Y_AXIS_VALUE));
                  }
                  /* x > 0 */
                  else {
                        /* y-axis ( at 0 ) */
                        if ( i == Y_AXIS - 3 ) {
                              /* draw the x-axis */
                              printf("___");
                        }
                        // **** We dont need another loop here!!!
                        //for (i = 0; i < Y_AXIS; i ++ )
                        // **** This should probably be SOMETHING LIKE 'if ( i == y_array[j] )' but you MUST understand WHY!
                        // **** Look at what's printed out earlier to see what the comparison should be.
                        if ( i == y_array[i] )
                        {
                              printf("*");
                        }
                        else
                        {
                              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();
}

Remember that University is your first opportunity in your life to learn how to think. All of your education up 'till now has been aimed at discovering if you are capable of that. Many people have now said that you can which is why you are there. It is now down to you to prove that their faith in you was justified. If you cheat, you are cheating yourself and making them liars.

I do not want to hear that you failed because I did it for you!! I am trusting  you here.

Paul
0
 

Author Comment

by:Magibwan
Comment Utility
Ive got the right array calculation now but the *'s print on the x axis and not on the space between the axis it also stuffs up the axies making huge gaps it would take a long time to show you the output but here's the code:

 #include <stdio.h>

#define Y_AXIS 17
#define X_AXIS 17
#define X_AXIS_VALUE 5
#define Y_AXIS_VALUE 5
int main(void)
{
     int y, i, j, steps, size;
     int y_array[80];
     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\n");
     for (j = 0; j <=steps; j ++ ) {
          y_array[j] = (j * size) + 10;
          // **** Lets look at what the array looks like because I think it should be SOMETHING LIKE (j*size)+10.
          printf("%d,", y_array[j]);
     }
     printf("\n\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+3))*Y_AXIS_VALUE));
               }
               /* x > 0 */
               else {
                    /* y-axis ( at 0 ) */
                    if ( i == Y_AXIS - 3 ) {
                         /* draw the x-axis */
                         printf("___");
                    }
                    // **** We dont need another loop here!!!
                    //for (i = 0; i < Y_AXIS; i ++ )
                    // **** This should probably be SOMETHING LIKE 'if ( i == y_array[j] )' but you MUST understand WHY!
                    // **** Look at what's printed out earlier to see what the comparison should be.
                    if ( i == y_array[j] )
                    {
                         printf("*");
                    }
                    else
                    {
                         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();
}
0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 150 total points
Comment Utility
Look at what appears as a result of the:

          // **** Lets look at what the array looks like because I think it should be SOMETHING LIKE (j*size)+10.
          printf("%d,", y_array[j]);
 
Work out what 'i' and 'j' are here:

                    // **** This should probably be SOMETHING LIKE 'if ( i == y_array[j] )' but you MUST understand WHY!
                    // **** Look at what's printed out earlier to see what the comparison should be.
                    if ( i == y_array[j] )
                    {
                         printf("*");
                    }
                    else
                    {
                         printf(" ");
                    }

and you should get a good idea what the problem is.

Paul
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now