LotharianBC - 5 months ago 24

C Question

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

The questions regarding this case are solved. Here is the code for a fully working program that does what the assignment requested. I print the code here for future reference, and hopefully you might find it usefull:

`/*`

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 */

int sorting_sign(int x);

double sorting_whole(double x);

double sorting_fract(double x);

/* 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("%lf", &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); /* 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 '-' */

int sorting_sign(int 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 = (int)x;

fract = x - whole;

return (fract);

}

Answer

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.