Solved

# write a function

Posted on 2003-03-15
Medium Priority
270 Views
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
0
Question by:jonatwin
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

Author Comment

ID: 8142239
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

LVL 5

Expert Comment

ID: 8142358
Dear jona.
Do you speak english ?

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

0

LVL 4

Expert Comment

ID: 8142423
Cases based on n mod 10
0

Accepted Solution

fivesigmaevent earned 189 total points
ID: 8142618
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

Expert Comment

ID: 8143638
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

LVL 2

Expert Comment

ID: 8144047
violent1, using n_fortynine's approach would produce 11st, 12nd, and 13rd for those special cases.
0

Assisted Solution

matja earned 186 total points
ID: 8146211
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

Expert Comment

ID: 8146213
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

LVL 9

Expert Comment

ID: 9495922
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

Tinchos
EE Cleanup Volunteer
0

LVL 9

Expert Comment

ID: 9495923

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

Tinchos
EE Cleanup Volunteer
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generatâ€¦
Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and â€¦
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Botâ€¦
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
###### Suggested Courses
Course of the Month8 days, 22 hours left to enroll