C code explanation needed [closed] - c

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 8 years ago.
Improve this question
Can someone explain the following code?
what is the use of the variable ort?
void squart_root(double a)
{
if (a>0.0){
double root = 1, ort = 0;
while(ort!=root)
{
ort = root;
root = ((a/root) + root) / 2;
}
printf("Root is : %lf",root);
}else if(a==0.00000){
printf("Root is : %lf",a);
}else{
printf("Cannot find the square root of a negative number");
}
}

This looks similar to Newton's method of calculating square root (derived from Newton-Raphson's method).
You can read more about this here.
It looks like:
X(n+1) = (A/X(n) + X(n))/2
It converges when X(n) = X(n+1) (that is in your case) or under some precision.
At each iteration, X(n) should get closer to the real root.
The purpose of ort is to hold X(n) and check for this convergence. When root converges, loop terminates. That is why ort is here.
You can read about Newton-Raphsone method (just google it) and you will be able to derive this equation.

Related

Why does this C program give me an missing integer from an For loop? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
I am trying to get the value of "w" but when I do the For loop ,it only gives me 4 results:
Problem:
https://imgur.com/a/F9wPKF7
Code:
#include <stdio.h>
#include <math.h>
int main() {
int x,e,w;
float a = 2.5;
for (x=1; x<6; x++) {
if (x>a) {
w = x*cbrt(x-a);
}
else if (x=a) {
w = x*sin(a*x);
}
else if (x<a) {
w = pow(e,(-a*x))*cos(a*x);
}
printf("%d ",x);
}
return 0;
}
Output:
2 3 4 5
I know in this program i dont search for the value of w but I wanted to see why does it give me only 4 number instead of 5? why is the one missing and ow can I solve it? Thank you
else if (x=a) should beelse if (x==a)

Using Regular Expression to extract word in a string in C [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 6 years ago.
Improve this question
I'm trying to extract a small substring from a string of words in c. I did it in a previous program written in python using this code:
out_block = "".join(re.findall(r'.FT off(.*?).FT on',finaltext,re.DOTALL))
The code extracts all the characters in between .FT off and .FT onand passes it to out_block as a string.
I wanted to know how to do the same using regular expressions but in C
How would I convert this code to a C code that does the exact same thing?
Basicly you do this:
#include <sys/types.h>
#include <regex.h>
regex_t finder;
if (0 != regcomp(&finder, "regex-string", 0)) { error handling }
You might need to adjust your regex string above.
After that you can do any number of times:
regmatch_t match[N_MATCH];
if (0 == regexec(&finder, "haystack", N_MATCH, match, 0)) {
process matches
} else {
no match
}
Clean up using regfree and get error messages (in regcomp) using regerror.
For more details, consult man page regex(3).

thread-safe variables comaprison C VS2010 [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 7 years ago.
Improve this question
threaded programming
I want to write simple multi-thread app.
where each thread when it open I increment (using InterlockedIncrement) member by one ,
and decrements it (using InterlockedDecrement) when thread finishes
I know about Mutex/Semaphore/event
but I would more clean /simple way to implement comparison similar to the Interlocked function .
What I need next is implement comparison function [if(member == x)]
simple example:
Thread 1 function:
{
//do somthing
InterlockedDecrement(member);
}
Thread 2 function:
{
//do something else
InterlockedDecrement(member);
}
main thread function :
{
while(member)//<--how can it be done in thread safe fashion
{
//do yet another something
}
}
Use InterlockedAdd and add 0. This will lock the member and return the value without changing it:
while (InterlockedAdd(&member, 0) == someValue)
{
//do yet another something
}

How does this program work C? [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 7 years ago.
Improve this question
void fade(ImagenBMP *imagen1, ImagenBMP *imagen2, int f) {
float v = (float)f/255;
for (int i = 0; i < imagen1->tamanyo; i++) {
imagen1->datos[i] = (imagen1->datos[i] - imagen2->datos[i])*v + imagen2->datos[i];
}}; //end of fade
It's supose to fade two images into a single one.
If you rewrite the equation, you will end up with:
imagen1->datos[i] = v*imagen1->datos[i] + (1-v)*imagen2->datos[i];
This is how the blend works. You are specifying how much of the first image (out of 255 parts) should be included in the function parameter f, 255-f parts will be included from the second image.

How to modify the function to satisfy OCP? [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
The following function is implemented in C:
function(struct_XX *p)
{
if(p->A)
{
if(p->B)
{
do something0;
}
if(p->C)
{
do something1;
}
if(p->D && p->E)
{
do something2;
}
if(p->Z)
{
do something3;
}
}
}
each branch has different things to do,It doesn't satisfy Open-Closed Principle(because the struct which p points at is not stable ,new fields will be added into it often,that means new process codes will be added into function frequently); how can it be modified to satisfy OCP?
you haven't given much to go, you have a bunch of conditions, and a bunch of "do"s based on those conditions.
So given that... you can make something that stores a collections of conditions and callbacks....
void ocpfunction(struct struct_XX *p)
{
int i;
for(i=0; i<p->conditions_count; i++)
{
if(p->conditions[i].evaluate(p))
{
p->conditions[i].callback(p, p->conditions[i].context);
}
}
}

Resources