How to alphabetically sort an array while converting numbers into words?


Introduction

This tutorial deals with the problem of sorting an array alphabetically while each number is converted into words. To translate numbers into words means changing numbers to their number names. Like, 65 is sixty-five. Here, we consider a numerical array, convert all array elements to words and arrange them alphabetically. Print the sorted array elements after converting words to their respective numbers.

Demonstration 1

Input = Arr = {13, 1, 6, 7}
Output = 1 7 6 13

Explanation

The input array elements are 13, 1, 6, 7

The output with alphabetically sorted elements is 1 7 6 13. The number after converting to words looks like this −

1 = one
7 = seven
6 = six
13 = thirteen

Hence, all numbers are alphabetically sorted by their number names.

Demonstration 2

Input = Ass = {17, 34, 65, 12, 10}
Output = 17 65 10 34 12 

Explanation

Using the input array elements{17, 34, 65, 12, 10}, the sorted numbers after converting to words are 17 65 10 34 12. The word conversion of these numbersis as follows −

17 = Seventeen
65 = Sixty-five
10 = ten
34 = thirty-four
12 = twelve

Hence, all words are sorted alphabetically.

C++ Library Functions

  • sizeof − It is used to find the size of the variables, data types during compile time as it is a compile time operator. It is a keyword in C++.

sizeof(value);
  • Vector − It is a dynamic array in C++. ITs all functions are defined in the <vector> header file. It is one of the data structures for storing elements without predefining the size.

vector<data_type> vector_name;
  • Map − It is a data structure for storing elements with their mapped value and key value. All its elements are associated with key and mapped value pairs. The keys are unique and are sorted in some specific manner.

map <data_type> map_name;
  • vector.push_back() − It is a predefined function in the <vector> header file. It pushes or inserts an element to the end of the vector.

vector_name.push_back(value);
  • vector.begin() − It is a predefined function of the vector header file. It returns the starting position of the vector.

vector_name.begin();
  • vector.end() −It is a predefined function of the vector header file. It returns the ending position of the vector.

vector_name.end();
  • size() − It is a standard C++ library function. It returns the length of the input string.

string.size();
  • vector.sort() − This vector class library function sorts the vector elements in ascending order. It takes parameters for sorting the vector elements.

sort(vector_name.begin(), vector_name.end());

Algorithm

  • Take a numerical array.

  • Convert each number of the array to words.

  • For converting to words consider a vector.

  • Save the number names into the vector.

  • Iterate the vector for each number to get its word form.

  • After converting each number to its word, sort all numbers alphabetically.

  • Printed the sorted array elements.

Example 1

Implement an approach to alphabetically sort array elements using their number names. Declare vectors to store words for all numbers. When the code finds a number greater than a two-digit number, it breaks the number and determines its words. Iterate over all vectors to find words for each number.

#include <bits/stdc++.h>
using namespace std;

string ones[]  // storing words for the from 1 to 19
   = { "", "one ", "two ", "three ","four ", "five ", "six ","seven ", "eight ", "nine ", "ten ",
   "eleven ", "twelve ", "thirteen ","fourteen ", "fifteen ", "sixteen ","seventeen ", "eighteen ", "nineteen "};
string tens[]  // Variable for storing the words for the numbers multiple of 10 till 90
   = { "", "", "twenty ","thirty ", "forty ","fifty ", "sixty" ,"seventy ", "eighty ","ninety " };
   
// Function for finding the words for numbers that are not stored in the above variables
string wordsForNum(int a, string str) {   
   string s = "";
   if (a > 19)  // when a number os >19
      s += tens[a / 10] + ones[a % 10];
   else
      s += ones[a];
   if (a)    // when number is non-zero
      s += str;
   return s;
}

// Function for converting the numbers to words and printing them
string convertingToWords(int a)   {   
   string result;
   result += wordsForNum((a / 10000000),"crore ");
   result += wordsForNum(((a / 100000) % 100),"lakh ");     // storing greater numbers
   result += wordsForNum(((a / 1000) % 100),"thousand ");
   result += wordsForNum(((a / 100) % 10),"hundred ");
   if (a > 100 && a % 100)
      result += "and ";
   result += wordsForNum((a % 100), "");
   return result;
}

// Function for sorting the numbers alphabetically with their words
void sortingNum(int arrNum[], int a)  {   
   vector<pair<string, int> > vecNum;
   
   // adding or inserting the words for the numbers in the vector variable
   for (int x = 0; x < a; x++) {
      vecNum.push_back(make_pair(convertingToWords(arrNum[x]), arrNum[x]));
   }
   sort(vecNum.begin(), vecNum.end());
   for (int x = 0; x < vecNum.size(); x++)
      cout << vecNum[x].second << " ";     
}

int main() {   
   int arrNum[] = {17, 34, 65, 12, 10};
   int a = sizeof(arrNum) / sizeof(arrNum[0]);
   sortingNum(arrNum, a);
   return 0;
}

Output

17 65 10 34 12 

Example 2

In this approach, we sort the array elements alphabetically after converting them to words through C++. Here, the output is words for numbers in sorted order. Use a map to store the words for numbers.

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;
map<int, string> numNames = {  
   {0, "zero"}, {1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5, "five"}, {6, "six"}, {7, "seven"}, {8, "eight"}, {9, "nine"},{10, "ten"}, {11, "eleven"}, {12, "twelve"}, {13, "thirteen"}, {14, "fourteen"},{15, "fifteen"}, {16, "sixteen"}, {17, "seventeen"}, {18, "eighteen"}, {19, "nineteen"},{20, "twenty"}, {30, "thirty"}, {40, "forty"}, {50, "fifty"}, {60, "sixty"},{70, "seventy"}, {80, "eighty"}, {90, "ninety"} 
};

//function for converting number to words
string numToWords(int n)  {  
   if (n < 20) 
      return numNames[n];
   else if (n < 100) {   
      if (n % 10 == 0)
         return numNames[n]; 
      else
         return numNames[n / 10 * 10] + " " + numNames[n % 10];
   }
   else if (n < 1000){   
      if (n % 100 == 0)
         return numNames[n / 100] + " hundred";
      else
         return numNames[n / 100] + " hundred and " + numToWords(n % 100);
   }
   else
      return "Number out of range (0-999)";
}

int main() {  
   vector<int> number = {13, 1, 6, 7};// Define the array with predefined values
   
   vector<string> numNamesArray;  // Convert numbers to their corresponding names
   for (int n : number)
      numNamesArray.push_back(numToWords(n));
   sort(numNamesArray.begin(), numNamesArray.end());     // Sort the names alphabetically 
   cout << "Sorted array in alphabetical order with words : \n;
   for (const string& words : numNamesArray){
      cout << words << endl;
   }
   return 0;
}

Output

Sorted array in alphabetical order with words : 
one
seven
six
thirteen

Conclusion

We have reached the end of this tutorial. In this tutorial, we sorted array elements based on the number names of the array elements. All array elements are positive numbers. We demonstrated the problem statement with examples to elaborate on the task's meaning.

C++ was used to implement two approaches to solve the task, using a map and a vector for storing the words for the array numbers. In one of the outputs, we printed the alphabetically sorted number names of the array.

Updated on: 03-Oct-2023

66 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements