Problem in converting.

Hi . I'm a new member in this forum and I have basic in C, I think. I have a bit problem, to convert from decimal to binary. I only have a bit of idea how it going to work. I know, user will input a number, for example 255 .the operation will use % until the final answer is 0, and all the remainder, either 1 or 0 will be the form of the decimal number in binary. and to collect all the remainder in one variable, I will use array. right But I think I misunderstood the concept of array. . Can someone give me a bit hint what is the right thing to do

Code: Code: #include int main() { int userinput; int binary ; int answer[8]; printf("Please input Base 10 Number : "); scanf("%d",&userinput); printf("Base 10 number is : %d \n", userinput); //Calculation for (binary = 0; binary < 8; binary++) { answer[8]= userinput % 2; } printf("%d is in binary form \n",answer); system("pause"); return 0; }

Posted On: Wednesday 19th of September 2012 02:19:57 AM Total Views:  273
View Complete with Replies

Related Messages:

Geometric search/intersection problem   (145 Views)
. I decided to post this in the C forum since its what I use, but its really a general problem. Please feel free to move it to the appropriate forum if needed (I have also asked the same question in and I need to find if a any given point in space (2d/3d) is inside an irregular region formed by rectangles (or cubes in 3d). I have several different boxes with its coordinates. of the boxes overlap with at least another box, meaning its a closed region with no holes. I want to form one big region from all these boxes and then try to find if points lie inside or outside. I have already developed an implementation that uses search trees (quadtrees) and is efficient in looking through each of the boxes to see if the points are there. Now, some of the problems I want to solve have millions of boxes and millions of points, so I'm looking for ways of say having one big region. In 2d this could be a polygon for example which is not hard to do, but not for 3d. Basically what I would like in the end is to form a big region where I can quickly get candidate points, which I can then process in detail with my search trees algorithms. I've been developing the search tree algorithms for the last few days so my head is pretty biased towards that at the moment and I need some external thinking. Any ideas
need to solve simple c problem   (161 Views)
Problem Name: Y = MX Everyone know that the equation of a straight line is Y = MX + C. But if C = 0, then the straight line is passing through the origin. Here I always put C = 0. So our equation will be always Y = MX. Straight line is a geometrical term but my problem is not geometrical. I just use the equation Y = MX. Since Y = MX, so Y is must be divisible by M and X, and for a specific value of Y and various value of M and X, the equation must be satisfy. But in this problem we find (N) the number of M and X that satisfy the equation with condition M must be less than X (M16 = 1 * 16 (11) so it is not countable. So our answer N=2. So your duty is to find out how many way to find M and X (M Input: You have to provide an integer Y (1
Makefile problem; create obj file outside the src folder   (197 Views)
1/ You should move your makefile at the root of the hierarchy instead of the src/ directory, that would be more logical from the user standpoint. When do it, adjust your INCL variable to -Iinclude 2/ What you've tried to do (obj/%.o: %.c) doesn't work because of makefile built-in rules. There's already a built-in rule "%.o: %.c" that exist that will built objects where the source is. Make is an amazing tool and I'm sure you can achieve what you want more easily with some makefile magic but for the moment, here's what I propose. Code: CC = gcc CFLAGS = -g -Wall INCL = -Iinclude EXEC = TmrSbc $(EXEC): obj/m_$(EXEC).o obj/f_$(EXEC).o $(CC) -o $@ $^ obj/%.o: src/%.c $(CC) $(CFLAGS) -c $< $(INCL) -o $@ ... By defining the specialized template "obj/%.o: src/%.c" you avoid to use the built-in one. However in order for it to work, you have to explicitely prefix the object names with the directory in the main rule: $(EXEC): obj/... obj/... otherwise make will look for a pattern like "%.o:..." and fall back on the built-in one. Alternatively you could also do that: Code: ... VPATH := src $(EXEC): obj/m_$(EXEC).o obj/f_$(EXEC).o $(CC) -o $@ $^ obj/%.o: %.c $(CC) $(CFLAGS) -c $< $(INCL) -o $@ ... This is almost what you've tried earlier, but notice the VPATH variable that tells make where to look for files (assuming the makefile is at the root).
a problem   (135 Views)
i have a question: printf("%f",9/5); it prints 0.000000 as output, shouldn't it be 1.000000. Or does it depends on the compiler
Very basic pointer problem   (173 Views)
Hi , I seem to have a very basic pointer problem. I have a main that has 2 counters, i need them to be updated by a function, so i decided to declare both int counters, and pointers to these int to pass to the function. This is how I (tried to) declare them: Code: int leftTriCont,rightTriCont; int* leftTriAbscissa,rightTriAbscissa; leftTriAbscissa=&leftTriCont; rightTriAbscissa=&rightTriCont; so leftTriAbscissa points to leftTriCont and the same with rightTriAbscissa and rightTriCont. So my idea was to pass the pointers to the function, BUT: I get an error, both on declaration, and when I call the famous function: main.c:56: warning: assignment makes integer from pointer without a cast main.c:160: warning: passing arg 4 of `fastTriangulate' makes pointer from integer without a cast Why that error What did i do wrong in the declaration
Stuck on a problem. Help needed please.   (111 Views)
, I am teaching myself programming using Kochan's "Programming in C" book. I am stuck on an exercise and was hoping you could help me out. Here is the question: 4. Write a program that acts as a simple printing calculator. The program should allow the user to type in expressions of the form: number operator The following operators should be recognized by the program: + - * / S E The S operator tells the program to set the accumulator to the typed-in number. The E operator tells the program that execution is to end. The arithmetic operations are performed on the contents of the accumulator with the number that was keyed in acting as the second operand. The following is a sample run showing how the program should operate: Begin Calculations 10 S //Set Accumulator to 10 = 10.000000 //Contents of Accumulator 2 / //Divide by 2 = 5.000000 //Contents of Accumulator 55 - //Subtract 55 -50.000000 100.25 S //Set Accumulator to 100.25 = 100.250000 4 * //Multiply by 4 = 401.000000 0 E //End of program = 401.000000 End of Calculations. Make certain that the program detects division by zero and also checks for unknown operators. Thank you so much for your help !
Character array problem   (163 Views)
I am trying to solve this problem from K&R book. Write a function reverse(s) that reverses the character string s. Use it to write a program that reverses its input a line at a time. My solution Code: #include #include #define MAXLINE 10 void reverse(char r[], char s[]); int main(void) { int i,c; char line[MAXLINE]; char reversed[MAXLINE]; for (i=0; i < MAXLINE && (c=getchar())!='X' && c!='\n'; ++i) line [i] = c; reverse (reversed,line); printf ("%s", reversed); return 0; } void reverse (char r[], char s[]) { int i=0; for (i=0; i
TFTP connection problem   (148 Views)
, I want to implement simple TFTP protocol, but I have problem: Client send request to the server, where the server port is 69. Then server sends back 512 bytes of data, or an ACK. But , server is not sending data on 69 , but on new port (TID). (RFC 1350) Must I have two different sockets for this Two connections, two binds When I duplicate all, I have also problems, they can not communicate. They communicate just if the client and the server have the same port: 69 ! Why
function problem   (177 Views)
have a problem, when int n=1 float a and b variable's are still the same when n=0. i write this program without function it work. Code: float people(float firstyear, float grow){ float total; grow = firstyear / 100 * 4.5; firstyear= grow + firstyear; total=firstyear; return total; } int main() { float a = 65, b = 0; int n; for(n = 0; n < 5; n++){ printf("%.2f\n", people(a ,b)); } return 0; } without function Code: int main() { float firstyear=650000, grow=0; int n; for(n = 0; n < 5; n++){ grow = firstyear / 100 * 4.5; firstyear= grow + firstyear; printf("%.2f\n", firstyear); } return 0; }
apparently linker problem   (227 Views)
. I'm trying to use a "Numerical Recipes in C" routine for the first time. I got a file with the main function, main.c, and a file with the routine I wanna use (Jacobi function) jacobi.c. In addition, I have 2 header files, that, for what I could see, are standard for all Numerical Recipes routines: nrutil.h and nr.h. Here the are the codes: main.c: Code: #include #include "nr.h" #include "nrutil.h" int main(void) { int i, j, nrot; int n = 2; /* We need a Numerical Recipes-style "matrix" to use as an argument to call the jacobi function. Here are the matrix values stored in row-major order in a 1-D array. This array name will be used as an argument for the convert_matrix function. */ float a_array[] = { 5.0, -2.0, /* First row */ -2.0, 2.0 /* Second row */ }; float *d = vector(1, n); float *r = vector(1, n); float **v = matrix(1, n, 1, n); float **a = convert_matrix(&a_array[0], 1, n, 1, n); printf("Matrix:\n"); for (i = 1; i
how to solve this problem   (199 Views)
You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≤ n ≤ 1000) the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Sample test case: Input 11 3 4 5 4 5 3 5 3 4 4 0
problem with send and receive a file and its size   (140 Views)
This part of code is for the client. It need to receive first the file size and then the size: Code: void do_retr_cmd(int f_sockd){ int fd; ssize_t nread = 0; uint32_t fsize, fsize_tmp, total_bytes_read, size_to_receive; char *filename = NULL, *conferma = NULL, *filebuffer = NULL; char buf[256], dirp[256], t_buf[256]; memset(dirp, 0, sizeof(dirp)); memset(buf, 0, sizeof(buf)); memset(t_buf, 0, sizeof(t_buf)); printf("Write the name of file to download: "); fgets(dirp, BUFFGETS, stdin) filename = NULL; filename = strtok(dirp, "\n"); sprintf(buf, "RETR %s", dirp); if(send(f_sockd, buf, strlen(buf), 0) < 0){ perror("Errore durante l'invio del nome del file"); onexit(f_sockd, 0, 0, 1); } fsize = 0; recv(f_sockd, t_buf, sizeof(t_buf), 0) fsize = atoi(t_buf); fd = open(filename, O_CREAT | O_WRONLY, 0644); fsize_tmp = fsize; filebuffer = (char *)malloc(fsize); total_bytes_read = 0; nread = 0; for(size_to_receive = fsize; size_to_receive > 0;){ nread = read(f_sockd, filebuffer, size_to_receive); if(nread < 0){ perror("read error on retr"); onexit(f_sockd, 0, 0, 1); } if(write(fd, filebuffer, nread) != nread){ perror("write error on retr"); onexit(f_sockd, 0, 0, 1); } size_to_receive -= nread; } close(fd); fflush(stdout); fflush(stdin); memset(buf, 0, sizeof(buf)); recv(f_sockd, buf, 21, 0) printf("%s", buf); memset(buf, 0, sizeof(buf)); memset(t_buf, 0, sizeof(t_buf)); memset(dirp, 0, sizeof(dirp)); free(filebuffer); } Here's the code of the server which first send the file size and then the file: Code: void do_server_retr_cmd(f_sockd, m_sockd){ int fd, rc; uint32_t fsize, size_to_send; char *filename = NULL, *other = NULL; char buf[512], t_buf[256]; off_t offset; struct stat fileStat; memset(buf, 0, sizeof(buf)); memset(t_buf, 0, sizeof(t_buf)); recv(f_sockd, buf, sizeof(buf), 0) other = NULL; filename = NULL; other = strtok(buf, " "); filename = strtok(NULL, "\n"); if(strcmp(other, "RETR") == 0){ printf("Ricevuta richiesta RETR\n"); } else /* do something */ fd = open(filename, O_RDONLY); memset(&fileStat, 0, sizeof(fileStat)); fileStat.st_size = 0; fstat(fd, &fileStat) fsize = fileStat.st_size; snprintf(t_buf, 255, "%" PRIu32, fsize); send(f_sockd, t_buf, sizeof(t_buf), 0) offset = 0; for (size_to_send = fsize; size_to_send > 0; ){ rc = sendfile(f_sockd, fd, &offset, size_to_send); if (rc Errors checking have been omitted
Execution time problem.   (159 Views)
We need to sum n numbers using for,while, and do while loops.. and need to compare the execution time for each of them. I wrote the following code. but every time the output comes out to be different!! so not able to decide which loop executes the fastest. kindly help me figure this out.. Code: #include #include #include #include #define n 20000 int sumfor(); int sumwhile(); int sumdowhile(); int a[n]; time_t first,second; double tfor,twhile,tdowhile; void main() { int i,s1,s2,s3; // initializing the array.. for(i=0;i
Guys please check my codes based on the problem < I don't know if i got it right >   (190 Views)
I have to create a program that will accept integers from the user and store them in one-dimensional array. It should also accept a number to be searched. This number will be searched among the 5 input values. If it is found, display the message "Number Found!". Otherwise, display "Number Not Found!".... I'll post the codes.... but I don't know if i get it right.... TT_TT Code: void main() { int arr[5], ctr=0, numsee; for(ctr=0; ctr
fgets problem   (174 Views)
Hi all. My program searches for and replaces the first occurrence of a string in a text file, which contains the line "How now brown cow." I want to replace "brown" with purple, so that the line will be "How now purple cow." Can someone please explain why the fgets() function highlighted in red near the bottom is not working I use fseek() to set the position to after the "brown", and then try to read in "cow." with fgets(), but it doesn't work. Basically, I get the test before "brown", and the text after "brown", then sandich the replacement string between the two strings make the replacement.
openssl: RSA_generate_key, problems   (279 Views)
i'm working on linux and i have a problem. The RSA_generate_key generate always the same public key on different running, and on different sources. How can i generate always different key for each call
pointer conversion problem   (180 Views)
1) Code: float f = 65.00f; int * ip = (int*)&f; printf("%i", *ip); prints garbage value instead. Why When i try to convert it double*, it prints 0.000000. I guess that it is because of the memory size difference of 4 bytes of float and 8 bytes of double. When i try the similar between int* and char*, it converts fine, is it because of fact that C stores character as decimal equivalent 2) Code: int * ip = (int[]){1,2,3,4}; ip[2] = 5; workes fine. Why Isn't it supposed to crash the program or cause so-called "undefined behaviour" Thank you
problem with bubble sort   (152 Views)
Hmm... this error has nothing to do with the compiler or your code, it's a problem with the makefile. The rule "helpers" apparently doesn't exist or maybe the name of the makefile is not standard and you have to specify it on the command line., Originally Posted by Dave Couture here is the makefile Code: # # Makefile # # Computer Science 50 # Problem Set 3 # all: find generate find: find.c helpers.c helpers.h gcc -ggdb -std=c99 -Wall -Werror -o find find.c helpers.c -lcs50 -lm generate: generate.c gcc -ggdb -std=c99 -Wall -Werror -o generate generate.c clean: rm -f *.o a.out core find generate Yep, root4 called it: you don't have a 'helpers' target in your make file. You have targets 'all', 'find', 'generate' and 'clean'. If you want to use the command 'make helpers', you must write a 'helpers' target. Follow the pattern used for find and generate. You should also try reading a tutorial or two on using make and on writing make files. Check the tutorials on our site.
Socket operation on non-socket problem   (171 Views)
I try to run this code: ipt_evil.c - ipt-evil - A userspace iptables QUEUE that does "evil" things to packets. - Google Project Hosting . But I get this error: Socket opertaion on non-socket - Could anyone tell me what is wrong here MC
Segmentation fault; struct and pointer problem   (154 Views)
... I try to create some simple posix shared memory programmed that will take some value... then I facing with some segmentation fault... I only have basic knowledge about struct and pointer... only learn by theory.. never used it before.. please advice My structure Code: typedef struct { unsigned int shm_hour; double *shm_adc; unsigned int *shm_dio; /* pid_t pid; */ } shmem_t; My idea is to store the data inside the struct.. shm_hour only have one value but shm_adc and shm_dio will have multiple input value... Here some of my declaration... Code: ... shmem_t *data; double *shm_adc = NULL; unsigned int *shm_dio = NULL; int main(int argc, char *argv[]) { ... /* attach/map shared memory to our data type */ data = (shmem_t *) shmat(shmid, NULL, 0); ... data->shm_hour = 13; /* data is the pointer to the struct */ data->shm_adc[0] = 0.01; data->shm_adc[1] = 1.23; data->shm_adc[3] = 3.33; data->shm_adc[2] = 1.52; data->shm_adc[7] = 2.58; data->shm_dio[1] = 1; data->shm_dio[3] = 0; ... And after debugging.. I know the problem came from shm_adc.. maybe I have wrong declaration and wrong concept.. Really appreciate if I could get some little help from all you .. Code: Program received signal SIGSEGV, Segmentation fault. 0x080484e4 in main (argc=1, argv=0xbffff3b4) at shm_data.c:50 50 data->shm_adc[0] = 0.01;