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...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
Checker for integer string Forms often allow a user to enter an integer. Write a program...
Checker for integer string Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9. Ex: If the input is: 1995 the output is: yes Ex: If the input is: 42,000 or 1995! the output is: no Hint: Use a loop and the Character.isDigit() function. import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr...
You will write a program that prompts the user to enter a 7-digit phone numbers, and...
You will write a program that prompts the user to enter a 7-digit phone numbers, and finds the 3- and 4-letter words that map to the phone number, according to the restrictions outlined earlier. A sample run: unixlab% java MapNumbers Enter name of dictionary file: words10683 Enter a test word (3 letters): cat Test word maps to 228 Enter telephone number (7 digits, no 0's or 1's, negative to quit): 2282273 Options for first 3 digits: act cat bat Options...
Forms often allow a user to enter an integer. Write a program that takes in a...
Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9. Ex: If the input is: 1995 the output is: yes Ex: If the input is: 42,000 or any string with a non-integer character, the output is: no PYTHON 3
Write a C program that prompts the user to enter some information about up to 20...
Write a C program that prompts the user to enter some information about up to 20 individuals (think of a way to welcome and prompt the user). It must be stored in a structure. Once data is entered, the program output it as shown in sample run below. Your program should include a structure with a tag name of: “information”. It should contain the following data as members: a struct to store employee's name, defined as: struct name fullname e.g....
Write a C program that asks the user to enter 15 integer numbers and then store them in the array.
Write a C program that asks the user to enter 15 integer numbers and then store them in the array. Then, the program will find the second largest element in array and its index without sorting the array. For example, In this array {-55,-2,1, 2, -3, 0, 5, 9, 13, 1, 4, 3, 2, 1, 0}, the second largest element is 9 [found at index 7].
(PYTHON) Write aprogram that prompts user to enter three sides of a triangle....
(PYTHON) Write a program that prompts user to enter three sides of a triangle. The program should determine if the three sides can form a triangle. If the three sides can form a triangle, then determine the type of the triangle.There are three types of triangles: Equilateral triangle (all 3 sides are equal) Isosceles triangle (two sides are equal, the third side is of a different length) Scalene triangle (all 3 sides are of different lengths)   The program should...
Write a program that prompts the user for their first and lastname. Display the first...
Write a program that prompts the user for their first and last name. Display the first initial of their first name and their last name to the user.Ask the user to input a phone number.The program checks which part of Colorado a phone number is from using the values below.If the second digit of the phone number is one of the below digits, print the phone number and which part of Colorado it is from. If none of the digits...
Write a program which prompts the user for the year of their birth, and which then...
Write a program which prompts the user for the year of their birth, and which then calculates their age (ignoring the month/day). Split the program into `main`, a "prompt for year" function which tests to make sure the user's input is a valid year, and a "calculate age" function which performs the actual calculation. (You can assume the current year is 2017.) for my intro to c++ class
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT