Hello everyone! First post, and pretty new to C++, so I apologize for any blatantly dumb questions that I may ask. Just trying to learn!

I'm working on a Project Euler problem that has me looking for the largest prime factor of a number. (Specifically, a very large number.) My main programming background is in MATLAB, and I have 3 different versions of code to solve the problem there, but MATLAB is just incapable of handling the value I am trying to evaluate. (~600 billion.) Hence, here's me, trying to learn C++, hoping that it will be efficient enough to handle the problem set forth, as well as furthering my goal of learning C++! hahaha :)

My question is regarding static vs. dynamic memory allocation for arrays. My code is running as expected, but before it was, I was getting some strange errors, and it brought up something I was confused about. At this point, I'm shying away from dynamic arrays just because they seem to be a hair too complicated for my noobishness. I'm trying to get my foundation set in C++ first, then I'll start to tackle some more of the finer points of the language.

In my code, I declare an array "factors" with "halfVal" cells. "halfVal" is defined beforehand, and I don't need it to change later on, so static should be fine, but I'm not sure how to declare it statically by using the value of halfVal. Is it possible to use the value of a variable, in this case halfVal, to statically define the number of cells in an array? If so, how would I do that?

Also, another question I have is if I try to access a value in the factors array that is declared, but not redefined in the first for-if loop, then I get undefined behavior when I try to print those values. For example, the factors of 30 are 1, 2, 3, 5, 6, 10, 15, 30. That is 8 total values. If halfVal is set to 16, (and therefore the declaration of the factors array should be 16 total cells) and then I try to print factors[0] through factors[15], after I pass the first 8 cells that have been redefined in the first for-if loop, I get undefined behavior. My output gives me something like:Code:`#include <iostream>`

#include <cmath>

using namespace std;

int main (int argc, const char * argv[])

{

//Declare variables. Initialization may or may not be necessary?

int halfVal = 1, counter = 0;

double value = 0, quotient = 0;

//Decalre value to find the largest prime value of

value = 30; //Goal Value is 600851475143

//Calculate halfVal for efficiency of following for loop

halfVal = (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.

int factors[halfVal];

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

for( int num = 2; num < halfVal ; num++ ){

quotient = (value / num);

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

if( quotient == ceil( quotient ) ) {

factors [counter] = num;

counter = counter + 1;

}

}

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

//If counter is replaced with halfVal, then I get undefined behavior. Something to do with dynamic array?

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

}

}

Factors: 2

Factors: 3

Factors: 5

Factors: 6

Factors: 10

Factors: 15

Factors: 1510532867

Factors: 5937525205

etc.

etc.

(The last several values are obviously the undefined behavior.)

My hunch is that since I have a dynamically allocated array, the values aren't initially defined, so when I try to access them, I get undefined behavior. Is it possible to initialize those values when the array is declared to avoid this type of problem (still keeping it a dynamic array)? If so, how does one go about that. If not, any tips on where I can start to look for a function to clear out the unused cells? No explanation needed, just a point in the right direction for that one. :) I think figuring this out will help me in starting to understand dynamic allocation more. Any insight on this issue would be wonderful!

Thanks so much, and sorry for the long post!