in my function OpenData, it corrupts error on memory allocation when I compile and run it.
in main, i need to creat an int array and pass it to other function lator.. I take 4 command-line arguments ie, a.out 7 100 lychrels.dat
7 is seed number
100 is number of turn.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stringtoll.h"
#define TRUE 1
#define FALSE 0
#define SIZE 20
void OpenData(int ** arrayPtr, char file, int *arraySizePtr );
int CheckLych(int array[], int n, int count);
void ReverseAndAdd(long long num, int* timesPtr);
int IsPalindrome (char *string, int left, int right);
void LongLongToString(long long num, char *stringPtr);
void PrintGreeting();
int main(int argc, char*argv[])
{
int *array, randomNb, arraySize;
int i, judge, iteration = 0;
if (argc !=4)
{
fprintf (stderr, "This program requires following command");
fprintf (stderr, " line arguments\n");
fprintf (stderr, "'seed number', number of 'values' to be tested, ");
fprintf (stderr, "and a 'file name'.\n");
exit(-1);
}
OpenData(&array, argv[3], &arraySize);
int numFiles, seed;
numFiles = atoi(argv[2]);
seed = atoi(argv[1]);
for (i=0; i <numFiles; i++)
{
srand(seed);
randomNb = rand()%2001;
judge = CheckLych(array, randomNb, arraySize);
printf (" NUM = %d\n", randomNb);
if (judge == TRUE)
{
fprintf(stderr, "This problem couldn't be solved: %d is a lychrel.\n", randomNb);
}
else
{
ReverseAndAdd(randomNb, &iteration);
}
}
return 0;
}
void OpenData(int ** arrayPtr, char file, int *arraySizePtr )
{
FILE *ifp;
int n;
int i;
ifp = fopen(file, "r");
if(ifp == NULL)
{
fprintf(stderr, "can't open %s ... ...\n", file);
exit(-2);
}
while(fscanf(ifp, "%d", &n) != EOF)
{
*arraySizePtr++;
}
rewind(ifp);
*arrayPtr = (int *) malloc (*arraySizePtr * sizeof(int));
if (*arrayPtr == NULL)
{
fprintf(stderr, "Memory allocation failed... ...\n");
exit (-3);
}
for (i=0; i<*arraySizePtr; i++)
{
fscanf(ifp, "%d", *arrayPtr[i]);
}
fclose(ifp);
}
void LongLongToString(long long num, char *stringPtr)
{
int counter = 0, i;
int copyNum;
copyNum = num;
while(num !=0)
{
num = num / 10;
counter ++;
}
for(i = counter; i>=1; i--)
{
stringPtr[i-1] = (copyNum % 10)+ '0';
copyNum = copyNum / 10;
}
stringPtr[counter] = '\0';
}
void ReverseAndAdd(long long num, int* timesPtr)
{
char reverseString[SIZE];
char string[SIZE];
long long copyNum;
int i= 0;
long long reverseNum, total;
int length;
int boolean;
int iteration;
iteration = *timesPtr;
LongLongToString(num, string);
length = strlen(string);
boolean = IsPalindrome(string, 0, length);
if (boolean == TRUE)
{
printf("This problem is solved in %d iteration(s).\n\n", iteration);
}
else if (boolean == FALSE)
{
copyNum = num;
while(num !=0)
{
reverseString[i] = (num %10)+'0';
num = num / 10;
i++;
}
reverseNum = StringToLongLong(reverseString);
total = num + reverseNum;
printf("%lld + %lld = %lld\n", num, reverseNum, total);
*timesPtr ++;
ReverseAndAdd(total, &iteration);
}
}
int IsPalindrome (char* string, int left, int right)
{
if (string == string)
{
return IsPalindrome(string, left+1, right-1);
}
if (string != string)
{
return FALSE;
}
if (left >= right)
{
return TRUE;
}
}
int CheckLych(int array[], int n, int count)
{
int counter = 0;
int i;
for (i=0; i<count; i++)
{
if (n == array[i])
{
counter = TRUE;
}
}
return counter;
}