Thread: please help me design a function!

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    Unhappy please help me design a function!

    I am writing a program that converts arabic numbers to mayan numbers and vice versa. I got the converting arabic numbers to mayan down perfect but I am really struggling to write a function converting mayan numbers to arabic. incase you don't know arabic numbers are regular 1 2 3 numbers and mayan numbers are represented by 0 . and -
    the conversion table looks like this

    0 0 | 5 - | 10 -- | 15 ---
    1 . | 6 .- | 11 .-- | 16 .---
    2 .. | 7 ..- | 12 ..-- | 17 ..---
    3 … | 8 …- | 13 …-- | 18 …---
    4 …. | 9 ….- | 14 ….-- | 19 ….---

    mayan numbers are base 20 and written backward so to Covert Mayan to Arabic you go like this:
    Mayan = ..-- .. ….--- 0 (12 2 19 0)
    Arabic = 0 * 20^0 + 19 * 20^1 + 2 * 20^2 + 12* 20^3 = 97180

    I read in the Mayan number as a char array and now have to create a function that converts the mayan number and returns an arabic number. This is what I have written so far but I am afraid I went in the completely wrong direction and there is a much easier way to solve this than I am attempting to. Any help would be great, Thanks!
    Code:
    	int counter=0;
    	int final=0;
    	int temp[20];
    	int temp2[50];
    	for(int q=0; q < 50; q++)
    		temp2[q] = 0;
    	for(int j=0; j < 20; j++)
    		temp[j] = 0;
    	for(int t=0; t < 25; t++)
    	{
    		for(int p=0; p < 25; p++)
    		{
    		if(MayanNumber[t] != '#') //MayanNumber array is declared as all #'s after the number
    		{
    			if(MayanNumber[t] != ' ')
    			{
    			switch(MayanNumber[t])
    			{
    				case '0':
    					temp[counter] = 0;
    					counter++;
    					break;
    				case '.':
    					temp[counter] = 1;
    					counter++;
    					break;
    				case '-':
    					temp[counter] = 5;
    					counter++;
    					break;
    			}
    			}
    			else
    			{
    				for(int k=0; k < counter; k++)
    					temp2[p] = temp2[p] + temp[k];
    			}
    		}
    		}
    	}
             //I havent written the part that tallys up every number yet but it will be assigned to 'final'
    	return final;
    Last edited by rainman39393; 10-01-2008 at 07:44 PM.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    >>mayan numbers are base 20 and written backward so to Covert Mayan to Arabic you go like this:

    Sounds like a job for strrev() and strtol(). Just put 20 for the third argument of strtol for a base 20 conversion.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rainman39393 View Post
    mayan numbers are base 20 and written backward so to Covert Mayan to Arabic you go like this:
    Mayan = ..-- .. ….--- 0 (12 2 19 0)
    Arabic = 0 * 20^0 + 19 * 20^1 + 2 * 20^2 + 12* 20^3 = 97180
    How is this written backwards? That's how we would normally write in a positional base system. (Largest exponent on the left, just like in base ten).

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Either way, strtol() handles arbitrary bases (within limitation). So you should be fine just converting strings strtol() can digest to mayan format and back. I had to do that for a barcoding system that I wrote a while back. I used a base 25 system that converted to and fro.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM