I need more comments all over this program. I want to understand in depth what\'
ID: 3586924 • Letter: I
Question
I need more comments all over this program. I want to understand in depth what's going on, so overkill isn't in my dictionary. Thank you!!
#include <stdio.h>
#include <string.h>
void convert(char *radix)
{
int i = 0, j = strlen(radix) - 1;
while (i<j)
{
char temp = radix[i];
radix[i] = radix[j];
radix[j] = temp;
++i;
--j;
}
}
// Main function.
int main()
{
int dec, rad, i = 0, num;
// Ask's the user to enter the decimal number, and stores it into integer 'dec'.
printf("Enter two decimal integers: ");
scanf("%d", &dec);
// Ask's the user to enter the radix number, and stores it into integer 'rad'.
printf("Enter the radix: ");
scanf("%d", &rad);
num = dec;
char output[100];
// Loop until the value of quotient becomes 0.
while (dec > 0)
{
// Get's the remainder.
int rem = dec % rad;
if (rem >= 0 && rem <= 9)
output[i++] = (char)(rem + '0');
else
output[i++] = (char)(rem - 10 + 'A');
dec = dec / rad;
}
output[i] = '';
convert(output);
// Reading the output to the user.
printf("The number %d in base %d is %s ", num, rad, output);
return 0;
}
Explanation / Answer
Here, I have used comments to explain each and every relevant information which will help you to get deep knowledge of the programming concepts.
-----------------------------------------------------------------------------------------------------------------------------------------------
//Include the stdio.h header file which contains all the main functions of c such //as printf(), scanf() etc.
#include <stdio.h>
//Include the string.h header file which contains a large set of useful string
//Handling library functions such as strlen(), strcmp() etc.
#include <string.h>
//The Convert functions converts an expression from one data type to another //type
void convert(char *radix)
{
//In j=strlen(radix) , we calculate the length of string stored in radix character //array
int i = 0, j = strlen(radix) - 1;
// While loop runs here from the first character in radix array to the last one.
while (i<j)
{
//The swap operation is taking place in the below lines.
char temp = radix[i];
radix[i] = radix[j];
radix[j] = temp;
++i;
--j;
}
}
// Main function.
int main()
{
int dec, rad, i = 0, num;
// Ask's the user to enter the decimal number, and stores it into integer 'dec'.
printf("Enter two decimal integers: ");
scanf("%d", &dec);
// Ask's the user to enter the radix number, and stores it into integer 'rad'.
printf("Enter the radix: ");
scanf("%d", &rad);
num = dec;
//Initializing a character array ‘Output’ of 100 elements
char output[100];
// Loop until the value of quotient becomes 0.
while (dec > 0)
{
// Get's the remainder.
int rem = dec % rad;
// In the below statements we change the integer data type to character data //type
if (rem >= 0 && rem <= 9)
output[i++] = (char)(rem + '0');
else
output[i++] = (char)(rem - 10 + 'A');
dec = dec / rad;
}
//Here we put '' after the last character of the output array to mark the end //of string
output[i] = '';
//Calling the convert function for the Output character array
convert(output);
// Reading the output to the user.
printf("The number %d in base %d is %s ", num, rad, output);
return 0;
}
-------------------------------------------------------------------------------------------------------------------------------------------------