How does this program work C? [closed] - c

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.

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)

How do I resolve this int overflow? [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
int frequency(string note)
{
int i;
float f;
int n=0;
float octave= note[strlen(note)-1];
if(strlen(note)==3)
{
if(note[1]=='#')
{
n+=1;
}
else if(note[1]=='b')
{
n-=1;
}
}
if(note[0]=='B')
{
n+=2;
}
else if(note[0]=='C')
{
n-=9;
}
else if(note[0]=='D')
{
n-=7;
}
else if(note[0]=='E')
{
n-=5;
}
else if(note[0]=='F')
{
n-=4;
}
else if(note[0]=='G')
{
n-=2;
}
n+=(octave-4.0)*12.0;
float p= n/12.0;
f=(int)(round(pow(2.0,p)*440.0));
return f;
}
So basically whenever I run this code I get an error stating "runtime error: value 7.3641e+16 is outside the range of representable values of type 'int'"
Then the value returned is just-2147483648. I've looked it up online and haven't found an answer that helps me with my code. Also this was made in the cs50 IDE so there are a bunch of commands and things that are imported. My program compiles properly and it can run so how do I fix this?
Frequencies of notes of the chromatic scale are not integers in the first place.
If you are passed a two character string like Cb, you get a stupidly huge octive with a frequency way too high.

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).

How to get the following output in C programming [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
A logical question asked in the aptitude test.
if(______)
{
printf("Hi");
}
else
{
printf("Hello");
}
What condition should be provided in place of _____ to get the following output ?
HelloHello
You do not need to give control to else twice. All you need is a false condition with a side effect of printing the other "Hello", for example
if (printf("Hello") == 0) // this condition is false, because printf returns the number of chars written
{
printf("Hi");
}
else
{
printf("Hello");
}

Creating a new file with a different extention [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
In my project I have one file as input, and I need the output file with a new extension. Suppose my input file is file.txt here as name. I need output file as file.dic. I tried the following.
fpout = fopen(strcat(name,".dic"), WRITE_ONLY);
I know it's not the correct way. But what should I do to get the file name as file.dic?
char * newstr;
newstr = strstr (name,"txt");// name = "file.txt"
strncpy (newstr,"dic",3);
//newstr = file.dic
this will replace the file.txt with file.dic
If you just want to change the file extension, this is an easy way to do it.
int lenght;
lenght = strlen(name);
name[lenght - 1] = c;
name[lenght - 2] = i;
name[lenght - 3] = d;
name[lenght - 4] = .;
you can also use while, and while you don't check a '.' increment i, then change the extension in the same way.
or
strncpy (strstr (name,"txt"),"dic",3);

Resources