Ask Question

The array s of ints contain integers each of which is between 1 and 1000 (inclusive). write code that stores in the variable ordinals the array of strings consisting of each number followed by its ordinal number abbreviation, "st", "nd", "rd", or "th". for example if s is the array

+2
Answers (1)
  1. 21 July, 15:30
    0
    Answer and Explanation

    An array holds a fixed number of values of a single type. Its length is established when the array is created where the length is fixed after the creation. In c programming an array is a collection of data items all of them of the same type and accessed using a common name while in Java they are items that are dynamically created and all elements have the same type called the component type of the array.

    //takes an array of int and returns an array of String the way you want!

    public String[] setOrdinal (int[] arr)

    {

    String[] ord = new String[arr. length];

    for (int i = 0; i < ord. length; i++)

    {

    ord[i] = convert (arr[i]);

    }

    return ord;

    }

    //takes a number and converts it to string and adds the proper letters

    private String convert (int num)

    {

    if (num > 1000 || num <=0)

    return "Not in range";

    else if (num%10==0 || num%10==9 || num%10==8 || num%10==7 || num%10==6 || num%10==5 || num%10==4 || num%10==3)

    {

    return String. valueOf (num) + "th";

    }

    else if (num%10==2)

    {

    if ((num/10) %10==1) / /checks if the second digit is 1

    return String. valueOf (num) + "th";

    else

    return String. valueOf (num) + "nd";

    }

    else

    {

    if ((num/10) %10==1)

    return String. valueOf (num) + "th";

    else

    return String. valueOf (num) + "st";

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “The array s of ints contain integers each of which is between 1 and 1000 (inclusive). write code that stores in the variable ordinals the ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers