Introduction:
Before I get started, this is an advance warning that I've never coded in C before. I've taken 2 years of java-related programming, but C is a brand new animal for me. This is for a class I'm currently taking, so I'd appreciate knowing the more basic ways of solving these problems rather than using new files or advanced work-around. That's how we learn, right?
Relavent information:
- I am using C-Free 5.0, and coding by C99 standards.
- There is one file read, which contains:
Code:
12 196 295 394 493 592 689 788 790 879 877 978 986
It is located at the root directory of the .c file. - I will update the provided code of my file/provided list as they change, and will ask a new question each time I do so until it's working. Please check the most recent questions, as previous ones may not apply to the new code.
The Program:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
int i;//Counter
/**
* Author Comments
* Variables are defined throughout various functions or bracketed sections when they apply
* only to that section.
*
*/
/**
* Function to reverse the order of a provided number.
* Returns reversed number
*/
int reverseOrder(int number1)
{
int reversedNumber;
char forwardsBuffer[32];
int length;
char backwardsBuffer[32];
int counter1 = 0;//Counter for backwardsBuffer
itoa(number1,forwardsBuffer,10);
for (i=strlen(forwardsBuffer)-1; i--; i <=0)
{
backwardsBuffer[counter1] = forwardsBuffer[i];
counter1++;
}
itoa(reversedNumber,backwardsBuffer,10);
return reversedNumber;
}
/**
* Checks a number to see if it is a palendrome. Returns bool.
*/
bool checkPalendrome(int number2)
{
int first, last;
bool returnValue = false;//One digit palendrome
if (reverseOrder(number2) == number2)//3+ digit palendrome
{
returnValue = true;
}
return returnValue;
}
/**
* Handles palendrome/reverse addition and itteration count
*/
int palendromeAddition(int number3)
{
int itterationCounter = 0;
int reversedNumber;
int summation;
while (checkPalendrome(number3) != true)
{
reversedNumber = reverseOrder(number3);
summation = number3 + reversedNumber;
printf("%19d + %19d = %20d\n", number3, reversedNumber, summation);
number3 = summation; // Repeats with new sum.
itterationCounter++;
}
printf("A palendrome was obtained in %d itterations.\n\n", itterationCounter);
}
int main()
{
int sizeOfLychrelList;
char fileName[256];
char nextLychrelChar[16];
char sizeOfLychrelListChar[16];
int n = 1; //Initial number to work with. Initializes as a nonzero.
printf("Please input the candidate lychrel filename: ");
gets(fileName);
FILE *lychrelFile;
if ((lychrelFile=fopen(fileName, "r")) == NULL) { //opens the file
printf ("Error opening file\n");
exit (-1);
}
fgets(sizeOfLychrelListChar, 10, lychrelFile);//Gets the first number of the file and stores it as the number of lychrels
sizeOfLychrelList = atoi(sizeOfLychrelListChar);
int lychrels[sizeOfLychrelList];//Array that stores the lychrels
/**
* Generates an int list of all lychrels found within the file.
*
*/
for(i=0; i++; i<sizeOfLychrelList)
{
fgets(nextLychrelChar,20,lychrelFile);
lychrels[i] = atoi(nextLychrelChar);
}
i = 0;//Resets i for reuse later
printf("Please input the value of N: ");
scanf("%d", n);
while(n != 0)// Entry of 0 will quit the program
{
while (n > lychrels[i] && n != lychrels[i] && i < sizeOfLychrelList)//Ensures that n is not a candidate lychrel
{
i++;
}
if (n == lychrels[i])//Checks if n is a Lychrel Candidate
{
printf("%d is a Candidate Lychrel.\n", n);
}
else if (i > sizeOfLychrelList)//Ensures that n is less than the maximum Lychrel Candidate supplied; otherwise n could be an unlisted lychrel and cause a crash.
{
printf("Supplied lychrel list limits exceeded, cannot process with certainty");
}
else if (checkPalendrome(n))
{
printf("%d is a palendrome.", n);
}
else
{
palendromeAddition(n);
}
printf("Please input the value of N: ");
scanf("%d", n);
i = 0;
}
printf ("\nGoodbye\n");
exit (-1);
}
Problems:
Problem 1: Syntactically my program works, but it likes to break at line 63:
Code:
summation = number3 + reversedNumber;
The error message is "Program stopped at 0x7539ecc0. It stopped with signal SIGSEGV, segmentation fault." Initially I had some run-ins with "segmentation faults" when setting up the reading process for the attached test file. I assume it has something to do with this?