Search code examples
csortingmodular

How to take in a number and divide it into three parts positive or negative, whole number and fractions


In a school assignment we where supposed to write a program that takes in a number and divide it in three parts: 1. Check if the number is positive or negative 2. whole number (magnitude) 3. fractional parts

The requirement is that there should be a own function called separate that has input and output parameter.

For example: if you type in 23.639, the program should sort of out and print out: Sign: + Whole number magnitude: 23 Fractional parts: 0.639

QUESTIONS: 1.The function for sorting out whether the number is positive or negative brings forth the wrong answer when typing in a negative number. It also posts wrong character. I have tried different data types, like int, char and float, but none seem to work. Any tip on how to solve this are greatly appreciated, cause I think I'm blinded by my own errors...

2.The function separating decimals from whole numbers (fractions) won't subtract the whole number away from the decimals, so I am stuck with whole number. Can anyone spot my error here?


* UPDATE *

I managed to solve the questions at hand, and did the terrible n00b error of editing the code I first posted in this question. I have now edited the code once more to hold the original errors as best as I can remember. The right code is posted as an answer below.

Sorry for the rookie error.

/*
Author: Thorbjørn Elvestad
Student ID: *****
E-mail: drommevandrer@gmail.com


This program take in number typed in by the user, and then divide it into three parts.

SIGN: '+' or '-'
Whole number: Show number as a whole number
Fraction: Show fractions 

The program uses function to sort out the number, and print out the result*/

/* Declaring libraries */
#include <stdio.h>
#include <stdlib.h>


/* Declaring functions */
double sorting_sign(char x);
double sorting_whole(double x);
double sorting_fract(double x, int y);



/* Calling main function */
int main()
{
    double num, fractures; /* declaring variables */
    int sign_sorted, part;
    double whole_sorted;

    printf("LET ME TELL YOU SOME INTERESTING STUF ABOUT YOUR NUMBER!\n\n");
    printf("Enter your number: ");
    scanf("%d", &num);

    sign_sorted = sorting_sign(num); /* Calling the function that sorts out if this number is '+' or '-' */
    whole_sorted = sorting_whole(num); /* Calling the function separating whole number from decimals */
    fractures = sorting_fract(num, num); /* Calling the function removing the whole number from the fractures */

    printf("Sign: %c\nWhole: %0.lf\nFraction: %f", sign_sorted, whole_sorted, fractures);

    return 0;
}


/* Function for sorting of if number is '+' or '-' */
double sorting_sign(char x)
{
    int sign;

    /* true if number is less than 0 */
    if(x < 0.0){sign = '-';}

    /* true if number is greater than 0 */
    else if(x > 0.0){sign = '+';}

    return (sign);
}


/* Function for sorting out the whole number */
double sorting_whole (double x)
{
    int whole;

    whole = x;

    return (whole);
}

/* Function for sorting out the fractions */
double sorting_fract(double x)
{
    int whole;
    double fract;
    whole = y;
    fract = x - whole;


    return (fract, whole);
}

Solution

  • You've declared your sorting_sign function to return a double, when you're returning an int set to the value of a char... sort your types out.