There is an array of integers, and I want to turn them into an array of separated digits and print them back out as the same sets of integers.
ps: reason being, I want to do lots of individual digit calculations, and I want to use this method to separate the digits, and store their "separating indexes" to turn them back to different integers afterward.
However, there is a problem, almost all the digits are translated correctly but some of them at each individual ends are wrong.
CODE:
#include <iostream>
using std::cout; using std::endl;
int* charArrToIntArray(char** input, int* sizeofnumbers, int sizeofarray) {
int* output = new int[100];
cout << input[0][0] << endl;
cout << sizeofnumbers[0] << endl;
cout << sizeofarray << endl;
for (int counter = 0; counter < sizeofarray; counter++) {
int i;
sscanf_s(input[counter], "%d", &i);
output[counter] = i;
}
return output;
}
int* intToIntArray(int input, int length) {
static int output[20];
for (int i = 0; i < length; i++) {
output[i] = input % 10;
input /= 10;
}
return output;
}
int main()
{
const int arraySize = 5;
int NumSize[arraySize] = { 6, 7, 5, 9, 8 };
int NumSizeFlatten[arraySize + 1] = { 0 };
static int IntArray[arraySize] = {345678, 6543210, 11101, 110010001, 00011100};
static int arr2D[500]; //The flat 2D array...
for (int counter = 0; counter < arraySize; counter++) {
int* arr1D = intToIntArray(IntArray[counter], NumSize[counter]);
for (int i = 0; i <= counter; i++) {
NumSizeFlatten[counter + 1] += NumSize[i]; //Get the values of positions of flattened array's separation points
}
for (int i = NumSize[counter] - 1; i >= 0; i--) {
printf("%d", arr1D[i]); //Directly Print from the arrays
}printf("\n");
for (int i = 0; i < NumSize[counter]; i++) {
arr2D[NumSizeFlatten[counter] + i - 1] = arr1D[NumSize[counter] - i]; //PUT arrays into a Flattened array //warning C6386: buffer overrun
}
}
for (int i = 0; i < 50; i++) {
printf("%d", arr2D[i]); //Directly print the Flattened array in a flattened from.
}
for (int j = 1; j <= arraySize; j++) {
printf("NumSizeFlatten[%d] = %d\n", j, NumSizeFlatten[j]); //Print the values of positions of flattened array's separation points
}
for (int i = 0; i < arraySize; i++) {
for (int j = NumSizeFlatten[i]; j < NumSizeFlatten[i + 1]; j++) {
//printf("%d.", j);
printf("%d", arr2D[j]); //Print From the Flattened Array
}printf("\n");
}
}
OUTPUT:
345678
6543210
11101
110010001
00004672
34567065432151110011001000100004670000000000000000NumSizeFlatten[1] = 6
NumSizeFlatten[2] = 13
NumSizeFlatten[3] = 18
NumSizeFlatten[4] = 27
NumSizeFlatten[5] = 35
345670
6543215
11100
110010001
00004670
and also the last integer is wrong.
I know this code is really trash, I am doing what I can...