Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple calculation

Status
Not open for further replies.

lykeat

Programmer
Sep 1, 2003
22
MY
Hi there, just asking some simple calculation. Now i need to create a program that let user to key in a number, after that it will convert the number to an array that store the value start from a. For example, if a user key in 5, then the result will be {a,b,c,d,e}. If the number they key in more than 26, then it will continue like{a,b,...,a1,a2}. just like Excel.
Hope some one can help, thank you.

Regards,
Henry
 
OK, I will describe how I'd do it. (No code tho' sorry ;-)

1) Create an array at a set size (say 5 elements)
2) read in user input
3) Use Realloc() to re size your array to the correct number of elements.
4) Start at element 0 of the array, and write in an 'a' to the first element.
5) increment to the next step and add in a 'b' etc.

This can be optimised by having the 'a' saved as a variable, (I can't remember the ASCII off the top of my head) and just adding one to it each time. When this value is > than 'z', yo uneed to reset it to 'a' and also you need a flag to begin to concatenate a '1' to it as well.

Be aware when you use realloc how big each of your array elements could be, if you size it to 28 elements of 1 char, then when you get to 'a1' you will crash (as this is obviously 2 chars). In fact, if you are going to store the data as char, you need to remember to allow for the '\0' at the end of the string as well.

Hope thats enough to get you started, reply here to ask for more help, or to let us know how you got on !

K
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top