Stuck!

Pages: 12
closed account (G854izwU)
I just still don't get it D= you guys are starting to confuse me. You each have about 5 different posts and your all saying the other is wrong so I don't know who is right!! Could one of you or each of you post one more post under this one trying to explain VERY simply what I would need to do to get started. Really what I want to know is how to store the number in the program, how to get the sets of 5 digits, and how to convert it to an int. Please try to explain clearly and simply I really need this. If you could explain all the different steps and parts and how each line of code works that would be great!!! Thank you all so much.

-TheNewKid-
My last one should work, in the beginning i got it wrong.
closed account (G854izwU)
It might work but I wont learn anything if I don't understand it! The whole point of me doing these exercises if for me to learn. It's not about getting the answer its about learning how to get the answer! So could you please explain. Thanks

-TheNewKid-
i'm sorry but i can't give huge replies as of now as i'm from mobile... And tomorrow i'm busy. I can explain you friday, unless someone else cares about explaining it
closed account (G854izwU)
Well if nobody else explains by friday I would be very thankful if you explained then!
Really what I want to know is how to store the number in the program, how to get the sets of 5 digits, and how to convert it to an int. Please try to explain clearly and simply I really need this.


If you can't figure out how to get a chunk of text into your code, you probably don't have much chance of solving the problem without a lot of guidance.

That said, copy+paste.

You could paste into a text file and open the file and read it in...

or you could paste it directly into your code, enclose each line in double quotes and you've got a nice long string.
Last edited on
closed account (G854izwU)
See this ^ This post helped none. I'm not so stupid that I don't know how to create a variable stupid! I just don't know how to handle and int that is 1000 digits long!!!!! Also that explained nothing all you did was re-state what I could do not HOW I could do it...
You aren't handling an int that is 1000 digits long. You're handling groups of 5 single digit numbers. Like I said, it's a chunk of text.

Really what I want to know is how to store the number in the program,


cire wrote:
or you could paste it directly into your code, enclose each line in double quotes and you've got a nice long string.


If you'd spend a little less time being offended and more time considering the problem, you'd realize I told you exactly how to store the number.

I didn't say you were stupid, although I'm now leaning towards "not particularly clever." I presumed you at least took a short glance at the code others provided and could see that they were storing the number as a string. And if you still had the question you did, my answer would seem to be the obvious one, at least to me.

In the interest of keeping this civil, I won't tell you where you could go or HOW you could get there. =P

Last edited on
NewKid, Go back and read, or better yet- go back and PRINT the examples and suggestions above. There are some very good solutions in there- including, and not at all limited to the post from LowestOne where he explains that you can use Five Arrays ( or one array of size 5 ints )

Take the problem one element at a time.


73167176531330624919225119674426574742355349194934

define an array to hold 5 ints

or just define 5 ints
Int first, second,third,fourth,fifth;

Or you could make a loop from 0-4

loop ( i=0;i<5;i++)
//get a value
dataValue= n
myProduct *= dataValue;
}

At this point you have looked at the first 5 values and created the first product- held in dataValue.

dataValue = 7 * 3 * 1 * 6 * 7
myProduct = 882

Save this first value
Store myProduct in come safe place, like int A


get the next product value, - ie do the loop again
to get a second product which will be



dataValue = 3 * 1 * 6 * 7 * 1
myProduct = 126

Store myProduct in come safe place, like int B

now write a piece of code to see which is larger, int A or int B ?

if A>B,
then myHomeworkAnswer = A,
else myHomeworkAnswer = B

Continue until you run out of input values.



Type this in, and run it.
Of course this is only Pseudo Code, but the beginning of any program is
to design it in pseudo code so that you know what to expect.
I use RATFOR, but anything is ok - so long as you lay out the steps first before you start to program.
Last edited on
closed account (G854izwU)
Thanks Incis B that helped a lot!

@cire I'm really sorry about earlier man I was just getting a bit frustrated at something that happened between me and one of my friends and I sorta took it out on you. I am really sorry but I did find this line of your reply quite humorous!
I didn't say you were stupid, although I'm now leaning towards "not particularly clever."


