So I finally finished up my first "real" C++ program which was trying to solve Problem 3 from Project Euler. It runs as expected except for when large values are entered. If I enter a value such as 1234567, I get a Segmentation Fault: 11. My guess is it has something to do with an overflow of memory or something, but I have no idea how to track it down, and the tutorial here was well over my head. Being very new to compiled languages, I am still learning all of the terminology and nuances as compared to my first language, MATLAB. Any help on the segmentation fault, as well as general comments about style or other tips you may have for a new C++ programmer would be greatly appreciated! If you do make any comments, please try to keep them "in English" for me. :) I plan to get there with the terminology, but it's a lot to learn.

Also, if any efficiency problems are pointed out, do me a favor and just address the loop or segment of code that can be optimized. I like the challenge of figuring out the optimization, and I know this code is not even close to fully optimized, just not sure where to start.

Thanks in advance!

Code:`//============================================================================`

// Name : Problem_03.cpp

// Author : Stefano Prezioso

// Version :

// Copyright :

// Description : Project Euler Problem 3

//============================================================================

#include <iostream>

#include <math.h>

#include <cmath>

using namespace std;

int main() {

//Declare variables

int counter = 0;

double value, quotient = 0;

//Declare value to find the largest prime value of

//Goal Value is 600851475143

cout<< "Enter a value: "; cin>>value;

cin.ignore();

//Calculate iVal for efficiency of following for loops

int iVal = value/2 +1;

//Declare array. Can I make this a static array while still using the value of halfVal somehow? I don't need a dynamic allocation of memory.

double factors[iVal];

//Determine the quotient of the value divided by num (from 2 to iVal) for logical test

for (double num = 1; num < iVal; num = num + 2) {

quotient = (value / num);

//Print equation to calculate quotient in each iteration

//cout << value << " / " << num << " = " << quotient << "\n";

//Logical test to determine whether or not num is a factor. If so, store in factors array and update counter

if (quotient == floor(quotient)) {

factors[counter] = num;

counter = counter + 1;

}

}

// //Print Factors of Value

// for (int i = 0; i < counter; i++) {

//

// cout << "Factor: " << factors[i] <<"\n";

// }

int numFactors = counter;

//printf("Counter: %i\n", counter);

double subValues[iVal];

counter = 0;

quotient = 0;

double primeFactors[iVal];

double i;

int j;

//Determine which factor to analyze

for (int position = 0; position < numFactors; position++) {

//printf("Factor being tested: %.0f\n", factors[position]);

//Builds array subValues with possible factors of factor

for (i = 1, j = 0; i < (factors[position] + 1); i++, j++) {

subValues[j] = i;

//printf("Subvalue: %.0f\nj: %i\n", subValues[j], j);

}

int flag = 0;

//Divide current factor n by all values from 2 to n-1 to determine primeness

for (int k = 1; k < factors[position] - 1; k++) {

//printf("Evaluated Factor: %.0f\n", factors[position]);

double currentVal = factors[position];

quotient = currentVal / subValues[k];

//printf("k Value: %i\nsubValues[k] Value: %f\nQuotient: %.2f\n", k, subValues[k], quotient);

double roundedQuotient = ceil(quotient);

//Flag non-prime numbers

if (quotient == roundedQuotient) {

flag = 1;

}

}

//Store prime factors in array

if (flag == 0) {

// printf("Factors[position]: %.0f\n", factors[position]);

// printf("counter: %i\n", counter);

primeFactors[counter] = factors[position];

counter = counter + 1;

}

}

printf("Value is: %.0f\n", value);

// printf("Number of factors: %i\n", numFactors);

printf("Number of Prime Factors: %i\n", counter);

printf("Largest prime factor is: %.0f\n", primeFactors[counter - 1]);

cout<< "Press enter to end program.";

cin.get();

}