473,414 Members | 1,723 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Histogram C++

Hi,
I'm a mathematics major, so the concepts behind this make sense, but I have the hardest problem putting it into computer language.

So I am supposed to write a function that produces this histogram:

Expand|Select|Wrap|Line Numbers
  1.     |                                         
  2. 18+                                         
  3.     |       
  4. 16+       
  5.     |        
  6. 14+      
  7.     |         
  8. 12+     
  9.     |    
  10. 10+     
  11.     |    
  12.   8+        
  13.     |       
  14.   6+     
  15.     |       
  16.   4+     
  17.     |   
  18.   2+     
  19.     |   
  20.     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  21.         0  1   2  3  4  5  6  7   8  9 10 1112 1314 15 161718 19 2021 22 2324 25
Obviously since I created this by typing, it's a little off, but that's the idea behind the program.

I have the following function (in progress, does not output the numbers on either the x or the y-axis):

Expand|Select|Wrap|Line Numbers
  1. void graph(int a[], int lo, int hi, int ymax)
  2. {
  3.    int x, y;
  4.    for (y=19; y>=-1; y--)
  5.    {
  6.       for (x=-2; x<=25; x++)
  7.       if (y==0 && x>=0 && x%3==0)
  8.          cout << "+";
  9.       else if (y==0 && x>=0 && (x%2==0 || x%1==0))
  10.          cout << "-";
  11.       else if (x==0 && y>=0 && y%2==0)
  12.          cout << "|";
  13.       else if (x==0 && y>=0 && y%1==0)
  14.          cout << "+";
  15.       else
  16.          cout << " ";
  17.    }
  18. }

Why do I get some horribly spread out mess?!

I feel like I should be using x and y to represent the xy-axis, so when x==0 I'm referencing the origin and then making y be variable and vice versa, but the screen comes up jumbled.
Nov 16 '07 #1
4 6184
oler1s
671 Expert 512MB
o when x==0 I'm referencing the origin
How does that correspond in any way to output actually being at the "origin"? The console has no concept of graphs and positions and what not. You get line output, and that's about it.

EDIT: Even if it did, your code is meaningless. How does setting x= 0 have any effect on the console output position?
Nov 17 '07 #2
How does that correspond in any way to output actually being at the "origin"? The console has no concept of graphs and positions and what not. You get line output, and that's about it.

EDIT: Even if it did, your code is meaningless. How does setting x= 0 have any effect on the console output position?


Obviously I'm having problems, but thanks for responding in the rudest way possible. I appreciate your help.
Nov 17 '07 #3
AHMEDYO
112 100+
HI...


no man you can do that using VC++ and if VC++ have some restriction about Screen Graph when using consol application, C++ can do that by graph and colors too, i will see what i can do and i will post you again


GOOD LUCK
Nov 17 '07 #4
AHMEDYO
112 100+
Hey Man..

First i wanna say , when you have variables in your code as int x,y when you change values on x or y it doesnt effect any thing except value placed in memory but does not effect screen or anything like that

and I Think that i got it for you!!!

First include Windows.h Header File in your .Cpp File and use my WriteToScreen Function instread of cout.., i test it by your code and i got your result but it reserved by Y position :D lol , i think you can handle that,for sorry im not good in graph.

Expand|Select|Wrap|Line Numbers
  1. #include <Windows.h>
  2.  
  3. void  graph();
  4. DWORD WriteToScreen(LPSTR Text,WORD XPos,WORD YPos);
  5.  
  6. int main(int argc, char* argv[])
  7. {
  8.     graph();
  9.     return 0;
  10. }
  11.  
  12. DWORD WriteToScreen(LPSTR Text,DWORD n_char,WORD XPos,WORD YPos)
  13. {
  14.     HANDLE    Outputhandle=GetStdHandle(STD_OUTPUT_HANDLE);
  15.     COORD    ConsolCursor;
  16.     DWORD    cbwchars=0;
  17.     Outputhandle=Outputhandle;
  18.     ConsolCursor.X=XPos;
  19.     ConsolCursor.Y=YPos;
  20.     SetConsoleCursorPosition(Outputhandle,ConsolCursor);
  21.     WriteConsole(Outputhandle,(CONST VOID*)Text,n_char,&cbwchars,NULL);
  22.     return cbwchars;
  23. }
  24.  
  25. void graph()
  26. {
  27.    int x, y;
  28.    for (y=19; y>=-1; y--)
  29.    {
  30.       for (x=-2; x<=25; x++)
  31.       if (y==0 && x>=0 && x%3==0)
  32.          WriteToScreen("+",1,x,y);
  33.       else if (y==0 && x>=0 && (x%2==0 || x%1==0))
  34.          WriteToScreen("-",1,x,y);
  35.       else if (x==0 && y>=0 && y%2==0)
  36.          WriteToScreen("|",1,x,y);
  37.       else if (x==0 && y>=0 && y%1==0)
  38.          WriteToScreen("+",1,x,y);
  39.       else
  40.          WriteToScreen(" ",1,x,y);
  41.    }
  42. }
i hope this code help you

GOOD LUCK...
Nov 17 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Oracle3001 | last post by:
Hi All, I am trying to use JAI to build a histogram of an image i have. I have posted the code below, and the error I get at runtime. I have taken the code from the offical java examples, so I am...
1
by: bleh | last post by:
....to include a removeData(datatoremove) function, to mirror the existing addData(datatoadd) function. If anybody knows of somewhere where this has been done already, if you could point me in...
27
by: ext_u | last post by:
Ok I thought I would try to take the program one thing at a time. (If you remember my last post I am trying to make a histogram with data on the size of each word) Anways first .. I obviously...
12
by: KraftDiner | last post by:
Hi, I wrote a C++ class that implements an n dimensional histogram in C++, using stl maps and vectors. I want to code this up now in Python and would like some input from this group. The C++...
5
by: Enigma Curry | last post by:
I'm playing around with matplotlib for the first time. I'm trying to make a very simple histogram of values 1-6 and how many times they occur in a sequence. However, after about an hour of...
2
by: Daniel Nogradi | last post by:
How does one do a histogram on only a part of an image? This is what I found in the PIL documentation about histogram( ): """ im.histogram(mask) =list Returns a histogram for those parts of...
5
by: arnuld | last post by:
this is a programme that counts the "lengths" of each word and then prints that many of stars(*) on the output . it is a modified form of K&R2 exercise 1-13. the programme runs without any...
12
by: arnuld | last post by:
i was able to create a solution for a Horizontal-Histogram. i was completely unable to understand this Vertical-Histogram phenomenon. even though i have looked at the solution at this page: ...
1
by: avenger3200 | last post by:
Hello everyone, I am trying to make a histogram for a project in my class and I really need some help. Here is the question that my instructor provided: Create 1000 Random numbers between...
0
by: Kurt Smith | last post by:
On Fri, Jul 25, 2008 at 5:02 PM, aditya shukla <adityashukla1983@gmail.comwrote: the 'bins' argument to pylab.hist() is supposed to be an integer or a list of the bins' lower edges. The default...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.