Change temperature F to C is having issues - c

I'm just started learning C programming. So I have problem with change temperature via Functions. Please check this program and where did I do mistake? Thank you!!!
#include<stdio.h>
double f_to_c(double f);
double get_integer(void);
int main(void)
{
double a;
a = get_integer();
printf("he degree in C:%f", f_to_c(a));
return 0;
}
double get_integer(void)
{
double n;
printf("Enter the variable:");
scanf_s("%f", &n);
return n;
}
double f_to_c(double f)
{
int f, c;
c = 5.0 / 0.9*(f - 32.0);
return c;
}
`

In your case,
double f_to_c(double f)
{
int f, c;
c = 5.0 / 0.9*(f - 32.0);
return c;
}
int f is shadowing the double f. Use some other name for the variable(s).
Essentially, you're trying to make use of an uninitialized automatic local variable which invokes undefined behavior

Corrected code below:
#include<stdio.h>
double f_to_c(double f);
double get_integer(void);
int main(void)
{
double a;
a = get_integer();
printf("he degree in C:%lf\n", f_to_c(a));
return 0;
}
double get_integer(void)
{
double n;
printf("Enter the variable:");
scanf("%lf", &n);
return n;
}
double f_to_c(double f)
{
double c;
c = 5.0/9*(f-32);
return c;
}
As you can see:
Type of c variable in f_to_c is changed to double, because of you need a double return for that function
Formula to convert F to C was not correct.
Format specifier of your printf and scanf was not correct.

in get_integer function return type is double, instead it should be integer
int get_integer(void)
{
int n;
printf("Enter the variable:");
scanf_s("%d", &n);
return n;
}
in your other function f_to_c, return type is double but you're returning an integer
double f_to_c(double f)
{
int f;
double c;
c = (5.0 / 0.9)*(f - 32.0);
return c;
}
also at the start, you need to change your code to:
int main(void)
{
int a;
a = get_integer();
printf("he degree in C:%f", f_to_c(a));
return 0;
}

Related

Bisection method. Optimise function in C code

I have a comment from my teacher related to my code for bisection method. He said "The number of calculations of the function is not optimised. In fact, on each iteration, the function is calculated three times, although it would be enough once."
Could you please help me to optimise the calculations. I actually don't even see at which point the function is calculated 3 times.
#include<stdio.h>
#include<math.h>
#include<time.h>
double f(double x); //Function
int res(int i, double a, double b, double ξ, double ε1, double ε2); //Print result
double Bisection(double a, double b, double ε1, double ε2); //Bisection method
int main()
{
double a=-10, b=0, ξ, h=0.5, α, x1, x2, ε1, ε2;
int i=0;
printf("\nf(x) = 2^x - 2 * cos(x)");
printf("\nStart of the interval a = %.0lf", a);
printf("\nEnd of the interval b = %.0lf", b);
printf("\nEnter error ε1 for function = ");
scanf("%lf", &ε1);
printf("Enter error ε2 for argument = ");
scanf("%lf", &ε2);
printf("\n\nSOLUTION:");
//selection of roots
x1=a;
x2=x1+h;
while (x2<=b)
{
if ((f(x1)*f(x2))<0)
{
i++;
printf("\n\n%d) %d root of the function is in the interval [%.1f, %.1f]\n",i, i, x1, x2);
printf("\nn\t a\t\t b\t\t ξ\t f(ξ)\t ε1\t\t ε2\n");
Bisection(x1,x2,ε1,ε2); //Bisection method
}
x1=x2;
x2=x1+h;
}
return 0;
}
//Function
double f(double x)
{
double y;
y=pow(2,x)-2*cos(x);
return y;
}
//Print result
int res(int i, double a, double b, double ξ, double ε1, double ε2)
{
printf("%d\t%10.7f %10.7f %10.7f %10.7f %e %e\n", i, a, b, ξ, f(ξ), ε1, ε2);
return 0;
}
//Bisection method
double Bisection(double a, double b, double ε1, double ε2)
{
double ξ=(a+b)/2; //Middle of the interval
double α;
int i=0;
if (f(ξ)==0)
{
printf("Root: %f \n\n", ξ);
}
else
{
while ((fabs(f(ξ))>ε1) && ((fabs(b-a)/2)>ε2)) //The accuracy of the definition of the root
{
if ((f(a)*f(ξ))<0)
{
b=ξ;
}
else
{
a=ξ;
}
ξ=(a+b)/2;
res(i+1, a, b, ξ, ε1, ε2); //Print results
i++;
}
printf("Root ξ=%.7f found after %d iterations\n", ξ, i);
printf("Function f(ξ)=%.10f found after %d iterations\n", f(ξ), i);
}
return 0;
}
Results
Only including relevant lines
Replace:
double Bisection(double a, double b, double ε1, double ε2)
{
double ξ=(a+b)/2;
if (f(ξ)==0)
else
{
while ((fabs(f(ξ))>ε1) && ((fabs(b-a)/2)>ε2))
{
if ((f(a)*f(ξ))<0)
ξ=(a+b)/2;
}
}
with
double Bisection(double a, double b, double ε1, double ε2)
{
double ξ=(a+b)/2;
double val = f(ξ); // Here is the magic
if (val==0)
else
{
while ((fabs(val)>ε1) && ((fabs(b-a)/2)>ε2))
{
if ((f(a) * val )<0)
ξ=(a+b)/2;
val = f(ξ); // And here
}
}
It's not really some secret trick we're talking about. It's only about saving the return value in a variable and use that variable instead of a function call whenever possible. If the argument to the function changes, then you need to execute the function again. Here is a simple example that makes a string uppercase.
void uppercase(char *str) {
// Bad, because it's not necessary to calculate the string
// length every iteration.
for(size_t i=0; i<strlen(str); i++)
str[i] = toupper(str[i]);
}
// Instead, do this
void uppercase(char *str) {
size_t len = strlen(str); // Precalculate and reuse in loop
for(size_t i=0; i<len; i++)
str[i] = toupper(str[i]);
}

levenshtein ALWAYS infinite loop recursive C

Liechtenstein in c programming always return infinite loop this is my code i try many solution and i try to stock variables and use pointers but always i have the infinite loop i think it's because the 3 recursive calls but in the doc of Liechtenstein algorithm i found this implementation
#include "distance.h"
#include "sequence.h"
#include "string.h"
#include <stdarg.h>
int max(int a, int b){
if (a>b) return a;
return b;
}
float min(float a,float b, float c){
if((a<b) && (a<c)) return a;
if((c<b) && (c<a)) return c;
return b;
}
float dif(int a,int b){
int d;
d = a-b;
if((a==32)||(b==32)) return 1.5; //verification d'espaceC
if(d==0.0) return d;
if((abs(d)==17.0) || (abs(d)==6.0)) return 1.0;
return 2;
}
float distance_D1(SEQUENCE S1, SEQUENCE S2){
float d = 0.0; int a,b; int m; int i;
m = max(S1.l,S2.l);
for(i=0;i < m; i++){
a = S1.tc[i];
b = S2.tc[i];
d = d + dif(a,b) ;
printf("%.1f\n",d);
}
return d;
}
float DistanceMinRec(char* a,char* b,int n,int m){
printf("%s \n",a);
printf("%s \n",b);
printf("%d \n",n);
printf("%d \n",m);
float l,k,j,d;
if (m <= 0) return n;
// If second string is empty, the only option is to
// remove all characters of first string
if (n <= 0) return m;
// If last characters of two strings are same, nothing
// much to do. Ignore last characters and get count for
// remaining strings.
if (a[m] == b[n]) {
return DistanceMinRec(a, b, m-1, n-1);
}
// If last characters are not same, consider all three
// operations on last character of first string, recursively
// compute minimum cost for all three operations and take
// minimum of three values.
l=DistanceMinRec(a, b, m, n-1)+dif(a[m],b[n-1]);
k=DistanceMinRec(a, b, m-1, n)+dif(a[m-1],b[n]);
j=DistanceMinRec(a, b, m-1, n-1)+dif(a[m-1],b[n-1]);
d= min (l , // Insert
k, // Remove
j // Replace
);
return d;
}
the main file
int n=strlen(S1.tc);
int m=strlen(S2.tc);
char* X = S1.tc;
char* Y = S2.tc;
// char* X = "sunday";
// char* Y = "saturday";
//affiche_sequence(S1);
//affiche_sequence(S2);
d = DistanceMinRec(X,Y,n,m);
printf("Distance entre %s et %s = %.1f\n", argv[1],argv[2],d);
exit(0);
i think that the flush of variable is the problem i comment the block of printf in recursive function and it work but im not sure that is the correct output

Function average and stdDev const int tab[ ]. Average problems

I have to use:
float average(const int tab[], int size);
float stdDev(const int tab[], int size);
to printf average and stdDev in C.
I have problem with average and i think with const int.
When i add const int tab[101] i have error with a1;
So how can i make it work with const int (if i can).
And if it is anything wrong with this code.
Any help will be helpful.
#include<stdio.h>
#include<math.h>
float average(const int tab[], int size);
float stdDev(const int tab[], int size);
int main()
{
float ave, std;
int a1;
int j;
int tab[101];
printf("Podaj liczby: ");
for(j=0; j<=99; j++)
{
a1 = scanf("%d", &tab[j]);
if(a1<1)
{
printf("Incorrect input");
return 1;
}
if(tab[0]==0)
{
printf("not enough data available");
return 2;
}
if(tab[j]==0)
{
break;
}
}
ave = average(tab, j);
printf("%.2f\n", ave);
std = stdDev(tab, j);
printf("%.2f", std);
return 0;
}
float average(const int tab[], int size)
{
int i;
float y=0, x;
if(size<=0)
{
return -1;
}
for(i=0; i<size; i++)
{
x = x + tab[i];
}
y = x/size;
return y;
}
float stdDev(const int tab[], int size)
{
int i;
float y, z, z1, z2=0, z3=0;
if(size<=0)
{
return -1;
}
y = average(tab, size);
for(i=0; i<size; i++)
{
z = tab[i] - y;
z1 = pow(z, 2);
z2 = z2 + z1;
z=0;
z1=0;
}
z3 = sqrt(z2/size);
return z3;
}
You define the variable x in average here:
float y=0, x;
without giving it a value. Then here:
x = x + tab[i];
you are reading its value without setting it anywhere beforehand. Because you never gave x a value, its value will be indeterminate and reading it will cause undefined behavior, which means that your program could e.g. print garbage output.
Always initialize your variables:
float y=0, x=0;

How to print this series x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!) in C/C++?

I wish to write a program which calculates the series x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!) by taking x and n as user inputs.
This is what i've tried, and well there's no output when I enter the values for x,n:
#include<stdio.h>
#include<math.h>
//#include<process.h>
#include<stdlib.h>
double series(int,int);
double factorial(int);
int main()
{
double x,n,res;
printf("This program will evaluate the following series:\nx-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)\n");
printf("\nPlease enter a value for x and an odd value for n\n");
scanf("%lf%lf",&x,&n);
/*if(n%2!=0)
{
printf("Please enter a positive value!\n");
exit(0);
}*/
res=series(x,n);
printf("For the values you've entered, the value of the series is:\n %lf",res);
}
double series(int s, int t)
{
int i,sign=1; double r,fact,exec;
for(i=1;i<=t;i+2)
{
exec=sign*(pow(s,i)/factorial(i));
r+=exec;
sign*=-1;
}
return r;
}
double factorial(int p)
{
double f=1.0;
while(p>0)
{
f*=p;
p--;
}
return f;
}
When I enter values for x and n, it simply shows nothing.
While I've written in C, C++ solutions are also appreciated.
Output window in code::blocks
The loop
for(i=1;i<=t;i+2)
in the function series() is an infinite loop when t >= 1 because i isn't updated in the loop. Try changing + to += and use
for(i=1;i<=t;i+=2)
instead. Also it seems you should use type int for x and n in the function main() because the arguments of series() is int. Don't forget to change the format specifier when changing their types.
Thanks to all those who helped. Here's the final working code:
#include<stdio.h>
#include<math.h>
#include<process.h>
#include<stdlib.h>
double series(int,int);
double factorial(int);
int main()
{
int x,n; double res;
printf("This program will evaluate the following series:\nx-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)\n");
printf("\nPlease enter a value for x and an odd value for n\n");
scanf("%d%d",&x,&n);
if(n%2==0)
{
n=n-1;
}
res=series(x,n);
printf("For the values you've entered, the value of the series is:\n%lf",res);
}
double series(int s, int t)
{
int i,sign=1; double r=0.0,fact,exec;
for(i=1;i<=t;i+=2)
{
exec=sign*(pow(s,i)/factorial(i));
r+=exec;
sign*=-1;
}
return r;
}
double factorial(int p)
{
double f=1;
while(p>0)
{
f*=p;
p--;
}
return f;
}
in loop we step by two for getting odd numbers.by multiplying the current temp variable by the previous temp variable in the loop with neccesary terms like x square and dividing by i*(i-1) i.e for factorial and multiply with -1 i.e to achive negavtive number alternatively. by using this temp variable and adding it to sum variable in every iteration will give us answer.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n, x;
cout << "enter x and no.of terms: ";
cin >> x >> n;
float sum = 0, temp = x;
for (int i = 3; i < 2 * n + 2; i = i + 2)
{
temp = ((-1 * temp) *(x*x)) / i*(i-1);
sum = sum + temp;
}
cout << x + sum;
return 0;
}
// series x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)
#include<stdio.h>
#include<math.h>
double factorial (int);
double calc (float, float);
int
main ()
{
int x, deg;
double fin;
printf ("x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)\n");
printf ("Enter value of x\n");
scanf ("%d", &x);
printf ("highest degree in denom i.e., 1 or 3 or 5 whatever, it should be odd .\n");
scanf ("%d", &deg);
fin = calc (x, deg);
printf ("the summation of series =%1f\n", fin);
return 0;
}
double calc (float num, float res)
{
int count, sign = 1;
double rres = 0;
for (count = 1; count <= res; count += 2)
{
rres += sign * (pow (num, count) / factorial (count));
sign *= -1;
}
return (rres);
}
double factorial (int num)
{
int count;
double sum = 1;
for (count = 1; count <= num; count++)
{
sum *= count;
}
return (sum);
}

why am I getting result=0.00000

I'm trying to calculate sin and cos without using math library with taylor series witch is
sinx =∑n=0 to n=∞ (-1)^n * x^(2n+1) / (2n+1)! and this code produces whatever the input is 0,00000 for sin 1,00000 for cos where is the problem in this code
#include <stdio.h>
#include <stdlib.h>
double fakt(int);
double power(int,int);
int negative_positive(int);
double calculate_sin(int,int);
double calculate_cos(int,int);
double calculate_radyan(int);
int main() {
int degree,number;
char command;
do{
scanf("%c",&command);
if(command=='d' || command=='D'){
scanf("%d %d",&degree,&number);
double radyan = calculate_radyan(degree);
printf("%lf \n%lf ",calculate_sin(radyan,number),calculate_cos(radyan,number));
}
}while(command!='e' && command!='E');
return 0;
}
double fakt(int n){
int i;
double result=1;
for(i=1;i<n;i++)
result = result*i;
return result;
}
double power(int base, int exponent){
int i;
double result=1;
for(i=0;i<exponent;i++)
result = result*base;
return result;
}
int negative_positive(int number){
if(number % 2 == 0)
return 1;
else
return -1;
}
double calculate_sin(int degree , int n){
int i;
double result=0;
double tmp=0;
for(i=0;i<n;i++){
tmp=negative_positive(i)*power(degree,2*i+1)/fakt(2*i+1);
result=tmp+result;}
return result;
}
double calculate_cos(int degree , int n){
int i;
double result=0;
double tmp=0;
for(i=0;i<n;i++)
{
tmp=negative_positive(i)*power(degree,i*2)/fakt(2*i);
result=tmp+result;
}
return result;
}
double calculate_radyan(int degree){
double result,pi=3.14159;
result =pi/180*degree;
return result;
}
ı will answer my own question but ı found the solution. when program trys to go to power method with double radyan variable it converts that variable to 0 cause that method just takes integer variables power(double base,int exponent) is solved my problem

Resources