Thread: "is a number binary" program that uses 2 functions

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    3

    "is a number binary" program that uses 2 functions

    pretty new to coding and have an assignment that i need guiding with.
    Here's the prompt.

    I can get code that works but is not using functions and I'm struggling with how to integrate them into this. please help

    Overview: Write a program that can determine whether a user input is a binary number or not. (1, 2, 3, 8, 13)

    Write a program that accomplishes all of the following:

    • Greets the user and informs them that the program will determine whether an input from the user is a valid binary number or not (e.g., consisting of only 0’s and/or 1’s).
    • Accept an integer input from the user. Assume that the user provides a valid numeric input that will fit in the standard integer type. However, check to make sure the input was a positive number - for our purposes, any negative integer input is not a binary number.
    • Use a loop to check all of the digits of the given input to determine that they are either 0 or 1 (hint: you may want to use the modulus and division operators to process the digits one-by-one).
    • If all digits were either 0 or 1, print a message to the user stating that the input was a valid binary number. Otherwise, print a message stating that the input was not a valid binary number.
    • Test your program thoroughly. Make sure to at least the following inputs and verify the results:
      • 0: binary
      • 1: binary
      • 01: binary
      • 21001: not binary
      • -1011: not binary
      • 123: not binary
      • 10101: binary
      • Use at least two functions to solve the problem.


    Here's the code I have that is not using functions

    project 2


    NEEDS TO USE 2 FUNCTIONS, CURRENTLY DOES NOT



    Code:
    
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    
    
    int main() {
    
    
    
    
    printf("Hello, this program determines whether your input is or is not binary")
    
    
    int n;
    
    
    printf("Enter a number: ");
    
    
    scanf("%d", &n);
    
    
    if (n < 0) {
    
    
    printf("%d: not binary\n", n);
    
    
    }
    else {
    
    
    int isBinary = 1;
    
    
    int mod, t = n;
    
    
    while (t > 0) {
    
    
    mod = t % 10;
    
    
    if (mod != 0 && mod != 1) {
    
    
    isBinary = 0;
    
    
    break;
    
    
    }
    
    
    t = t / 10;
    
    
    }
    
    
    if (isBinary) {
    
    
    printf("%d: binary\n", n);
    
    
    }
    else {
    
    
    printf("%d: not binary\n", n);
    
    
    }
    
    
    }
    
    
    return 0;
    
    
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    I'd use a function to find whether the number is binary or not. I can't think of any serious use a second function would have in this simple situation. Take input from the user and check for errors maybe?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    I agree with GReaper on the two functions. You can remove the t = n statement if you use t as the formal parameter and n as the actual argument to a discriminateBinary function that includes your while statement. If you have that function return isBinary, then you can put a call to that function in place of testing the isBinary variable in your if (isBinary) statement. What is at the top of your main up to the scanf could be put into a getInput function that returns n.

  4. #4
    Registered User
    Join Date
    Mar 2018
    Posts
    3
    I'm struggling with trying to do so :/

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Post your work! Post your attempt at creating an function that return a bool and takes an int as an input parameter.

    isBinary would be a good function name.

    Edit: Add link Functions in C - Cprogramming.com

    Tim S.
    Last edited by stahta01; 04-03-2018 at 05:05 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Mar 2018
    Posts
    3
    still having no luck. could someone show me this? the original code runs but now i need to integrate two functions and I'm lost

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    If you don't know what functions are, start there. We gave you ideas on what the function may do, the implementation is up to you.
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Essentially the same code you have above, but wrapped in a function:
    Code:
    #include <stdbool.h>
    static bool isBinary(int);
    int main(void) { ... }
    
    bool isBinary(int t) { // what does t stand for?
      bool valid = true; // until proven otherwise
      while (t > 0) {
        int mod = t % 10;
        if (mod != 0 && mod != 1) {
          valid = false;
          break; }
        t = t / 10; }
      return valid; }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling C functions from a "parallel" (MPI) Fortran program
    By Nooby 1 Kenooby in forum C Programming
    Replies: 2
    Last Post: 04-25-2013, 04:45 AM
  2. Replies: 4
    Last Post: 04-19-2013, 05:11 PM
  3. Replies: 1
    Last Post: 02-27-2012, 06:38 AM
  4. Replies: 4
    Last Post: 04-02-2006, 09:31 AM
  5. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM

Tags for this Thread