It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I'm working in a small program using Language C.
I want to create a function which transform a decimal number to binary.
So i Have that:
#include< stdio.h>
int *binaire(int nb,int base)
{
int d,i=0,j;
int *tab= (int*) malloc(sizeof(int));
while(nb>0){
tab[i]=(nb%base);//inverser les bits
nb=nb/base;
i++;
}
return tab;
}
please Help
Thxs
sizeof returns the size in bytes, not in bits. May be you are looking for this:
int *tab= (int*) malloc(sizeof(int) * 8);
EDIT: As pointed in the comments, there are other problems here. Please check them too.
EDIT2: The previous code contains a bug in calculation. I am assuming that you are trying to store the bits in an int array. So the array will contain sizeof(int) * 8 integers, all either zero or one. So the code will be:
int arraySize = sizeof(int) * 8;
int *tab = (int *) malloc(arraySize * sizeof(int));
In fact you don't need to use full int to store a single bit. One byte character is enough to store a bit.
Somethign along those lines:
#include <math.h>
#include <string.h>
#include <stdio.h>
typedef struct value_
{
int* digits;
int nb_digits;
} value;
value* base_convert(int nb,int base) ;
value* make_value(int nb, int base)
{
value* v = malloc(sizeof(value));
if(v)
{
v->nb_digits = 1+round(log(nb)/log(base));
v->digits = malloc(sizeof(int)*v->nb_digits);
if(v->digits) base_convert(v, nb, base);
}
return v;
}
void destroy_value(value* v)
{
if(v->digits) free(v->digits);
free(v);
}
void base_convert(value* v, int nb,int base)
{
int i;
for(i=0;i<v->nb_digits;++i)
{
v->digits[v->nb_digits-i-1]=(nb%base);//inverser les bits
nb=nb/base;
}
return v;
}
int main()
{
int i;
value* v = make_value(137,2);
for(i=0;i<v->nb_digits;++i) printf("%d",v->digits[i]);
destroy_value(v);
return 0;
}
This needs :
a get_digit function to mask the rough access to value::digits
Number of digits is computed using log to be generic. The abse 2 version can be computed in a faster way using bitshifts.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I decided to rewrite what I was doing going backwards to basics:
#include <stdio.h>
int main () {
int a;
int b;
int c;
int d;
printf("ta-dah: %i %i %i %i\n", a, b, c, d);
return 0;
}
I call it me.c and I compile it with gcc me.c and run it with ./a.out.
I get this as a result:
hc$ ./a.out
ta-dah: 32767 1477090280 0 0
This is a very simple program, and I am not sure where the error is. Any suggestions?
You dont initialize the values, so it prints garbage values (whatever happens to be on the stack memory at that point in (memory)spacetime.
You should initialize them first, if you want to be able to read from them.
int a = 3;
int b = 42;
int c = 1337;
int d = 0;
Example
Non-static variables (local variables) are indeterminate. Reading them prior to assigning a value results in undefined behavior.
Either initialize the variables:
#include <stdio.h>
int main () {
int a = 1;
int b = 2;
int c = 3;
int d = 4;
printf("ta-dah: %i %i %i %i\n", a, b, c, d);
return 0;
}
Output: ta-dah: 1 2 3 4
Or set them to static:
#include <stdio.h>
int main () {
static int a;
static int b;
static int c;
static int d;
printf("ta-dah: %i %i %i %i\n", a, b, c, d);
return 0;
}
Output: ta-dah: 0 0 0 0
Unless you initialize the variables with values you will see whatever value happens to be at the memory address used to store the variable's value.
Change it to:
#include <stdio.h>
int main () {
int a = 1;
int b = 2;
int c = 3;
int d = 4;
printf("ta-dah: %i %i %i %i\n", a, b, c, d);
return 0;
}
And you will get:
ta-dah: 1 2 3 4
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'd like to know how many element I have between two pointers to a tab, with a function.
For example in this code, nb would be 20.
int main(void)
{
int t[50] = {0};
int nb;
nb = finding_number_element(&t[10], &t[30]);
return (0);
}
Have you got a idea?
Try
int finding_number_element(int *a, int *b)
{
return (b - a);
}
if a and b will be pointers to the same array this should work.
If you need something that doesn't care about the data type of t
#define ele_size(x) sizeof(x[0])
int main(void)
{
int t[50] = {0};
int nb;
nb = finding_number_element(ele_size(t),&t[10], &t[30]);
return (0);
}
int finding_number_element(unsigned int ui_ele_size, void *a, void *b)
{
unsigned int ui_pointer1 = (unsigned int)a;
unsigned int ui_pointer2 = (unsigned int)b;
return ((b - a)/ui_ele_size);
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Write a function that will return a value in which i-th byte of x has been replaced by b:
unsigned replace_f (unsigned x, int i, unsigned char b){
}
EX: replace_f(0x12345678, 2, 0xBC) --> 0x12BC5678
unsigned replace_f (unsigned x, int i, unsigned char b){
unsigned char *place = (unsigned char*)&x;
place[sizeof(int)-i] = b;
return x;
}
Assume little endian
Didn't try out, but this could work:
unsigned replace_f (unsigned x, int i, unsigned char b){
char *c;
c = (char *)&x;
c[i] = b;
return x;
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
How to take some unknown number of integers from input and display them on the console?
The number of values will be given through console.
Well the prototype for main is:
main(void) or
main(int argc, char *argv[]);
This are the command line arguments of the program
you can iterate through them with something along this lines
int i = 0;
for (i=0; i < argc; i++) {
printf("%s" argv[i]);
}
Untested but should be enough to get you going.
You can dynamically allocate memory for the numbers using malloc.
int count=0;
scanf("%d",&count);
int* numbers=malloc(sizeof(int)*count);
//take integers from input here
free(numbers);
normally your main method should look something like this
int main(int argc, char** argv)
when you run the you program you will supply a number of arguments to it, say n numbers, so you will have n+1 arguments in total (recall that your program name is argument 0!). therefore you can do something like:
int* array = malloc(sizeof(int) * (argc-1));
int i;
for (i = 0; i < argc-1; i++) {
array[i] = atoi(argv[i+1]);
}
then you can do:
for (i = 0; i < argc-1; i++) {
printf("%d ", array[i]);
}
sorry if there are any syntax errors
hope this helps
Print one-by-one as you go (ignore the first: the number of numbers)
int main(void) {
<READ_NUMBER>; // and promptly ignore it
while (<READ_NUMBER>) {
printf(<PRINT_NUMBER_JUST_READ>);
}
return 0;
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I have been asked in an interview to swap the last and first digit in an integer using bitwise operators. Tried a lot but I could not find the solution. How can I do this?
Use int digits = log10(x) to get the number of digits.
Use int first = x / pow(10,digits) to get the first digit.
Use int last = x % 10 to get the last digit.
Put it all together and you have
int swapped = x + (last - first) * pow(10,digits) + (first - last)
A trivial solution:
def swap_digit(n):
x = str(n)
if len(x) < 2:
return x
return int(x[-1] + x[1:-1] + x[0])
EDIT: Added a quick and dirty C solution
#include <stdio.h>
#include <string.h>
int main()
{
int n = 123456789;
char buf[100];
int r = snprintf(buf, sizeof(buf), "%d", n);
char t = buf[0];
buf[0] = buf[r-1];
buf[r-1] = t;
int swap;
sscanf(buf, "%d", &swap);
printf("n = %d, swap = %d\n", n, swap);
return 0;
}
def swap(i):
s = list(str(i))
s[0], s[-1] = s[-1], s[0]
i = int(''.join(s))
return i
print swap(123456789) # 923456781