That made my night lol! Well thank you all I think I will print this out that way I can write on it and make notes. I will post back tomorrow with an answer.
So i found some time right now, and i'm going to explain you everything as if you was a 3-years old.
Sit down, so Daddy can tell you a story. /* Sarcasm /*
The correct code of mine is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char GiantNumber = "123392181217"; //Your Giant Number here
int main()
{
	unsigned int Length = strlen(GiantNumber); // String Length;
	unsigned int i = 0;
	while (i < (Length-5)) // While GiantNumber still has 5 Characters to be copied
	{
			int Product = 1; // Equals 1 so it can be multiplied
		for( unsigned int j = 0; j < 5; j++)
		{
			Product *= GiantNumber[j+i] - '0'; // Takes Integer Value of a Char, but be sure it is a valid '0'-'9' char.
		}
		// Do here your comparations
	}
}

Its comments are good enough for a middle-experienced programmer, but if you really need to go in-deep:
char GiantNumber = "123392181217"; //Your Giant Number here
Your giant number is stored as a string (a char *), and not as a number.
1
2
3
4
int main()
{
	unsigned int Length = strlen(GiantNumber);
	unsigned int i = 0;

The newly declared variable called Length will store the complete length of the string GiantNumber. strlen = StringLength.
The variable called 'i' will be our index. Based on I, we will go through the entire string. Let's say GiantNumber[i]. If i == 0, we will get the first character. If i == 1, we will get the second one. And so on.
 
while (i < (Length-5))

While you can get 5 characters, and still get valid results...
 
int Product = 1; // Equals 1 so it can be multiplied 

The variable Product will store the product of the next five numbers you are going to "catch". It is initialized to 1, because if we initialize it to 0, every number you are going to multiply by 0 is going to be 0.
1
2
3
4
for( unsigned int j = 0; j < 5; j++)
	{
		Product *= GiantNumber[j+i] - '0'; // Takes Integer Value of a Char, but be sure it is a valid '0'-'9' char.
	}

Here we get 5 numbers
( The code Product *= GiantNumber[j+i] - '0'; will be executed 5 times )
We calculate the resulting character and subtract the ASCII value of '0', which is 0x30, in hex, and then Product will multiply himself by that number.
If Product was initialized as 0, Every time Product will multiply himself, it will always become a 0.

What is left to you, is just to handle your Product variable each time-and calculate the higher Product.
closed account (G854izwU)
Hey thanks I think I get it now. I am going to start making the program to get my answer now. I will post the source code once I get it so you can see how I did it.
It's not me the one who needs it :d
closed account (G854izwU)
Haha I know that I just want you to check to see if I made the program ok.
closed account (G854izwU)
Hey again this is what I have so far.

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
//This code was made completely by -TheNewKid-  Please do not take credit for this

#include <iostream>
#include <string.h>


using namespace std;

int main ()
{
    char GiantNumber[] = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
    long long highestMultiplication = 0;

    int length = strlen(GiantNumber);
    int i = 0;

    while (i < length - 4) {
        int one = GiantNumber[i];
        int two = GiantNumber[i+1];
        int three = GiantNumber[i+2];
        int four = GiantNumber[i+3];
        int five = GiantNumber[i+4];

        long long answer = one * two * three * four * five;
        if (answer > highestMultiplication) {
            highestMultiplication = answer;
        }
        i++;
    }

    cout << highestMultiplication << endl;

    return 0;
}


But now I am stuck again. It gives me a number but Project Euler says its the wrong one. What am I doing wrong.
closed account (G854izwU)
OH! Wait don't I need to convert the char to an int?
closed account (G854izwU)
I fixed it =D Now it works great! Here is the final code!

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
//This code was made completely by -TheNewKid-  Please do not take credit for this

#include <iostream>
#include <string.h>


using namespace std;

int main ()
{
    char GiantNumber[] = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
    long long highestMultiplication = 0;

    int length = strlen(GiantNumber);
    int i = 0;

    while (i < length - 4) {
        int one = GiantNumber[i] - '0';
        int two = GiantNumber[i+1] - '0';
        int three = GiantNumber[i+2] - '0';
        int four = GiantNumber[i+3] - '0';
        int five = GiantNumber[i+4] - '0';

        long long answer = one * two * three * four * five;
        if (answer > highestMultiplication) {
            highestMultiplication = answer;
        }
        i++;
    }

    cout << highestMultiplication << endl;

    return 0;
}



Thanks for everything!

-TheNewKid-
Yes. You can do it by subtracting the ASCII value of the '0', but you must be sure those are numbers and not characters.
My method was: Number = Character - '0';
closed account (G854izwU)
Haha I figured it out before you even said it!

Also check your PM's Ess
Last edited on
Topic archived. No new replies allowed.
Pages: 12