You are on page 1of 2

/*

* 42 - Coded Triangle Numbers.cpp


*
* Author: tempo
*/
#include <iostream>
#include <fstream>
#include <string>

char alphabet[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
int numbers[26] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26};

void triangleNumbs(float triangle[]){


for (int i = 1; i <= 100; i++){
float a = i;
triangle[i] = (a/2)*(a+1);
}
}

int score(std::string name){


int sum = 0, length = name.length();
for (int i = 0; i < length; i++)
for (int j = 0; j < 26; j++)
if (name[i] == alphabet[j])
sum += numbers[j];
return sum;
}

bool check (std::string word, float tri[]){


bool status = false;

int test = score(word);


for (int i = 1; i <= 100; i++)
if (test == tri[i])
status = true;

return status;
}

int main(){
std::fstream document;

document.open("C:\\Users\\tempo\\Desktop\\Programming\\Misc\\p042_words.txt");
std::string words[5000];
char end = '#', singleChar;
int count = 0;
while (singleChar != end){
document.get(singleChar);

for (int i = 0; i < 26; i++)


if (singleChar == alphabet[i])
words[count] += singleChar;

if (singleChar == ',')
count++;
}
// Count indexed properly

float triangle[100];
triangleNumbs(triangle);

int triCount = 0;
// for (int i = 1; i < 10; i++)
// std::cout << triangle[i] << std::endl;
for (int i = 0; i <= count; i++)
if (check(words[i], triangle) == true)
triCount++;

std::cout << triCount;

You might also like