So, a few days ago I decided to try Project Euler question #22. The question is:
Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.
What is the total of all the name scores in the file?
Now, I saved the file, and using the internets I found out that names.txt has 5163 names. Also, names.txt is formatted in the following manner: "John","George" ,"Abe" etc. All in one line.
According to that, I built the following program:
The code runs very fast, no compile errors. Only one problem - I'm getting the wrong result. What I'm getting:
996321502
I'm running Ubuntu. Compile: g++ -Wall 22.cpp
After a day of headaches, I decided to give up on it. Just now I thought why not ask here, because I really don't want my mistake to repeat itself.
So what's wrong? And how could I avoid this mistake in the future?
Thanks!
Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.
What is the total of all the name scores in the file?
Now, I saved the file, and using the internets I found out that names.txt has 5163 names. Also, names.txt is formatted in the following manner: "John","George" ,"Abe" etc. All in one line.
According to that, I built the following program:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void swap (int& x, int& y)
{
int holder = x;
x = y;
y = holder;
}
int main()
{
string namelist;
string temp;
int sum;
int counter = 0;
unsigned long long total = 0;
char alpha[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 values[5163];
ifstream names ("names.txt");
if (names.is_open())
getline (names, namelist);
names.close();
for (long i = 0; i < namelist.length(); i += 3) //i += 3 to skip the commas and quotes
{
temp.clear();
sum = 0;
while (namelist[i+1] != '"') //Loop character by character
{
temp += namelist[i+1]; //Build the string from individual characters
for (int k = 0; k < 26; k++) //Loop through alphabet, compare, save the sum
{
if (namelist[i+1] == alpha[k])
sum += k + 1;
}
i++;
}
values[counter] = sum; //Save the sum in the values array
counter++;
}
for (int i = 0; i < 5163; i++)
{
for (int j = i; j < 5163; j++)
{
if (values[j] < values[i]) //Sort numerically, not alphabetically, as we are looking for numerical values
swap(values[j], values[i]);
}
}
for (int i = 0; i < 5163; i++)
total += values[i] * (i + 1); //Calculate the total
cout << total << endl;
return 0;
}
996321502
I'm running Ubuntu. Compile: g++ -Wall 22.cpp
After a day of headaches, I decided to give up on it. Just now I thought why not ask here, because I really don't want my mistake to repeat itself.
So what's wrong? And how could I avoid this mistake in the future?
Thanks!
Comment