2 Replies - 355 Views - Last Post: 22 January 2013 - 11:11 PM Rate Topic: -----

#1 kelvinyap92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-September 12

Error Display in Conversion Base program in C.

Posted 22 January 2013 - 10:15 PM

Hey all,
I'm kinda new in C program. So I'm suppose to convert a value to a base number from 1 - 16.
The program will compile, but output is wrong. I'm not sure which part did i do wrong, whether isit the reverse part or the code for conversion.

Really hope I could get help from all the members here. Thanks


#include<stdio.h>


void DivAlg(int,int);
int main() 
{
 int val,base; //variables declaration
 
 printf("Enter any non-negative value: "); //Print a statement to ask user for non-negative input
 scanf("%d",&val); //prompt user for non negative value input
 
 while(val<0) //If the value entered is negative integer, prompt user for positive integer/value
 {
   printf("Enter a positive integer: ");
   scanf("%d",&val);
 }
 printf("Enter a base value between (1-16)"); //Print a statement to ask user for base input
 scanf("%d",&base);
 while(base<1 || base>16)
 {
   printf("Enter the base value within the range(1-16)");
   scanf("%d",&base);
 }

 DivAlg(val,base);
 
}
void DivAlg(int val,int base)
{
{
  int converted_number[64];
  int next_digit, index=0;
  char TABLE[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  
  while(val != 0)
{
  converted_number[index] = val % base;
  val = val/base;
   ++index;
}

--index;
printf("\n\nConverted Number = ");
for( ; index>0; index--)
{
    printf("%c", TABLE[converted_number[index]]);
}
}
  printf("\n");
}





Is This A Good Question/Topic? 0
  • +

Replies To: Error Display in Conversion Base program in C.

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Error Display in Conversion Base program in C.

Posted 22 January 2013 - 10:40 PM

Quote

The program will compile, but output is wrong.

Define wrong
Was This Post Helpful? 0
  • +
  • -

#3 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Error Display in Conversion Base program in C.

Posted 22 January 2013 - 11:11 PM

I am able to see that you only get output when your val is greater than or equal to your base, looking at your line 38...

Quote

So I'm suppose to convert a value to a base number from 1 - 16.


Well, can you please give an example of the above statement ?

regards,
Raghav

Edit: Typo

This post has been edited by raghav.naganathan: 22 January 2013 - 11:31 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1