So I took your suggestions into consideration, it looks like changing the flow to process each input individually (taking out my thisarray = malloc statement) took care of large inputs from crashing the program originally. I went ahead and changed all of my sizes to take care of 1000001 (or 50001 for my shorter arrays for good measure) but it still is seg faulting somewhere.
I also removed the system(“PAUSE”) for good measure. Is the judge able to overlook this or will this cause problems in the future?
Thats really good news to find out that I can process inputs individually, that’s going to help out immensely with future projects. So here is my updated code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define REPS 100
char *Strrev(char *str);
int arrayincr(char *array, long initial_count);
void append(char *s, char c);
void even(char array[1000001], int arraylen);
void odd(char array[1000001], int arraylen);
int main() {
int total, i, even_or_odd;
//how many repetitions?
scanf("%i", &total);
size_t arraylen;
char *array = malloc(sizeof(char) * 10000001);
while (total + 1) {
for (i = 0; i < total; i++) {
scanf("%s", array);
arraylen = strlen(array);
arraylen = arrayincr(array, arraylen);
even_or_odd = arraylen % 2;
}
for (i = 0; i < total; i++) {
if (even_or_odd == 0) {
even(array, arraylen);
}
else if (even_or_odd == 1) {
odd(array, arraylen);
}
}
total--;
}
free(array);
}
void append(char *s, char c) {
int len = strlen(s);
s[len] = c;
s[len + 1] = '\0';
}
int arrayincr(char *array, long initial_count) {
long count = initial_count;
while (1) {
if (array[count - 1] == '9') {
array[count - 1] = '0';
count--;
}
else if (count == 0) {
memmove(array + 1, array, initial_count);
array[0] = '1';
count = ++initial_count;
array[initial_count] = '\0';
return count;
}
else {
array[count - 1]++;
return initial_count;
}
}
}
void even(char array[1000001], int arraylen) {
char revarray[500001], firsthalf[500001], *temparray = malloc(1000001);
arraylen /= 2;
strcpy(firsthalf, array);
firsthalf[arraylen] = '\0';
while (1) {
strcpy(revarray, firsthalf);
Strrev(revarray);
strcpy(temparray, firsthalf);
strcat(temparray, revarray);
if (strcmp(array, temparray) < 0) {
printf("%s\n", temparray);
free(temparray);
break;
}
else if (strcmp(array, temparray) > 0) {
arrayincr(firsthalf, arraylen);
}
else {
printf("%s\n", temparray);
free(temparray);
break;
}
}
}
void odd(char array[1000001], int arraylen) {
char revarray[500001], firsthalf[500001], *temparray = malloc(1000001), middle;
arraylen--;
arraylen /= 2;
middle = array[arraylen];
strcpy(firsthalf, array);
firsthalf[arraylen] = '\0';
while (1) {
strcpy(revarray, firsthalf);
Strrev(revarray);
strcpy(temparray, firsthalf);
append(temparray, middle);
strcat(temparray, revarray);
if (strcmp(array, temparray) < 0) {
printf("%s\n", temparray);
free(temparray);
break;
}
else if (strcmp(array, temparray) > 0) {
if (middle != '9') middle++;
else {
middle = '0';
arrayincr(firsthalf, arraylen);
}
}
else {
printf("%s\n", temparray);
free(temparray);
break;
}
}
}
char *Strrev(char *str)
{
char *p1, *p2;
if (!str || !*str)
return str;
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
return str;
}