Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Number Bases Implementation Implement a program in C to convert integers between

ID: 3746790 • Letter: N

Question

Number Bases Implementation Implement a program in C to convert integers between any two arbitrary bases, and add 2 numbers of the same arbitrary base. Support any base between 2 and 36, using the digit symbols 0123456789ABCDEFGHIJKLMNOPQRSTUVWxYZ Humans typically enter numbers into machines as strings of characters representing a value in some positional number system. Common bases are 2, 10, and 16. In theory, any base could be used, and these unusual bases do occur occasionally in computer science and in Klingon. Kaplal More generally, many strings, other symbol sequences, and also more abstract sequences, can be interpreted as positional numbers. One example is the 3-letter sequences in DNA that code for particular amino acids. Perhaps the inhabitants of some distant planet 'count in colors Requirements: 1) Convert a string representation of a positional number in some arbitrary base to intemal memory (binary) representation (int or char. Recall that chars are little integers.) 2) Convert the internal representation to a string representation of a positional number in some arbitrary base. 3) Convert a DNA 3-codon sequence to internal (binary) representation. (Already done for you) 4) Convert an internal representation to a DNA 3-codon sequence intemal (binary) representation. (Already done for you). Your addition algorithm MUST perform the addition IN the numbers as strings, iterating through the digits of each THE BASE. By which I mean, process string, add num1+ num2]+ carry sum], loop. Do NOT convert to internal memory representation (int) first. Resources: Download the "bases.cpp" template. Complete and test the functions declared in the template. Testing: Thoroughly test with a variety of input valueshp demonstrate that your program works. Turn In: Paper printouts of 1) Source Code. 2) Output Sample Output 16 base 36 is 42 base 10 42 base 10 is 39 base 11 ACT in DNA is 33 base 10 33 base 10 is ACt in DNA ...etc...do a few more to prove it works ABBA base 16 + ACDC base 16 15896 base 16 etc. . . do a few more to prove it works

Explanation / Answer

#include <stdio.h>

#include<string.h>

#include<stdlib.h>

#include<math.h>

/*

Nucleotide

Decimal

Representation--->

A 0

U / T 1

G 2

C 3

*/

int get_num(char n1[], int len){

//first convert n1 to b1 into decimal

int numb10;

int i;

int b1=rand()%20+1; //will generate base for num 1

int t=0;

for(i=len-1;i>=0;i--){

numb10+=ch[i]-'0'*pow(b1, t);

t++;

}

return numb10;

}

void get_num2(char n1[], int len){

int x;

sscanf(n1, "%d", &x);

int base=rand()%20+1;

string str="";

while(x>0){

int q=x/base;

int r=n1%base;

str+=char(r);

x=x/base;

}

printf("%s ",strrev(str));

}

int get_num3(char n1[]){

int numb10=0;

int t=0;

for(int i=len-1;i>=0;i--){

char ch=n1[i];

switch(ch){

case 'A':numb10+=0*ch-'0'*pow(10, t);

t++;

break;

case 'U':numb10+=1*ch-'0'*pow(10, t);

t++;

break;

case 'T':numb10+=1*ch-'0'*pow(10, t);

t++;

break;

case 'G':numb10+=2*ch-'0'*pow(10, t);

t++;

break;

case 'C':numb10+=3*ch-'0'*pow(10, t);

t++;

break;

}

}

return numb10;

}

int get_num3(char n){

string str="";

while(n>0){

int r=n%10;

switch(r){

case 0:str+='A';

break;

case 1:str+='T';

break;

case 2:str+='G';

break;

case 3:str+='C';

break;  

}

n=n/10;

}

printf("%s ",strrev(str));

}

for(int i=len-1;i>=0;i--){

char ch=n1[i];

switch(ch){

case 'A':numb10+=0*ch-'0'*pow(10, t);

t++;

break;

case 'U':numb10+=1*ch-'0'*pow(10, t);

t++;

break;

case 'T':numb10+=1*ch-'0'*pow(10, t);

t++;

break;

case 'G':numb10+=2*ch-'0'*pow(10, t);

t++;

break;

case 'C':numb10+=3*ch-'0'*pow(10, t);

t++;

break;

}

}

return numb10;

}

void get_num5(char str1[], char str2[], int len1, int len2){

int base=rand()%20+1;

int i;

string s="";

int carry=0;

for(i=len1-1;i>=0;i--){

char ch=str1[i];

int flag=0;

int n1, n2;

switch(ch){

case 'A':

flag=1;

n1=10;

break;

case 'B':

flag=1;

n1=11;

break;

case 'C':

flag=1;

n1=12;

break;

case 'D':

flag=1;

n1=13;

break;

case 'E':

flag=1;

n1=14;

break;

case 'F':

flag=1;

n1=15;

break;

}

if(flag==0){

n1=ch[i]-'0';

}

char ch2=str2[i];

int flag=0;

int n1, n2;

switch(ch2){

case 'A':

flag=1;

n1=10;

break;

case 'B':

flag=1;

n1=11;

break;

case 'C':

flag=1;

n1=12;

break;

case 'D':

flag=1;

n1=13;

break;

case 'E':

flag=1;

n1=14;

break;

case 'F':

flag=1;

n1=15;

break;

}

if(flag==0){

n2=ch2[i]-'0';

}

  

int num=n1+n2+carry;

int r=num%base;

str+=char(r);

carry=(num/base);

}

printf("%s ", strrev(str));

}

int main(void) {

// your code goes here

char ch='y';

while(true){

printf("Enter choice: " );

int choice;

scanf("%d",&choice);

switch(choice){

case 1: //convert between bases

printf("Enter String: ");

char str[20];

gets(str);

int len=strlen(str);

get_num(str, len);

break;

case 2: //convert into bases

printf("Enter String: ");

char str[20];

gets(str);

int len=strlen(str);

get_num2(str, len);

break;

case 3:

printf("Enter String: ");

char str[20];

gets(str);

int len=strlen(str);

int num=get_num3(str, len);

printf("%d ",num);

break;

case 4:

int n;

scanf("%d ",&n);

get_num4(n);

break;

case 5:

printf("Enter String: ");

char str1[20];

char str2[20];

gets(str1);

gets(str2);

int len1=strlen(str1);

int len2=strlen(str2);

get_num5(str1, str2, len1, len2);

break;

  

}

printf(" Do you want to continue? ");

scanf("%c",ch);

if(ch=='n'||ch=='N')

break;

}

return 0;

}