
Patrick B. answered 05/05/21
Math and computer tutor/teacher
This is challenging to do WITH loops, let alone without...
You must replace every loop with a recursive function.
for example, if array A contains {1,2,3,4,5,6,7,8,9,10}, then to print it forwards, you have to
do something like this:
using namespace std;
#include <iostream>
Show(int * A, int iIndexPos)
{
if (iIndexPos>0)
{
Show(A,iIndexPos-1);
}
cout << A[iIndexPos] << endl;
}
int main()
{
int a[]={1,2,3,4,5,6,7,8,9,10};
int n=10;
Show(a,n-1);
}
Here's some code that does the base conversions
/**************************/
using namespace std;
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void GetDecimalNum(int baseFrom, char * numStr, long * runningTotal, int iIndexPos, int maxIndexPos)
{
cout << " iIndexPos =" <<iIndexPos << " : maxIndexPos = " << maxIndexPos << endl;
if (iIndexPos<maxIndexPos)
{
GetDecimalNum(baseFrom,numStr,runningTotal,iIndexPos+1,maxIndexPos);
}
if (iIndexPos<maxIndexPos)
{
int iDigit;
if (baseFrom==16)
{
if (numStr[iIndexPos]>='A' && numStr[iIndexPos]<='F')
{
iDigit = numStr[iIndexPos]-55;
}
else
{
iDigit=numStr[iIndexPos]-48;
}
}
else
{
iDigit = numStr[iIndexPos]-48;
}
*runningTotal = *runningTotal + iDigit * pow(baseFrom,maxIndexPos-iIndexPos-1);
cout << "iDigit is " << iDigit << " : running total is " << *runningTotal << endl;
}
}
void GetMaxExponent(int baseTo, long longIntNum, int * maxExpo )
{
if ((int)pow(baseTo,*maxExpo) < longIntNum)
{
*maxExpo = *maxExpo+1;
GetMaxExponent(baseTo,longIntNum,maxExpo);
}
}
void _convert(int baseTo, long longIntNum, int maxExpo, int curExpo, char * numStr )
{
int iNumDigit;
if (curExpo>0)
{
int denominator = pow(baseTo,curExpo);
iNumDigit = longIntNum/denominator;
if (baseTo==16)
{
if (iNumDigit>=10 && iNumDigit<=15)
{
numStr[maxExpo-curExpo]= char(iNumDigit+55);
}
else
{
numStr[maxExpo-curExpo]= char(iNumDigit+48);
}
}
else
{
numStr[maxExpo-curExpo]=char(iNumDigit+48);
}
longIntNum%=denominator;
_convert(baseTo,longIntNum,maxExpo,curExpo-1,numStr);
}
else
{
if (baseTo==16)
{
if (longIntNum>=10 && longIntNum<=15)
{
numStr[maxExpo-curExpo]= char(longIntNum+55);
}
else
{
numStr[maxExpo-curExpo]= char(longIntNum+48);
}
}
else
{
numStr[maxExpo-curExpo]=char(longIntNum+48);
}
}
}
char * Convert(char * numStr, int baseTo, int baseFrom)
{
char * numStrReturn;
long longIntNum=0;
int strLen = strlen(numStr);
GetDecimalNum(baseFrom,numStr,&longIntNum,0,strLen);
int maxExpo = 0;
GetMaxExponent(baseTo,longIntNum,&maxExpo);
int n = maxExpo+2;
numStrReturn = (char*)malloc(n);
memset(numStrReturn,0,n);
_convert(baseTo,longIntNum,maxExpo,maxExpo,numStrReturn);
return(numStrReturn);
}
char * DoConversion(char * numStrInput, int baseTo, int baseFrom)
{
char * numStrResult = Convert(numStrInput,baseTo,baseFrom);
return(numStrResult);
}
Go ( int baseFrom, int baseTo, int n, char * argv[],int iIndexPos)
{
cout << "Go : iIndexPos = " <<iIndexPos << endl;
if (iIndexPos>3)
{
Go(baseFrom,baseTo,n,argv,iIndexPos-1);
}
char * numStrResult= DoConversion(argv[iIndexPos],baseTo,baseFrom);
cout << numStrResult;
if (numStrResult!=NULL) { free(numStrResult); }
}
int main(int argc, char * argv[])
{
if (argc>=4)
{
int baseFrom = atoi(argv[1]);
int baseTo = atoi(argv[2]);
printf(" command line args: baseFrom = %d : baseTo = %d \n",baseFrom,baseTo);
Go(baseFrom,baseTo,argc,argv,argc);
}
else
{
//seek input
}
}