# write a function

i want to write a function as following:

write and test a single function which sets a small array with 2 characters to the ordinal suffix of numbers 1st, 2nd, 3rd, 4th and suffix refers to the letters.

void suffix(int n, char suff[]);

assumming the following declarations: char s[5];
int x;
and the result of call suffix(x,s);

and some sample of the values for x and exped value of s are:
1  2  3  4   100  101  102
st nd rd th  th   st    nd
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Author Commented:
who i can write this function, and make sure that any string in s for any given positive value of x from 0 to 9999 i can get the answer like example  1 st and so on
thanks for you help if you can help me...and hopefully if i get an answer to understand and learn about it.

jona
0
Commented:
Dear jona.
I can't understand your question.
Do you speak english ?

If not, try to write your C++ program as best as you can.
Maybe we will understand it better.

0
Commented:
Cases based on n mod 10
0
Commented:
jonatwin,

I think I know what you are asking, sort of.

The algorithm is to solve it goes like so.

You need to check if the number's tens position is between 11 through 13 because that's a special condition where the "ordinal suffix" is not consistent with the rest of the number. To do this you must use the modulo operator % with 100. If it is not a number between 11 and 13, then use the regular set of ordinal suffixes.

Simple way to do this is to have two sets of ordinal suffixes, one set is for numbers ending with 11 through 13 and the other ordinal suffixes are for all other numbers. Use a switch statement for 11, 12, and 13, and default for the rest.

For example,
2013 should print 2013th
2003 should print 2003rd
2014 should print 2014th
2004 should print 2004th

Notice the difference with 2013 and 2003 vs 2014 and 2004.

I hope I understood your question. Post your code if it's still not clear.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
i think i would go with n_fortynine's idea.  simply mod the position by 10.  if the result is 1, you know what you want is "st", if 2 "nd", if 3 "rd", and all others should "th".

a simple switch statement would probably work for determing which suffix to use.

0
Commented:
violent1, using n_fortynine's approach would produce 11st, 12nd, and 13rd for those special cases.
0
Commented:
i dont understand the function declaration -
"void suffix(int n, char suff[]);"
when you say -
"assumming the following declarations: char s[5]; int x;"
whats the 5?

Heres a program that will return the suffix for any given number.

#include <stdio.h>

const char *suffix(int i)
{
/* check if its an "x-teenth" */
if (((i / 10) % 10) == 1) return "th";

switch (i % 10)
{
case 1 : return "st";
case 2 : return "nd";
case 3 : return "rd";
}

return "th";
}

int main()
{
const int n[] = { 1, 2, 3, 2013, 2003, 2014, 2004, 0 };
const int *p = n;
while (*p++) printf("%d%s\n", *p, suffix(*p));
return 0;
}

and the output:

2nd
3rd
2013th
2003rd
2014th
2004th
0th
0
Commented:
ah, just saw a bug when i posted it!
do { printf("%d%s\n", *p, suffix(*p)); } while (*p++);
...but it doesnt affect the suffix() algorithm

output:
1st
2nd
3rd
2013th
2003rd
2014th
2004th
0th
0
Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Split between fivesigmaevent and matja

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0
Commented:

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Split between fivesigmaevent and matja

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.