GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Apr-2005, 10:40
lilmainy lilmainy is offline
New Member
 
Join Date: Apr 2005
Posts: 5
lilmainy is on a distinguished road

Creating A Graph In C


Hi All Again

I have produced a program so far that creates an '*' for an equation thus code is below.. but now i wish too add x and y axis and numbers to the program any ideas i am really struggling. Question and My Answer So Far Below:

QUESTION

Write a program that displays the graph of the following equation

Y=X+10

The program should first ask the user for:
1. the X axis starting point
2. the number of steps
3. step-size

It should then repeatedly calculate the value of 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, e.g. 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, then 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.


Add axis to the graph using

a) ‘-‘ (minus) character for the Y axis
b) ‘|’ (pipe) character for the X axis
c) ‘+’ character in the lower left to show where they intersect at 0,0

Add numbers to the X and Y axis. The numbers on the X axis should be dictated by the user input.


MY ANSWER SO FAR

CPP / C++ / C Code:

#include <stdio.h>
#include <string.h>
int main(void)
{
  /* Delclaring Varibles */
  char str[81];
  int start, stepsize, steps;
  int x, y;

  /* Initialize the last char to \0 */
  str[80] = '\0';

  /* Get the parameters from the user */
  printf("please enter x axis starting point: ");
  scanf("%d", &start);
  printf("You entered %d\n", start);
  printf("please enter stepsize: ");
  scanf("%d", &stepsize);
  printf("You entered %d\n", stepsize);
  printf("please enter no of steps: ");
  scanf("%d", &steps);
  printf("You entered %d\n", steps);

  /* For Loop */
  for (x = start; x < steps; x += stepsize)
  {
    /* initialize the array */
    memset(str, ' ', 80);

    /* Compute y */
    y = x + 10;
    printf("x = %d, y = %d\n", x, y);

   /* make sure y isn't over the limits */
  if (y > 80)
  {
    /* hat in the 79th element of the array */
    str[79] = '^';
   }
   /* checking if y is less than 0 */
  else if (y < 0)
  {
    str[0] = 0;
  }
    /* Set the appropriate char to */
  else
  {
    str[y] = '*';
    str[y+1] = '\0';
  }

    /* Print the result */
    printf("%s\n", str);
  }
  getch();
}

 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I import another Lib while I am creating a static library in C++.Net? How? george MS Visual C++ / MFC Forum 2 06-Jan-2005 16:07
Help creating lists of pointers The_Kingpin C Programming Language 0 11-Dec-2004 13:49
Problem with the function creating updating a linked list nkhambal C Programming Language 3 28-Oct-2004 20:45
creating new file Nelly CPP / C++ Forum 5 22-Apr-2004 02:35
algorithm for display complex graph ctai010 CPP / C++ Forum 0 20-Mar-2004 05:24

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:37.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.