how to handle a mathematical function in C language like MATLAB [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm looking for a way to create a "variable-function" in C language.
In MATLAB i'm able to create something like:
my_function = (#x) sin(x) + x^2 + x;
so that i'm able to evaluate it for any value of 'x' i like:
my_point = 3.09;
my_function(my_point);
is there anything like that for C language?

That's just a regular C function. The terminology would be: "A function with arguments"
double my_function(double x)
{
return sin(x) + x*x + x;
}

#include <stdio.h>
#include<math.h>
#define my_function(x) sin(x) + pow(x,2) + x
int main()
{
double my_point = 3.09;
printf("%lf",my_function(my_point));
return(0);
}

Related

convert numbers in scientific notation to decimal [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Write a C program to convert a number in scientific notation to its equivalent decimal form :
Given
8.3e+2 output = 830
2.E-1 output = 0.2
4.3E2 output = 430
Is there any inbuilt library or function in C/C++ which can do this?
or one will have to write his own code, in that case, any easy way to achieve this?
try this
#include <iostream>
#include <string>
int32_t main(int32_t argc, char *argv[]) {
std::string str = "8.3e+2";
std::cout << std::stod(str) << std::endl;
return EXIT_SUCCESS;
}

Integer Overflows in C and Defenses Against Numeric Errors [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to research various integer overflow scenarios in C and I was wondering does the C language provide any defenses against numeric errors and are there any additional classes or libraries in the C language that can help with that? Also, can anyone give me an example of code that results in an integer overflow in C?
No, there are no defenses.
This overflows:
#include <limits.h>
#include <stdio.h>
const int a = INT_MAX - 2;
const int b = INT_MAX - 2;
printf("%d + %d = %d\n", a, b, a + b);
When I tested it it printed -6, but anything could happen I guess.

Cosine expression in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How can I write 3 cosĀ² 2x with C?
Assuming x is in radians :
3*pow(cos(2*x),2)
Here is a function for your purpose:
#include <math.h>
double f(double x) {
double c = cos(2 * x);
return c * c * 3;
}
First compute cos 2x, then square the result and multiply by 3.

Write c program to find mean of two numbers without using division [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I just wonder how to find mean of two numbers without using division.
do not use these conditions :
int mean = (a + b) >> 1;
four fundamental arithmetic operations
I think this may be helpful -->
int a,b,i,j;
if (a>b)
{
int temp = a;
a = b;
b = temp;
}
for(i=a,j=b;i<j;i++,j--)
continue;
if(i==j)printf("%d\n", i);
else printf("%lf\n", (double)(i)-0.5);
Add them then multiply by 0.5 , no division involved.
If they're both integers, you can use a right shift:
int median = (a + b) >> 1;

How to obtain the real output from a C function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am having difficulty getting the real output from a C function. For example:
int max3(int a, int b, int c){
if ((a>b)&&(a>c))
return a;
if ((b>c)&&(b>a))
return b;
return c;
}
Can you give me an idea of how to specify the real output (e.g. tools, algorithms, etc.)?
In this above example, the real output is 6 (in case (a,b,c) = (1,2,6)).
Thank in advance very much.
I'd have probably written it, as it is simple, using the ternary operator:
int max3(int a, int b, int c){
if (a>b)
return (a>c)?a:c;
else
return (b>c)?b:c;
}

Resources