473,405 Members | 2,421 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,405 software developers and data experts.

Filling a string with characters '#' and space to create bar-graph

I want to create a bar-graph display using a string. The string needs to be 14 characters long, filled from the left with either a '#' character or a ' ' depending on the value of a variable, level.

Each character represents 1/14 of the total (this is my display width), so as level rises from 0 to 14, there will be more # characters on the left, and fewer spaces, viz:

Expand|Select|Wrap|Line Numbers
  1. If level < 1, the string should be:  "              "
  2. If level == 1, the string should be: "#             "
  3. If level == 7, the string should be: "#######       "
  4. If level == 14, the string should be: "##############"
I started by using a for loop, but it looked very messy. Is there a shortcut in ANSI C?

Note: I'm a newbie, so would appreciate simple suggestions, without pointers if possible!
Mar 1 '10 #1
7 3666
BTW, in case anyone thinks to suggest a neater display method using points and lines, or other graphics.

This is for use on a microcontroller and must be done like this. I also lied about using a #. I just can't create the actual character here as it is a self created character (to replace the Yen character in the ASCII set), and it looks like the bars in a bar-graph :)
Mar 1 '10 #2
jkmyoung
2,057 Expert 2GB
One possible way is to have a hardcoded string, and use memcpy from string.h in the standard library.

fullStr: "##############"
if (level > 0)
memcpy(str, fullStr, level);

Might be easier if you post your for loop code, and we edited that.
Mar 1 '10 #3
donbock
2,426 Expert 2GB
Another way would be to have an array of all 15 possible bar graph strings and compute an array index based on your level variable.
Mar 1 '10 #4
@jkmyoung
In fact my loop code does not work, but here it is:

Expand|Select|Wrap|Line Numbers
  1.  void upDisp(void)
  2. { // Update display
  3.   char bargraph[15] = "0123456789ABCD";
  4.   byte level;
  5.  
  6.   fuelRead8 = getFuel();       // get ADC value from channel 0
  7.   ByteToStr(fuelRead8, txt4);  // convert to string for display
  8.   strLCD11XY(txt4, 0, 0);      // display on LCD in large font at 0,0
  9.  
  10.   level = (fuelRead8/18); // DEGUB temp calculation, munged for test!
  11.   ByteToStr(level, txt4); // convert to string for display
  12.   strLCD5XY(txt4, 0 , 3); // display on LCD in small font at 0,3
  13.  
  14.   // come here with 0 <= level <= 14
  15.  
  16.   for (i = 0; i == 13; i++)
  17.   {
  18.     if (level > i)
  19.     {
  20.       bargraph[i] = '#';
  21.     }
  22.     else
  23.     {
  24.       bargraph[i] = '-';
  25.     }
  26.   }
  27.   strLCD5XY(bargraph, 0 , 5); // display on LCD in small font at 0,5
  28.  
  29. }
The idea is that in the loop [line 16] the characters in the string barcode get changed. In fact they stay with the initial values.

I'm going to check on something, because I think the original string may get put in ROM, which would explain why it doesn't work!
Mar 1 '10 #5
jkmyoung
2,057 Expert 2GB
So you're saying it always displays:
0123456789ABCD?


Besides initializing it to all full or all empty, could also break it into 2 for loops.
Expand|Select|Wrap|Line Numbers
  1.   for (i = 0; i <= level; i++) 
  2.       bargraph[i] = '#'; 
  3.   for (i = i; i <= 14; i++) 
  4.       bargraph[i] = '-'; 
  5.  
Mar 1 '10 #6
@jkmyoung
All sorted. Thanks for giving me the idea where to look in the string library, I also discovered memset:

Expand|Select|Wrap|Line Numbers
  1. void upDisp(void)
  2. { // Update display
  3.   char bargraph[15] = "              ";
  4.   char reserve[15] =  "*RESERVE TANK*";
  5.   byte level;
  6. // set level here
  7.   if (level == 0)
  8.   {memcpy(bargraph, reserve, 14);}
  9.   else
  10.   {memset(bargraph, 0x5C, level);} // copy block characters into string
  11.   strLCD5XY(bargraph, 0 , 5); // display on LCD in small font at 0,5
Mar 1 '10 #7
[quote=nigelmercier;3549785]

Expand|Select|Wrap|Line Numbers
  1. for (i = 0; i == 13; i++)
  2. //should be i <= 13 
I've also found the error in the loop, very surprised none of you guys spotted it!

For some reason I can't get it into my head that the second expression in a for statement is a while rather than a terminating condition! Must be all that BASIC I did in the 1970s.
Mar 3 '10 #8

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

Similar topics

0
by: Dolphe Tilly | last post by:
Hi All, I need to set some string array space "REPLACE ME" aside. In the compiled version I wish to replace the text string for something else using a hex-editor. Reason: password acces to the...
3
by: Harry | last post by:
Folks I am trying to input the characters of a string into an array. The array needs to hold strings, so I need to convert the characters into strings and then input them into the array. I run...
5
by: Jawahar Rajan | last post by:
All, 1) When using the QueryString of the request object the actual values are exposed to the viewer of the site and often user pickup on these values and start changing them . This can lead user...
1
by: Raph | last post by:
hi, i would like export data to a DEL file, but without string characters delimiters ! Do you have some solutions or examples ? thanks Raph
6
by: brian_harris | last post by:
I have a function that passes a string class pointer to a function, this function is then suppose to fill it and the outer function uses it. But I believe that I am running into problem due to...
3
by: shaft | last post by:
hi i have the following code : string addrress; cin >> address; cout << address; if I enter any string with space in between , it will save the first word only. e.g. if i enter "this is...
5
by: Helmut Jarausch | last post by:
Hi, I'm looking for an elegant solution to the following (quite common) problem: Given a string of substrings separated by white space, split this into tuple/list of elements. The problem...
2
by: skumari | last post by:
Hello I am new user of this Forum.Can anyone help me regarding "How tp Replace a new line character "\n" in a string with space or ," Using Javascript. Please reply me ASAP.
0
by: koosh34 | last post by:
I want to add a progress bar control on to a form that shows how much space is being used up on the drive the application is running on. How do I find out in the how big the disk is and how much...
2
by: nhbach | last post by:
I have a long string in db $code = "ACTGTCTACTGGTCTAGCTAGTCATGCTAGTAATCG" How to separate a long string to short string with space each 5 character? Example: ACTGT CTACT GGTCT AGCTA GTCAT...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.