C Program Function [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I don't want an answer in the form of code, but I do need to know how to start working on this problem. I have just begun learning C language and during the learning process, I am stuck with this confusing question. The question is as follows:
Write a function to compute the distance between two points and use it to develop another function that will compute the area of the triangle whose vertices are *A(x1,y1), B(x2,y2), and C(x3,y3).* Use these functions to develop a function which returns the value 1 if the point (x,y) lines inside the triangle ABC, otherwise returns a value 0.
Please suggest me the appropriate coding for this problem in C language and kindly include comments for better understanding.
I know both the formulas by the way.

Approach this problem, like all other coding problems, in increments. Your problem statement states pretty much what you need to do:
first, write a routine to compute the distance between two points. You need to find the formula to do this. Probably easiest to use double variables, be sure to read about how to specify the format statement for printf so that it can print double variables.
next, find the formula to use in order to compute the area of a triangle.
the last step is the hardest, and you will need to use everything you learned in steps one and two. Again, dig out your calculus or algebra book and see how this is done and then write code to do the same thing. It is interesting to see how a math description gets translated to computer code, they are not the same thing and unless you write a lot comments in your code it will be difficult to see how they do the same function.
good luck.
PS. when defining a function that returns a double variable, code:
double distance_calc(double x1, double x2, double y1, double y2)
{
double computed_value;
// do the formula and compute: computed_value = etc.
return computed_value;
}
Hope this helps.

Related

Calculate using Rational Index Binomial Theorem in C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I have tried so many ways to calculate using this binomial theorem but I still couldn't find one:
The value of x and n is given for example b=0.5 and n=8
I know for the factorial we have to use loop but the numerator part is a little bit tricky.
Obviously I know how to code for (1+b)^n, but the question is still asking for the coding for binom theorem.
For example if the value of x is 0<x<1 and n is any positive integer, what will the value of (1+x)^n will be using the binomial theorem?
I understand that you know how to calculate the left side of the equation in programming.
I understand that you also know how to program the right side, apart from the problem that it is an infinite loop; but you want it to end at some point and have a result.
By the math theory ending early means a wrong result.
But in programming you will have problems with restricted precision of floating point math anyway. So you can take shortcuts to solve your problem.
In the comments you find recommendations how to do the calculation of each step efficiently. I will only focus on the end condition.
Write a loop calculating more and more precise steps.
End the loop when a freshly calculated (intermediate) result is the same as the previous one. With floating point representation having restricted precision that will sooner or later happen and the result will be within only one "minimal rounding" of the correct result.
Note:
In order to avoid the restricted precision getting in the way at the wrong place, I recommend to calculate the parts (as described in the recommendation in comments) in double and the intermediate results (those you compare for the loop condition) into a float variable.

pow used to calculate lengths of triangles [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I´m programming in c and I'm using Netbeans IDE.
I'm trying to calculate the length of the sides of two triangles from their vertexes. However, when I try to use pow to calculate a square root, I get an error. I´ve included and when I use for example pow(2,2), it works. It's just this specific example that doesn't work. I've checked the parentheses like five times and they seem correct to me.
my code looks like this
(Don't mind the other variables, so far I'm just working with a1, a2, b1, b2 and c.)
You are using (1/2) as exponent in your call to the function pow. This is integer division and the result of 1/2 is 0. So this will not give you the square root.
Use 0.5 instead as exponent in the call to pow.
(1/2) will give you an integer value. and you need double.
you can use one of the following ways to avoid this situation.
(1.0/2) // (double/int) = double
// or (1/2.0)
((double)1/2) // 0.50000
// or ((double)1/(double)2)
Useful links :
C - Type Casting
Type Casting - C Programming

Code that is supposed to solve Laplace's equation iteratively - numbers stored in array change unexpectedly to random large numbers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I've written a program in C that iteratively solves Laplace's equation on a square domain by successive over-relaxation, but although it was working perfectly a few days ago, I've come back to it and now it's doing really weird things.
The program involves creating two 7x7 arrays, one to hold the values of phi (the dependent variable in Laplace's equation) and one to hold the residuals from the successive over-relaxation. The array of residuals is filled with zeroes initially because I thought that was probably a good idea, but thinking about it now it shouldn't make much difference either way.
The problem is that certain values in the array (specifically R[4][6]) jump around randomly to enormous numbers, even though I've set them to zero and then not touched them. I know this because I put another line in to output the value of the residual at (4,6) to the screen after each iteration. This problem is causing my method of checking for convergence to fail, and also somehow causing the final solution (plotted as a surface using gnuplot) to look correct except for small peaks and valleys in the surface.
I'm coding in C, Dev-C++ 4.9.9.2 is the compiler I'm using (the files are definitely saved as C files and not C++ ones too), outputting the data into a DATA file which comes up in notepad when opened, and finally using gnuplot to produce the 3D contour plots that are my results. I'm working on windows 7.
I've put the code here: https://gist.github.com/anonymous/8220425 and you need to enter 1.35 for alpha when the program asks you for it.
If anyone could help at all I would be very grateful!
printf("Residual at (4,6)=%lf\n",R[4,6]);
You mean:
printf("Residual at (4,6)=%lf\n",R[4][6]);
The issue here is that R[4,6] basically means R[6] (4,6 means: evaluate 4, then evaluate 6), which is some sort of pointer rather than a double. So it will print random-looking values depending on where your array has been allocated in memory.

Cycling through a folder and using a matlab operation on each file [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am BRAND NEW to Matlab (like, 5-minutes ago new). I need it to read some files that I have because no other program can read them. I was hoping someone could help me understand some basic matlab so that I can accomplish this. I need to go through every file in a folder and perform the function
X = multibandread(filename, size, precision, offset, interleave, byte order)
on each file. (X is an array and I need to make a different array for each file). To further complicate things, the size argument to multibandread is a 3-element vector of [height, width,N], and I'd like to get the height and width values from other files. i.e:
[size(other_file, 1) size(other_file, 2) 2].
So, I'm still very much trying to understand matlab. It seems like a powerful type of command prompt that I can write programs into? Is that accurate? Is there a way to point to each file in my program, call multibandread on it, and then move to the next file?
I know some C programming but know absolutely nothing about matlab.
Thanks for any help or really any general matlab education anyone can give!
Use the inbuilt Matlab dir command and a simple loop:
myFiles = dir('c:\MyFolder');
% Now loop through the files.
for k = 1:numel(myFiles)
X = multibandread(myFiles(k).name, size, precision, offset, interleave, byte order);
end

Evaluate Mathematical Function from String [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
Can you give me some ideas about how can I make a simple mathematical expression parser in C?
User enters a mathematical function in a string and from the string I want to create the function in C.
eg. x + sin(2*x)
-> return x + sin(2x);
Thanks in advance.
You can parse the expression based "Shunting-Yard Algorithm" http://en.wikipedia.org/wiki/Shunting-yard_algorithm. You will need to extend to handle the function calls such as sin, cos etc...
This is not a simple thing to do at all, in face, it's a hard thing. You need a full grammar parser, combined with pre-defined constants/functions (sin, log, pi, etc).
If you have no extensive previous experience with C I would disrecommend doing this, but if you really want to do this look at recursive descent parsing which is arguably the easiest way to do this (without putting a burden on the user, like reverse polish notation).
Last but not least you say you want to create a C function from the user-generated input. This is almost always a wrong thing to do - generating code from user input, instead the easiest approach is pre-processing to create a intermediate representation that can be efficiently executed.
Writing an expression parser and evaluator is one of the usual examples used when discussions parser writing techniques.
For example you could look the documentation for flex/bison or lex/yacc. That will have examples of constructing parsers/expression evaluators.
One way to do it is to use reverse polish notation for the expressions and a stack for the operands.
Some quick pseudo-code:
if element is operand
push in stack
else if element is operation
pop last 2 elements
perform operation
push result in stack
Repeat till end of expression. Final result is the only element in stack.

Resources