How do I create multiple answers in the output?
How do I create multiple answers in the output?
I have written a small bit of code that computes on equation for the given number, x. But, when I want to try and duplicate this code it doesn't work. It says "redeclaration of result with no linkage." What I want to do is make an output in the console for when x=0, x=10 and x=-10. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int main()
float x = 0.0, result;
result = 1/(1+exp(x));
printf("Exponential of %f = %f", x, result);
return 0;
It works for just one, but when I duplicate it won't work. What I want to do is just duplicate that so it outputs three calculations for the numbers in the console. Thank you <3
3 Answers
3
"redeclaration" means you make copy of line
float x = 0.0, result;
if you want just to make from
float x = 0.0, result;
result = 1/(1+exp(x));
printf("Exponential of %f = %f", x, result);
something to calculate result for different x
, jut make as follows:
x
float x = 0.0, result; // declaration - only once
result = 1/(1+exp(x));
printf("Exponential of %f = %f", x, result);
x = 0.5; // new value for the same variable
result = 1/(1+exp(x)); // new value for the same variable
printf("Exponential of %f = %f", x, result);
Also consider making of loop to exclude copying the code. Common approach is like:
x
start
end
for
while
dowhile
float x = 0.0
printf("Exponential of %f = %fn", x, 1/(1+exp(x));
x = 10.0
printf("Exponential of %f = %fn", x, 1/(1+exp(x));
x = -10.0
printf("Exponential of %f = %fn", x, 1/(1+exp(x));
In c language, you cannot redeclare a variable, but you can re-assign variable.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int main()
float x = 0.0, result;
result = 1/(1+exp(x));
printf("Exponential of %f = %fn", x, result);
x=10.0;
result = 1/(1+exp(x));
printf("Exponential of %f = %fn", x, result);
x=-10.0;
result = 1/(1+exp(x));
printf("Exponential of %f = %fn", x, result);
return 0;
Hope this helped.
You cannot reinitialize either, but you can reassign
– Antti Haapala
Sep 7 '18 at 5:33
@AnttiHaapala Sorry, my bad.
– Adil Saju
Sep 7 '18 at 5:48
@MakaylaRobinson You're welcome.
– Adil Saju
Sep 7 '18 at 6:01
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
When asking about a code with error it is imperative that you provide a proper Minimal, Complete, and Verifiable example in the question, i.e. the code that produces the error, and the verbatim error text including the line number and the possible position within the line where the error occurred.
– Antti Haapala
Sep 7 '18 at 5:35