optimize problem 22

Pages: 12
This version of your code works, see arrows to lines I changed.
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <fstream>
#include <string>

bool compare(const char * name1, const char * name2);
int position(const char * name);
int value(const char * name);
int main()
{
	using namespace std;
	ifstream inFile;
	inFile.open("names.txt");
	int total_character_value = 0;
	char *name;
	bool x = 1;
	char temp[30];
	while(inFile.good())//<---------------------
	{
		int i = 0;
		char ch;
		while(inFile.get(ch) && ch != '"')
		{
			if(ch == ';')
				x = 0;
		}
		if(ch == '"')
		{
			inFile.get(ch);
			while(ch != '"')
			{
				temp[i] = ch;
				i++;
				inFile.get(ch);
			}
			temp[i] = '\0';
		}
		name = temp;
		int pos = position(name);
		total_character_value += (value(name) * (pos - 1));//<------------------
	}
	cout << "total = " << total_character_value;
	cin.get();
	return 0;
}

bool compare(const char * name1, const char * name2)
{
	int i = 0;
	while(name1[i] != '\0' && name2[i] != '\0')
	{
		if(int(name1[i]) > int(name2[i]))
			return 1;
		if(int(name1[i]) < int(name2[i]))
			return 0;
		else
			i++;
		if(name2[i] == '\0')
			return 1;
	}
	return 0;
}
int position(const char * name)
{
	std::ifstream inFile;
	inFile.open("names.txt");
	int pos = 1;
	char *str;
	char ch;
	char n[20];
	while(inFile.good())//<-----------------------------
	{
		while(inFile.get(ch) && ch != '"')
		{
			if(ch == ';')
				return pos;
		}
		if(ch == '"')
		{
			int i = 0;
			inFile.get(ch);
			
			while(ch != '"')
			{
				n[i] = ch;
				i++;
				inFile.get(ch);
			}
			n[i] = '\0';
			str = n;
			if(compare(name, str))
			{
				pos++;
			}
		}
	}
	return pos;
}

int value(const char * name)
{
	int i = 0;
	int tot = 0;
	while(name[i])
	{
		tot = tot + (int(name[i]) - 64);
		i++;
	}
	return tot;
}


You really should have scraped this code and started over as your method is very slow and inefficient. We all have times when we have to just start over fresh.
Last edited on
Didn't give me the right answer. Did you try the code?
Last edited on
try changing your compare function to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int compare(const char * name1, const char * name2)
{
	int i = 0;
	while(name1[i] != '\0' && name2[i] != '\0')
	{
		if(int(name1[i]) > int(name2[i]))
			return 1;
		if(int(name1[i]) < int(name2[i]))
			return -1;
		else
			i++;
	}
	if (name1[i] == '\0' && name2[i] == '\0')   return 0;
        if (name1[i] == '\0')    return -1;
        if (name2[i] == '\0')    return 1;
}
Last edited on
Yes I did, and yes it does, 871198282.

ok, something weird with the file maybe... but thanks anyway, delete the answer and program so that other people can't cheat off it.

Thank you very much, the problem may have been something about the file for quite some time. Hope you continue to help when project euler frustrates me :P
Last edited on
Topic archived. No new replies allowed.
Pages: 12