Question

In: Computer Science

Write a program that prompts the user to enter a positive integer and then computes the...

Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files.

dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number.

dec2bin.h header file that has function prototype for dec2bin() function

dec2binconv.c file with main function that calls dec2bin and print results.

This is what i have so far. Im doing this in unix. All the files compiled fine but when i tried to link them I got errors.

dec2bin.c

#include <stdio.h>

#include <string.h>

#include "dec2bin.h"

void dec2bin(int x, char s[])

{

int i,k;

char s1[8];

for(i=0; x > 0; i++)

{

if(x%2) s1[i] = '1';

else s1[i] = '0';

x = x/2;

}

//reverses the string s1 and store the result in s

k = strlen(s1);

for(i=0; s1[i] != ' \0' ; i++)

s[k-i] = s1[i];

}

dec2bin.h

#include <stdio.h>

#include <string.h>

void dec2bin(int x, char s[])

dec2binconv.c

#include <stdio.h>

#include <string.h>

#include "dec2bin.c"

#include "dec2bin.h"

int main()

{

int x;

char arr[8];

printf("Enter an integer\n");

scanf("%d", &x);

dec2bin(x,arr[8]);

printf("%s",arr);

return 0;

}

Solutions

Expert Solution

// dec2bin.h

#include <stdio.h>

#include <string.h>

void dec2bin(int x, char s[]);

-----------------------------------------------------------------------------------------------------------

// dec2bin.c

#include "dec2bin.h"

void dec2bin(int x, char s[])

{

int i=0,k=0;

char s1[8];

for(i=0; x > 0; i++)

{

if(x%2)

{

s1[i] = '1';

}

else

{

s1[i] = '0';

}

x = x/2;

}

k = strlen(s1);

for(i=0; s1[i] != '\0' ; i++)

{

s[k-i] = s1[i];

}

}

-------------------------------------------------------------------------------------------------------------

// dec2binconv.c

#include "dec2bin.h"

int main()

{

int x=0;

char arr[8];

printf("Enter an integer\n");

scanf("%d", &x);

dec2bin(x,arr);

printf("%s\n",arr);

return 0;

}

--------------------------------------------------------------------------------------------------------------------------------------------------------

command for run program on terminal

gcc dec2binconv.c dec2bin.c

./a.out

-----------------------------------------------------------------------------------


Related Solutions

JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
Problem 4 : Write a program that prompts the user to enter in an integer and...
Problem 4 : Write a program that prompts the user to enter in an integer and then prints as shown in the example below Enter an integer 5 // User enters 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Bye
Write a program which: Prompts the user for a positive integer >= 0 Validates the user...
Write a program which: Prompts the user for a positive integer >= 0 Validates the user input to ensure it is a positive integer >= 0 Allocate (dynamically) an array big enough for the data. Load the array with random numbers ranging in value from1 to 100 Display the elements of the array (unsorted) Display the elements of the array (sorted) Display the average Display the median Display the mode, if none, display appropriate message RESTRICTIONS No global variables No...
Problem 1 Write a program that prompts the user to enter an integer It then tells...
Problem 1 Write a program that prompts the user to enter an integer It then tells the user if the integers is a multiple of 2, 3, 5, 7 or none of the above. Program language is C Ex. Enter an integer 12 You entered 12 The number you entered is a multiple of 2 ----------------------------------------------- Enter an integer 11 You entered 11 The number you entered is not a multiple of 2, 3, 5, or 7
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
Write a program that prompts the user to enter an integer from 1 to 15 and...
Write a program that prompts the user to enter an integer from 1 to 15 and displays a pyramid, as shown in the following sample run: here............THE PYRAMID HAS TO BE THIS SHAPE AND IT IS DONE IN JAVA PLEASE 7 6 5 4 3 2 1 2 3 4 5 6 7 6 5 4 3 2 1 2 3 4 5 6 5 4 3 2 1 2 3 4 5 4 3 2 1 2 3 4...
Write a program which prompts the user for a positive integer, and then prints out the...
Write a program which prompts the user for a positive integer, and then prints out the prime factorization of their response. Do not import anything other than the Scanner. One way you might go about this using nested loops: Start a "factor" variable at 2 In a loop: repeatedly print the current factor, and divide the user input by it, until the user input is no longer divisible by the factor increment the factor This plan is by no stretch...
In Java: Write a program that prompts the user to enter a positive number until the...
In Java: Write a program that prompts the user to enter a positive number until the user input a negative number. In the end, the program outputs the sum of all the positive numbers. You should use do-while loop (not while, not for). You cannot use break or if statement in the lab. Hint: if the program adds the last negative number to your sum, you can subtract the last number from the sum after the loop.
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value....
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value. The program should then output a message saying whether the number is positive, negative, or zero.
Write a C++ program which prompts the user to enter an integer value, stores it into...
Write a C++ program which prompts the user to enter an integer value, stores it into a variable called ‘num’, evaluates the following expressions and displays results on screen. num+5, num-3, (num+3) – 2, ((num+5)*2 / (num+3)) For performing addition and subtraction, you are allowed to use ONLY the increment and decrement operators (both prefixing and postfixing are allowed). You must remember that using increment/decrement operators changes the original value of a number. Indent your code and include comments for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT