Let's say I have a function that takes in an integer as an argument and returns an array that has all the digits of the integer separated as a 1D array (pseudocode):
int separatingInt (int num)
{
int length = amount of digits in num
int *arr = new int[length]
for ( int i = length; i > length; i-- )
{
arr[i] = num%10
num = num /10
}
return *arr
}
int main()
{
ifstream file
file.open("liczby.txt")
int number
file >> number
int* arrayFromNumber = seperatingInt(int num)
}
How do I declare an array from seperatingInt() in main() (referring to the last line in the pseduocode)? Is it better to just separate the digits into an array in seperate?
I was not even sure how to formulate my question properly, so I couldn't find answers myself.
I guess you could do it with a loop in the main() function, but since it's reading numbers from a file, and they all have different amounts of digits, it would look messy. Maybe messy is the way to go, I don't know.
return *arrshould bereturn arr. And change the function type toint *.std::vector, not C-style arrays.std::arrayfor arrays where the size is fixed at compile time orstd::vectorfor an array with dynamic size.std::vector<int>which is an array like object (actually much more powerful than an array) which can be returned from a function.arr[i] = num%10,iis out of bounds. Not that it matters, since yourforloop is wrong to begin with, asi > lengthwill never be true.