|
Функция за определяне на знак, цяла и дробна част на реално число
|
Дата | 19.02.2017 | Размер | 18.06 Kb. |
|
Функция за определяне на знак, цяла и дробна част на реално число
#include
#include /* For the function floor */
void split(float a, char *s, int *w, float *f)
/* Параметри: a – a real number, s – sign, w – whole, f – fraction*/
{ float absval;
if(a < 0)
*s = '-';
else if (a == 0)
*s = ' ';
else
*s = '+';
absval = fabs(a);
*w = floor(absval);
*f = absval - (*w);
}
void main()
{ float x, fx;
char sx;
int wx;
printf("\n Input a number: \n");
scanf("%f", &x);
split(x, &sx, &wx, &fx);
printf("\n Sign: %c", sx);
printf(" Whole: %d", wx);
printf(" Fraction: %f", fx);
}
/* Function with output parameters as actual variables */
/* Problem: To order 3 variables using function which odrderes two variables */
/* main --> order --> two */
#include "stdio.h"
#include "math.h"
/* Orders two variables */
void two(float *sm, float *gt)
{
float temp;
if(*sm > *gt){
temp = *sm;
*sm = *gt;
*gt = temp;
}
}
/* Orders three variables */
void order(float *a, float *b, float *c)
{
two(a,b);
two(b,c);
two(a,b);
}
int
main(void)
{
float x, y, z;
printf("\n Input three values : \n");
scanf("%f %f %f", &x, &y, &z);
order(&x, &y, &z);
printf("\n Ordered values : %f, %f, %f\n", x, y, z);
return(0);
}
/* Function to calculate the max value and index of array */
#include "stdio.h"
#include "math.h"
#define DIM 10
void maxi(int n, float a[], float *max_val, int *index)
{
int i,i_max;
float max;
max = a[0];
i_max = 0;
for (i=1; iif(a[i] > max){
max = a[i];
i_max = i;
}
printf("\nFunction results: Max = %f, Index = %d", max, i_max);
*max_val = max;
*index = i_max;
}
int main(void)
{
int nx, i, x_index;
float x[DIM], x_max;
do {
printf("\n\nBroi elementi =");
scanf("%d",&nx);
}while(nx<1 || nx > 10);
for (i=0; iprintf("x[%1d] = ", i);
scanf("%f", &x[i]);
}
maxi(nx, x, &x_max, &x_index);
printf("\nMax value = %f, Index = %d", x_max,x_index);
return(0);
}1>
Поделитесь с Вашими друзьями: |
|
|