14 Replies - 829 Views - Last Post: 04 October 2012 - 11:16 PM Rate Topic: -----

#1 toners  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-April 12

ASCII graph art

Posted 04 October 2012 - 01:45 PM

I'm trying to create a program that displays a mathematical function as graph using hash tags. The number of hash tags are based on the y value of the funtion. This is an example of the function: y=10*(1+sin(x))
A plot of y = 10*(1+sin(x)) from x=0.00 to 20.00
X Y
0.00 10.00 ##########
0.20 11.99 ############
0.40 13.89 ##############
0.60 15.65 ################
0.80 17.17 ##################
1.00 18.41 ###################
1.20 19.32 ####################
1.40 19.85 ####################
1.60 20.00 ####################
1.80 19.74 ####################
2.00 19.09 ####################
2.20 18.08 ###################
2.40 16.75 #################
2.60 15.16 ################
2.80 13.35 ##############
3.00 11.41 ############
3.20 9.42 ##########
3.40 7.44 ########
3.60 5.57 ######
3.80 3.88 ####
4.00 2.43 ###
4.20 1.28 ##
4.40 0.48 #
4.60 0.06 #
4.80 0.04 #
5.00 0.41 #
5.20 1.17 ##
5.40 2.27 ###
5.60 3.69 ####
5.80 5.35 ######
6.00 7.21 ########
6.20 9.17 ##########
6.40 11.17 ############
6.60 13.12 ##############
6.80 14.94 ###############
7.00 16.57 #################
7.20 17.94 ##################
7.40 18.99 ###################
7.60 19.68 ####################
7.80 19.99 ####################
8.00 19.89 ####################
8.20 19.41 ####################
8.40 18.55 ###################
8.60 17.34 ##################
8.80 15.85 ################
9.00 14.12 ###############
9.20 12.23 #############
9.40 10.25 ###########
9.60 8.26 #########
9.80 6.34 #######
10.00 4.56 #####
10.20 3.00 ####
10.40 1.72 ##
I don't know how to get the hash tages to print off, and this is the code I have so far:
#include <stdio.h>
#include <math.h>

int main(int argc, char *argv[])
{
        float x,y;

        printf("X\tY\n");
        for(x=1;x<20;x++)
        {
                y =5*sin(1+(2*x));
                printf("%.2f\t%.2f\t%c\n",x,y,'#'*y);
        }

        return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: ASCII graph art

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,751
  • Joined: 25-December 09

Re: ASCII graph art

Posted 04 October 2012 - 01:58 PM

What output are you getting for your program?

Have you thought about using a loop to print the correct number of hash marks?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,595
  • Joined: 05-May 12

Re: ASCII graph art

Posted 04 October 2012 - 02:00 PM

Using '#'*y is not going to print out y hash marks. Consider using a for loop to print out y hash marks instead.
Was This Post Helpful? 0
  • +
  • -

#4 toners  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-April 12

Re: ASCII graph art

Posted 04 October 2012 - 02:45 PM

I know I need a for loop, but I don't know how to make it print y hash marks. Any hints?
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,751
  • Joined: 25-December 09

Re: ASCII graph art

Posted 04 October 2012 - 02:49 PM

Study this link: Control structures, think "for".

Jim
Was This Post Helpful? 0
  • +
  • -

#6 Duta  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 21-June 12

Re: ASCII graph art

Posted 04 October 2012 - 04:25 PM

Am I allowed to post code for this, or do I have to leave it up to the OP to work out for themselves? Its in C++ instead of C anyway, but could quite easily be converted (by changing the output method from cout)... Figured I should ask first.
(By the way, thanks for this thread; its a simple program but for some reason I think ASCII graphs are really cool).
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,595
  • Joined: 05-May 12

Re: ASCII graph art

Posted 04 October 2012 - 04:31 PM

You should let the OP figure it out for themselves, but feel free to give them hints as they work their way through the problem.

And yes, some ASCII graphs are beautiful, specially when run on an amber monochrome monitor with the brightness set just right so that there is a nice "ghosting" effect. :)
Was This Post Helpful? 0
  • +
  • -

#8 toners  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-April 12

Re: ASCII graph art

Posted 04 October 2012 - 04:38 PM

Do I have to make the # a char variable?
Was This Post Helpful? 0
  • +
  • -

#9 Duta  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 21-June 12

Re: ASCII graph art

Posted 04 October 2012 - 04:42 PM

Fair enough.
After playing around with different f(x)s (where y = f(x)) I've decided that if I get the time I'll make a more complete ASCII graph generator... Emphasis on beautifulness (if that's even a word) rather than functionalness (again, probably not a real word), because really no-one except me will be using it.

@OP, do update on how you're getting on :)

EDIT: In the time it took for me to write this post, you posted.

In answer to your question, you could do but its not necessary.

This post has been edited by Duta: 04 October 2012 - 04:43 PM

Was This Post Helpful? 0
  • +
  • -

#10 toners  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-April 12

Re: ASCII graph art

Posted 04 October 2012 - 04:51 PM

Okay, so do I need to creat a nested for loop within the loop that prints the x and y values or is there a way to print the # in the same printf?
Was This Post Helpful? 0
  • +
  • -

#11 Duta  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 21-June 12

Re: ASCII graph art

Posted 04 October 2012 - 04:55 PM

View Posttoners, on 04 October 2012 - 04:51 PM, said:

Okay, so do I need to creat a nested for loop within the loop that prints the x and y values or is there a way to print the # in the same printf?

I'm going to give a probably quite annoying answer; you can do it both ways.
Being slightly more specific, to have it all in one printf, you'd need a separate function (unless there's some method I'm overlooking)
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,595
  • Joined: 05-May 12

Re: ASCII graph art

Posted 04 October 2012 - 05:18 PM

My recommendation is do a nested for loop first so that you have something working to submit in case you run out of time for your assignment. It will be the simplest way to get something working.

Then with that done (and having made a backup), then explore other approaches like the separate function mentioned by Duta.
Was This Post Helpful? 0
  • +
  • -

#13 Duta  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 21-June 12

Re: ASCII graph art

Posted 04 October 2012 - 05:24 PM

View PostSkydiver, on 04 October 2012 - 05:18 PM, said:

My recommendation is do a nested for loop first so that you have something working to submit in case you run out of time for your assignment. It will be the simplest way to get something working.

Then with that done (and having made a backup), then explore other approaches like the separate function mentioned by Duta.

This is the road I would take.

Also, Skydiver he didn't say it was an assignment did he? Of course, many code help questions are for assignments..
Was This Post Helpful? 0
  • +
  • -

#14 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: ASCII graph art

Posted 04 October 2012 - 10:32 PM

Functions that will be useful for this

by Gabehabe

can be found in the DIC snippets section

a function to clear the screen
http://www.dreaminco...snippet1979.htm

a gotoxy function to place the '#' anywhere on the screen anywhere you like
http://www.dreaminco...snippet1980.htm

Best Wishes

Snoopy.

This post has been edited by snoopy11: 04 October 2012 - 10:33 PM

Was This Post Helpful? 0
  • +
  • -

#15 Duta  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 21-June 12

Re: ASCII graph art

Posted 04 October 2012 - 11:16 PM

Personally I'm not a fan of using gotoxy()s etc unless its really necessary (eg making a text game sometimes needs it, along with other applications of course)... It just seems a bit hackish.
Of course, it's completely your decision, I'm just saying; if you don't have to, why bother? (Also use of gotoxy would probably complicate this.)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1