Skip to content

Instantly share code, notes, and snippets.

@AhmedMourad0
Last active December 23, 2017 20:53
Show Gist options
  • Save AhmedMourad0/89a72347d4ce3678975427d714a14531 to your computer and use it in GitHub Desktop.
Save AhmedMourad0/89a72347d4ce3678975427d714a14531 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/PracticalExam.iml" filepath="$PROJECT_DIR$/.idea/PracticalExam.iml" />
</modules>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />
cmake_minimum_required(VERSION 3.8)
project(PracticalExam)
set(CMAKE_CXX_STANDARD 17)
set(SOURCE_FILES main.cpp)
add_executable(PracticalExam ${SOURCE_FILES})
#include <string> // string
#include <iostream> // cin, cout
using namespace std;
int number_char(char *);
char* map_number(int);
void printOutput(int, int[10], string[10][100]);
void processData(int, int *, string[10][100], int[10]);
int main() {
cout << "Enter the number of words:\n";
int numberOfWords;
cin >> numberOfWords;
cout << "Enter the words:\n";
// can only have 100 words at max with 10 letters as a maximum length
string words[10][100];
// because we only want to print until we reach the last length with words
int largestLength = 0;
// the count of words for each length, also the last index we used in their arrays
int wordsCount[10];
// receive and classify the words
processData(numberOfWords, &largestLength, words, wordsCount);
// print our output
printOutput(largestLength, wordsCount, words);
return 0;
}
/**
* This is where the logic of our app goes
* @param numberOfWords the number of words which is entered by the used
* @param largestLength length of longest word
* @param words the words we received
* @param wordsCount count of words for each category
*/
void processData(int numberOfWords, int* largestLength, string words[10][100], int wordsCount[10]) {
string word;
for (int i = 0; i < numberOfWords; ++i) {
cin >> word;
// the length of the word
int length = number_char(&word[0]);
// looking for the largest length of all
if (length > *largestLength)
*largestLength = length;
// we subtract one because we count from zero
words[length - 1][wordsCount[length - 1]] = word;
// increase the count of words (AKA last filled index) for this length
++wordsCount[length - 1];
}
}
/**
* print our results
* @param largestLength length of longest word
* @param wordsCount count of words for each category
* @param words the words we received
*/
void printOutput(int largestLength, int wordsCount[10], string words[10][100]) {
for (int i = 0; i < largestLength; ++i) {
cout << map_number(i + 1) << "-letter words\t" << wordsCount[i];
for (int j = 0; j < wordsCount[i]; ++j)
cout << "\t" << words[i][j];
cout << endl;
}
}
/**
* calculates the length of a given string
* @param s the string to get the length of
* @return the length of the string
*/
int number_char(char* s) {
int length = 0;
// we know a string is finished in memory when we encounter the null terminator
for (; *s != '\0'; ++s)
++length;
return length;
}
/**
* gets the literal representation of a given number
* @param number the number to convert
* @return the literal representation of the number
*/
char* map_number(int number) {
// cast the number to a char* and return it
switch (number) {
case 1:
return const_cast<char *>("one");
case 2:
return const_cast<char *>("two");
case 3:
return const_cast<char *>("three");
case 4:
return const_cast<char *>("four");
case 5:
return const_cast<char *>("five");
case 6:
return const_cast<char *>("six");
case 7:
return const_cast<char *>("seven");
case 8:
return const_cast<char *>("eight");
case 9:
return const_cast<char *>("nine");
case 10:
return const_cast<char *>("ten");
default:
return const_cast<char *>("");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment