im writing a code for converting a number to hexadecimal and im getting a random numbers as result.
at first i succeed to convert the number but it was in a reversed order (the first mod needs to be the last number or letter).
the code is a part (for cases of %x) from a big project that is kind of implementation of sprintf (so sprintf or printf are not allowed obviously). so the buffer is for composing a string without any placeholders.
thank u in advance.
here's my code:
int num = *(int*)ptrs[counter];
int tempnum=num;
int mod=0;
int length =0;
for(int i=0;tempnum !=0;i++)
{
length++;
tempnum /= 16;
}
int array[length];
for(int i= length; i>0;i--)
{
mod = num%16;
num = num/16;
array[i] = mod;
}
for(int i=0;i<length;i++)
{
if(array[i]<10)
*buffer = array[i]+ '0';
else
*buffer = array[i] -10 + 'a';
buffer++;
}
The for loop of calculating array[i] should change to:
for(int i = length-1; i>=0;i--) // i from (length - 1) to 0 instead of from length to 1.
{
mod = num%16;
num = num/16;
array[i] = mod;
}
You do not need to change the buffer pointer. You can use:
for(int i=0;i<length;i++)
{
if(array[i]<10)
buffer[i] = array[i]+ '0';
else
buffer[i] = array[i] + 55;
}
then do not forget at the null character at the end of buffer:
buffer[length] = '\0';
I do not see the declaration of buffer in your code, so i propose the solution above for the declaration:
char buffer[length+1];
for(int i= length; i>0;i--)
{
mod = num%16;
num = num/16;
array[i] = mod;
}
mistakes at array[i] to array[i-1].
buffer = array[i] + '87';
I suggest you use:
buffer = array[i]-10 + 'a';
printf("%i", (int)[any hexadecimal number]);
would print the integer form of this '[any hexadecimal number]' number
Related
This Code convert a decimal number i to its binary ... Along with storing its value in a array ans.
In This below code i used ans size 100 . Now when i enter input as 0.5 it gives a weird output . But if i change the size of Array to 1000 the it's giving correct output can anyone explain this behaviour.
Code with ans Size 100
#include <stdio.h>
int main() {
float n;
printf ("Enter Decimal Number:");
scanf("%f", &n);
// Integer Part of Number
int ip = n;
// To store the Binary Number
char ans[100];
// Index of array
int i = 0;
// For integer part
while(ip != 1 && ip != 0) {
if ((ip % 2) == 0) {
ans[i] = '0';
}
else {
ans[i] = '1';
}
i++;
ip /= 2;
}
if (ip) {
ans[i++] = '1';
}
else{
ans[i++] = '0';
}
// Reverse the Integer Part
for (int j = 0; j < (i/2); j++) {
char temp = ans[j];
ans[j] = ans[i - j - 1];
ans[i - j - 1] = temp;
}
ans[i++] = '.';
ans[i] = '0';
// Flaoting Part of Number
float fp = n - (int) n;
// For floating Part
if (fp) {
for (int j = 0; j < 5; j++) {
fp *= 2;
// Integer part of fp
int x = fp;
// floating part of new fp
fp = fp - x;
if (x) {
ans[i++] = '1';
}
else {
ans[i++] = '0';
}
if(!fp){
break;
}
}
}
printf("%f in Binary is %s", n, ans);
return 0;
}
OUTPUT
Enter Decimal Number:0.5
0.500000 in Binary is 0.1�
[Program finished]
Code with ans size 1000
#include <stdio.h>
int main() {
float n;
printf ("Enter Decimal Number:");
scanf("%f", &n);
// Integer Part of Number
int ip = n;
// To store the Binary Number
char ans[1000];
// Index of array
int i = 0;
// For integer part
while(ip != 1 && ip != 0) {
if ((ip % 2) == 0) {
ans[i] = '0';
}
else {
ans[i] = '1';
}
i++;
ip /= 2;
}
if (ip) {
ans[i++] = '1';
}
else{
ans[i++] = '0';
}
// Reverse the Integer Part
for (int j = 0; j < (i/2); j++) {
char temp = ans[j];
ans[j] = ans[i - j - 1];
ans[i - j - 1] = temp;
}
ans[i++] = '.';
ans[i] = '0';
// Flaoting Part of Number
float fp = n - (int) n;
// For floating Part
if (fp) {
for (int j = 0; j < 5; j++) {
fp *= 2;
// Integer part of fp
int x = fp;
// floating part of new fp
fp = fp - x;
if (x) {
ans[i++] = '1';
}
else {
ans[i++] = '0';
}
if(!fp){
break;
}
}
}
printf("%f in Binary is %s", n, ans);
return 0;
}
OUTPUT
Enter Decimal Number:0.5
0.500000 in Binary is 0.1
[Program finished]
Your ans string in not null-terminated, so printing a string that is not null-terminated causes UB (undefined behavior). As to why it works when the size is 1000 vs. 100 I cannot say, but the bottom line is that undefined bahavior is undefined... anything could happen. So fix the bug as follows:
:
:
// Flaoting Part of Number
float fp = n - ip;
// For floating Part
if (fp) {
for (int j = 0; j < 5; j++) {
fp *= 2;
// Integer part of fp
int x = (int)fp;
// floating part of new fp
fp -= x;
ans[i++] = x ? '1' : '0';
if(!fp)
break;
}
} else i++;
ans[i] = '\0';
printf("%f in Binary is %s", n, ans);
return 0;
}
I am turning my comment into an answer.
Your ans array is not null terminated, hence printf("%s", arr) doesn't know where to stop reading and keeps printing whatever garbage follows. If you are going to print it with %s append a null terminator at the end of your data in the array before printing it, or print it character by character in a loop.
You can also initialize your array as char ans[100] = { 0 }, which assigns all elements 0. Which also is the ASCII code for the null terminator, hence when you are done writing your characters, the next character will be a null terminator. This however, doesn't work if you re-write a smaller data to your array, previous characters that are not overwritten will still be printed. You can consider using memset to clean you array with 0 before each time you re-write it.
When it comes to the difference between 100 and 1000 element array, I guess your compiler happens to allocate 100 element array in a part of the memory containing garbage, and 1000 element array in a part that happens to contain 0. This is unpredictable and as you can see, can mislead you. Never, ever use your variables uninitialized.
I have written the code in c language(for n factorial).
The code is-->
#include <stdio.h>
#include <string.h>
int main()
{
int n;
scanf("%d", &n);
char str[200];
str[0] = '1';
int k;
int t = 0;
int carry = 0;
for (int i = 1; i <= n; i++)
{
carry = 0;
for (int j = 0;; j++)
{
int arr = str[j] - 48;
k = arr * i + carry;
arr = k % 10;
str[j] = arr + 48;
carry = k / 10;
if (carry == 0 && str[j + 1] == '\0')
{
break;
}
if (carry != 0 && str[j + 1] == '\0')
{
for (int r = j;; r++)
{
str[r + 1] = (carry % 10) + 48;
carry = carry / 10;
if (carry == 0)
{
str[r + 2] = '\0';
t = 1;
break;
}
}
break;
}
}
}
int len = strlen(str);
// // printf("%d\n",len);
char prr[200];
for (int i = 0; i < len; i++)
{
int b = len - i - 1;
printf("%c", str[b]);
}
// printf(" %s\n", str);
return 0;
}
In other systems(including online c compilers) it is showing correct answer
Input=7
Output=5040
In my system(laptop):
Input=7
Output=+,*)'040
My laptop is hp envy 13-ab070TU
os:Windows 10 Home
system type:64-bit operating system, x64-based processor
I have also tried my code on virtual machine in my laptop on ubuntu and kali but the result is same that it is showing wrong output.
What is the reason for this and how I can rectify this issue?
You're filling digits in to your str array, but str is not necessarily a proper, null-terminated string.
At the end, you call strlen(str) to discover how many digits you computed in your result. But since str is not necessarily a null-terminated string, strlen doesn't necessarily get the right answer.
str is a local (stack allocated) variable, and you don't give it an initializer, so it starts out containing unpredictable garbage.
If str happens to start out containing zeroes (which it might), your program will happen to work. But if it contains one or more nonzero bytes, strlen might compute too long a length, so your digit-printing loop at the end might print some extra characters, as you saw on your laptop.
There are two or three ways to fix this.
Call memset(str, '\0', sizeof(str)); to fill the array with 0.
Initialize the array: char str[200] = "";. (It turns out that will fill the whole array with 0.)
Keep track of the number of digits some other way. I suspect it's the maximum value ever taken on by k or r, or something like that.
I"m trying to store int array as a str and display it but in the reverse order.
Its only while printing the str that i get junk.
What is wrong in my code?
int main() {
int a[] = { 1, 2, 3 }; // Output should be 321 (char)
int size = sizeof(a) / sizeof(int);
char str[size + 1];
int i;
for (size = size - 1; size >= 0; size--) {
sprintf(&str[size], "%d", a[size]);
//printf("%c\n", str[size]);
}
printf("%s\n", str); // I get garbage.
}
I modified your solution with several bug fixes. For starters, you can't assume that your integer array will only hold single digit values.
And that for loop as you have it:
for(size=size-1;size >= 0;size--)
Is very suspicious looking. (the index variable is the thing its based off?)
Simple solution
This is likely what you meant:
for(i = 0; i < size; i++) {
sprintf(&str[i],"%d", a[size-1-i]);
}
str[size] = '\0';
Or this:
str[size] = '\0';
for(i = size-1; i <= 0; i--) {
sprintf(&str[i],"%d", a[size-1-i]);
}
Better solution
I'm not sure what you are expecting to do if an integer within the a array is negative. So the - sign will just get inserted into str inplace.
The solution I have will first count how many chars are needed for each integer in a. Then it will allocate the str buffer with that length (+1 for null char).
Then we make use of the return value from sprintf to figure out where to concatenate onto. We could use strcat, but this is likely faster.
int main() {
int j = 0;
int a[] = { 1,2,3 }; // Output should be 321 (char)
int size = sizeof(a) / sizeof(int);
int length = 1; // +1 for final null char
// Count the size of characters needed for each integer
// Do a dummy sprintf and use its return value to figure out how many chars are needed
for (int i = 0; i < size; i++) {
char tmp[sizeof(int) * 5]; // sizeof(int)*5 is big enough to hold any integer including a negative value
length += sprintf(tmp, "%d", a[i]); // utilize the return value from sprintf and add it to the running length
}
char str[length];
str[0] = '\0'; // initially null terminate our string
// reverse print chars from a into str
for (int i = 0; i < size; i++) { // use i as index variable, not size
j += sprintf(str + j, "%d", a[size - 1 - i]);
}
printf("%s\n", str);
}
Alternative solution, closer to original posts, and clearly not trying to address the general problem (assume values are single digit):
int a[]={1,2,3}; // Output should be 321 (char)
int size = sizeof(a)/sizeof(int);
char str[size+1];
for(int i=0; i<size ; i++) {
str[size-1-i] = ‘0’ + a[i];
}
str[size] = 0;
printf("%s\n", str); // I get garbage.
}
Taking advantage of the assumed input value, converting each int to character representation at the reverse position.
i'm a beginner and my english is not well so sorry first. im trying to sum the numbers in a string (for a14fg5pk145 it returns 14+5+145), and it doesn't work:
"Exception thrown: read access violation.
str was 0x61."
this i my code:
void main()
{
int x, i;
char* pp;
char buffer[SIZE];
printf("Please enter numbers and letters:\n");
gets(buffer);
pp = buffer;
x = SumStr(*pp);
printf("%d", x);
}
int SumStr(char* str)
{
int sum=0, num=0, flag = 0;
while ((*str) != '\0')
{
while (((*str) > '1') && ((*str) < '9'))
{
if (flag == 0)
{
num += (*str);
flag = 1;
}
else if (flag == 1)
num = num * 10 + (*str);
str++;
}
if (flag == 0)
str++;
sum += num;
num = 0;
flag = 0;
}
return sum;
}
First problem with your code which is causing Exception.
x = SumStr(*pp);
it should be
x = SumStr(pp);
Because you should pass address of the string pointer not its first character by attaching asterix.
Second Issue that will not make it work is.
num += (*str);
and
num = num * 10 + (*str);
By (*str) you are actually adding the character ascii value instead of number.
This will solve the problem by changing the ascii value to number.
num += (*str) - '0';
num = num * 10 + (*str) - '0';
This may serve your purpose
#include<stdio.h>
#include<string.h>
int main()
{
int i, sum = 0, store;
char str[] = "a14fg5pk145asdasdad6";
int length = strlen(str);
for(i = 0; i < length; i++) {
store = 0;
while(isdigit(str[i])) {
store = (store * 10) + (str[i] - '0');
i++;
}
sum += store;
}
printf("%d\n", sum);
return 0;
}
output :
170
Pass pp, not *pp, to the function SumStr. *pp has the type char, and the function expects char *. In fact, you do not even need pp at all, just pass the buffer as the parameter.
Also:
Never use gets(). Because it is impossible to tell without knowing the
data in advance how many characters gets() will read, and because
gets() will continue to store characters past the end of the buffer, it
is extremely dangerous to use. It has been used to break computer
security. Use fgets() instead.
How can I remove a certain number of digits in a number so the number obtained is minimal?
Specifically, I want to write a function int remove_digits(int large, int num_digits_to_remove) such that:
Any num_digits_to_remove digits are removed from large as though removing characters from its string representation
The number that is returned has the lowest possible value from removing digits as in step 1
For example, removing 4 digits from 69469813 would give 4613
I would prefer answers written in C.
Idea:
char number[] = "69469813";
char digits[ARRAY_SIZE(number)];
size_t i;
// sort digits; complexity O(n * log n);
sort_digits(digits, number); // -> digits becomes "99866431"
for (i = 0; i < number_of_digits_to_be_removed; ++i) {
size_t j;
for (j = 0; j < ARRAY_SIZE(number); ++j) {
if (number[j] == digits[i]) {
number[j] = 'X'; // invalidate it
break;
}
}
}
for (i = 0; i < ARRAY_SIZE(number); ++i)
if (number[i] != 'X')
printf("%c", number[i]);
Whole thing has a complexity of O(n * m);
The basic idea is that if you can only remove one digit, you want to remove the first digit (starting with the most significant digit) that is followed by a smaller digit.
For example, if your number is 123432, you want to remove the 4 (since it is followed by a 3), resulting in 12332.
You then repeat this process for as many digits as you want to remove:
char *num = "69469813";
char *buf = malloc(strlen(num)+1);
size_t to_remove = 4;
while (to_remove --> 0) {
char *src = num;
char *dst = buf;
while (*src < *(src+1)) { *dst++ = *src++; } // Advance until the next digit is less than the current digit
src++; // Skip it
while (*dst++ = *src++); // Copy the rest
strcpy(num, buf);
}
printf("%s\n", num); // Prints 4613
I don't know C but here is how I would do it in java:
String original = "69469813";
String result = "";
int numNeedToBeTaken = 4;
int numLeft = original.length() - numNeedToBeTaken;
while(result.length() < numLeft)
{
String temp = original.substring(0,original.length()-numNeedToBeTaken+1);
int smallest= 9;
int index = 0;
for(int i = 0; i<temp.length(); i++)
{
int number = Integer.parseInt(Character.toString(temp.charAt(i)));
if( number < smallest)
{
smallest = number;
index = i+1;
}
}
numNeedToBeTaken--;
result = result.concat(String.valueOf(smallest));
original = original.substring(index);
}
Log.d("debug","result: "+result); //tested to work with your example, returns 4613
converting this to C should be pretty easy, I only used some basic operations.