I'm trying to solve this problem. The input file I used was the one on the site:

4
9 0123456789 oF8
Foo oF8 0123456789
13 0123456789abcdef 01
CODE O!CDE? A?JM!.

My abstract idea was:
Read the first line, loop the program that many times.
Read the next line. Load the data into three diffrent arrays.
Use the displace function with the first two arrays. Look here.
Convert the numbers to base 10.
Convert them to the output base.
Get there new values from the output characters, output them to new file.
Heres where I was before I put in the "tobase10" function:

#include <stdio.h>
#include <math.h>
#include <string.h>

void displace(int *output, char *string, char *chrset);

int main(int argc, char *argv[]) {
	char chrnum[256];
	char srclang[256];
	char outlang[256];
	int intnum[256];
	int size;
	int max;
	int count = 0;
	FILE *infile = fopen(argv[1], "r");

	if(infile == NULL) {
		printf("Error, Cannot open: %s\n", argv[1]);
		return 1; }

	fscanf(infile, "%d", &max);

	while(count != max) {
		fscanf(infile, "%s %s %s", chrnum, srclang, outlang);
		displace(intnum, chrnum, srclang);
 		count++; }

	fclose(infile);
	printf("\nPress any key to quit ...");
	getchar();
	return 0; }

void displace(int *output, char *string, char *chrset) {
		int count = 0;
		while(string[count] != 0) {
			output[count] = ((strchr(chrset, string[count])) - (int)chrset);
			count++; } }

To test it, put the following after the line "displace(intnum, chrnum, srclang);" :

int count1 = 0;
		printf("\nbase: %d\nsize: %d\n", strlen(srclang), strlen(chrnum));
		while(count1 != strlen(chrnum)) {
			printf("%d", intnum[count1]);
			count1++; }

So everything up to there seems to be working. Heres the program I made to test the "tobase10" function:

#include <stdio.h>
#include <math.h>
#include <string.h>

int tobase10(int *input, int size, int base);

int main() {
	int array[256] = {2, 0, 3, 4};
	int count = 0;
	int size;

	size = tobase10(array, 4, 6);
	while(count != size) {
		printf("%d", array[count]);
		count++; }

	printf("\nPress any key to continue ...\n");
	getchar();
	return 0;}

int tobase10(int *input, int size, int base) {
	int result = 0;
	int count = 0;
	int intsize;
	
	while((size+1) != 0) {
		result = (input[count] * pow(base, (size-1))) + result;
		count++;
		size--; }
	intsize = ((int)log10(result)+1);
	count = (intsize-1);
	while(count >= 0) {
		input[count] = result % 10;
		result = result / 10;
		count--; }
	return intsize; }

It also works perfectly. But what happens when I join the two togeather?
I can't think of any reson of why it wouldent work:

#include <stdio.h>
#include <math.h>
#include <string.h>

void displace(int *output, char *string, char *chrset);
int tobase10(int *input, int size, int base);


int main(int argc, char *argv[]) {
	char chrnum[256];
	char srclang[256];
	char outlang[256];
	int intnum[256];
	int size;
	int max;
	int count = 0;
	FILE *infile = fopen(argv[1], "r");

	if(infile == NULL) {
		printf("Error, Cannot open: %s\n", argv[1]);
		return 1; }

	fscanf(infile, "%d", &max);

	while(count != max) {
		fscanf(infile, "%s %s %s", chrnum, srclang, outlang);
		displace(intnum, chrnum, srclang);
		size = tobase10(intnum, strlen(chrnum), strlen(srclang));
		int count1 = 0;
		while(count1 != size) {
			printf("%d", intnum[count1]);
			count1++; }
		printf("\n");
 		count++; }

	fclose(infile);
	printf("\nPress any key to quit ...");
	getchar();
	return 0; }

void displace(int *output, char *string, char *chrset) {
		int count = 0;
		while(string[count] != 0) {
			output[count] = ((strchr(chrset, string[count])) - (int)chrset);
			count++; } }

int tobase10(int *input, int size, int base) {
	int result = 0;
	int count = 0;
	int intsize;
	
	while((size+1) != 0) {
		result = (input[count] * pow(base, (size-1))) + result;
		count++;
		size--; }
	intsize = ((int)log10(result)+1);
	count = (intsize-1);
	while(count >= 0) {
		input[count] = result % 10;
		result = result / 10;
		count--; }
	return intsize; }

It exploded in my face. whoa!
P.S. The code I used to output to the sceen is just for debugging. It's not ment to be good programing practece.

Recommended Answers

All 8 Replies

Your tobase10 function does not work properly, and it is overly complicated. You do not need log10 or pow. You can loop until result becomes 0 (instead of using log10). A special case here is when result starts out as zero. Instead of pow you can start a number at 1 and multiply it by the base every time through the loop.

And you really don't want to convert to base 10 anyway, so the last part of the function should go (and the name should be changed to, say, getValue). You really just want the numerical value, not a base 10 representation.

Also there's no reason to use "displace". You can deal with the digits one at a time in a loop.

Your tobase10 function does not work properly, and it is overly complicated. You do not need log10 or pow. You can loop until result becomes 0 (instead of using log10). A special case here is when result starts out as zero. Instead of pow you can start a number at 1 and multiply it by the base every time through the loop.

And you really don't want to convert to base 10 anyway, so the last part of the function should go (and the name should be changed to, say, getValue). You really just want the numerical value, not a base 10 representation.

Also there's no reason to use "displace". You can deal with the digits one at a time in a loop.

Did you see the link's? The displace function is one of the main keys of the program. It is absolulty neccesary. The pow function is needed for the algorithm. Google "converting bases" and click on the first link. The point of the log10 was to get the length, but not for what you think. The function returns the size, and edits the array. I thought my function was somewhat simple, first we convert the array into a variable. After that we put it back into the array, and return its size. I've tested the function using the second program in my post. It returns the right anwsers for the input file I provided at the top, and I havent seen it fail before. I need it to feed into an array, so I can use the next frombase10 function and to "un-displace" it.
Thanks for your reply

Are you talking about the algorithm from here (second post)?. Looks a little more complicated. Multiple loops would be needed to get the result, however it may be a little more faster.

I stand by my original post, every word of it. You're on your own. Good luck. (You'll need it!)

For future refrence, the displacement function is basicly a ceasors cypher. It has nothing to do with the base conversions! My tobase10 function seemed to work, but I checked it with a website whitch sometimes gives the wrong answers. I'll check for acuracy again latter, but still, the function in the demo does not return crazy values like it is in the main program. And the main program isnt giving any crazy values to the function (even if you give a letter that isnt in the alphabet, it shouldent do that).:-/

Ok I figured it out. The code:

(int)log10(n) + 1

is the problem. Sometimes it works and I have no idea why. But the function seems to edit the array fine. Now I just need to think of a better way to get the size (shouldent be a problem). Thanks nucleon.

Sorry for getting kind of mean there. I had just read another post by a guy complaining because I had asked for more information, so I was kind of pissed off. I'll be sure to count to 10 next time.

Sorry for getting kind of mean there. I had just read another post by a guy complaining because I had asked for more information, so I was kind of pissed off. I'll be sure to count to 10 next time.

No problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.