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 years ago.
Improve this question
I had this task :
Given the following array, write a program that reverses all array elements then prints them.
int x[] = {1,2,3,4,5,6,7,8,9,10};
then i wrote that code :
int main() {
int x[] = {1,2,3,4,5,6,7,8,9,10};
int y[10] ;
int i;
i=0 ;
for(i=0 ; i<10 ; i++) {
x[i]=y[9-i] ;
}
printf("\r\n The reversed array is : \r\n{ ") ;
i=0 ;
for(i=0 ; i<10 ; i++) {
printf("%d \t ,",y[i] ) ;
}
printf("}") ;
return(0) ;
}
and the output was this
what is wrong with the code ?
The problem is in this line:
x[i]=y[9-i] ;
You are assigning to x; however, x is your input data. The y array is uninitialized, so when you assign values from it to x, you get garbage data. This is why generic variable names like x and y are generally discouraged; it's easy to mix them up and make mistakes like this.
Anyway, if you switch that around, and assign the values from x to y instead, it should fix your problem.
Related
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 1 year ago.
Improve this question
I can't imagine a simpler case of matrix initialization, and yet it goes horribly wrong, with the program getting into an endless loop. WHY????
#include <stdio.h>
int main ( void )
{
int M [ 5 ] [5] = {{}} ;
int i , j ;
for ( i = 1 ; i <= 5 ; i ++ )
{
for ( j = 1 ; j <= 5 ; j ++ ) M [ i ] [ j ] = 1 ;
}
return 0 ;
}
Indexes in arrays start from zero.
So the code should be
#include <stdio.h>
int main(void)
{
int M[5][5] = {{}} ;
int i, j ;
for (i = 0; i < 5; i ++ )
{
for (j = 0 ; j < 5 ;j ++ ) M[i][j] = 1 ;
}
return 0 ;
}
Not only, at stated above, indexes start with 0, not 1, which would lead you to write
for (i=0; i<5; i++)
… instead of :
for (i=1; i<=5; i++)
… (note the < sign instead of <=) but if you do it anyway, nothing will prevent you from doing. Since the machine code produced by your C compiler sticks to adding the index to the base pointer, you'll exceed the end of your array, thus leading to a buffer overflow.
And what lies after the array you declared are your index variables i and j. Since you fill your array with ones only, you'll write 1 into your index variables each time you run out of your array, hence resetting it to this particular value, and that's why your loop will never end.
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 years ago.
Improve this question
I am trying to learn C but my code is not running properly.It always gives fatal error.I think there is a problem in for loop.How can i fix it?
#include<stdio.h>
int main( void )
{
int a ;
int b = 1 ;
int i = 0 ;
printf("Enter a number:");
scanf("%d",&a);
if(a=0)
printf("Factorial=1");
else if (a > 0){
for(i=1 ; i<=a ;i++){
b = 1;
b *= i;
}
printf("Factorial=%d",b);
}
else
printf("FATAL ERROR");
return 0;
}
if(a==0) Not assignment use comparison.
You wanted to use the comparison but you ended up using assignment.
if(a=0) is same as if(0) so else part is executed.1
But that part also looks for a>0 which is not the case.
So it prints FATAL ERROR.
1. This happens because the result of an assignment expression is the value of the expression
What do I need to do to calculate factorial?
fact(0)=1
fact(1)=1
fact(n)=n*fact(n-1);
so you will do something like
for(int i=1;i<=a;i++)
b*=i;
You don't need that b=1 part because it is making everything 1. So your calculated value is not retained.
So the complete corrected code will be
#include<stdio.h>
int main()
{
int a;
int b = 1 ;
int i = 0 ;
printf("Enter a number:");
scanf("%d",&a);
if(a=0)
printf("Factorial=1");
else if (a > 0){
for(i=1 ; i<=a ;i++){
b *= i; // don't overwrite value of b with 1
}
printf("Factorial=%d",b);
}
else
printf("FATAL ERROR");
return 0;
}
There are two major issues.
First being your use of if(a=0) which results in assignment in place of comparison which is achieved by if(a==0).
Secondly
for(i=1 ; i<=a ;i++)
{
b = 1;
b *= i;
}
This piece of code is faulty too. You are making b=1 after every iteration which overwrites the intermediate results Hence resulting in incorrect answer. As you have already initialized b to 1 in your code, you do not need this line at all.
for(i=1 ; i<=a ;i++)
{
b *= i;
}
Also it is worth mentioning that the reason you are always getting fatal error is because of what you are doing in the if condition. Due to if(a=0), variable a is assigned value 0 . Hence not satisfying both if and else if conditions and resulting in the execution of else block every time.
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 6 years ago.
Improve this question
this is a simple question as i'm a new-comer to C. I am trying to write a script for outputting an array of the tangents of radians, of multiples of 5 from 0-60. but for some reason the for loop i have written only does this for the first element, and all other elements in the resulting array are 0.00, and it wont print them for each loop. i'm sure i've done something simple wrong with my loop, but i just can't see it.
#include <stdio.h>
#include <math.h>
float rad(float degree){
return degree*M_PI/180;
}
int main(void){
int i, j, dim=13;
float Tan[dim];
for(i=0; i<13; i++);{
j+=5;
Tan[i]=tan(rad(j));
printf("%f\n", Tan[i]);
}
return 0;
}
First of all, in your code
j+=5;
is undefined behavior, as the intial value of j is indeterminate. To elaborate, j is an automatic local variable and not initialized explicitly, so the content is indeterminate.
Then, the for loop is also buggy.
for(i=0; i<13; i++);
should be
for(i=0; i<13; i++) // no ; here
to have a meaningful loop body to be executed.
1. You have inserted a semi-colon which you shouldn't have. Change your loop to :
for(i = 0; i < 13; i++){ //erase the ; after the parenthesis
j+=5;
Tan[i] = tan(rad(j));
printf("%f\n", Tan[i]);
}
2. Initialize variable j before trying to increase it with the statement j+=5, as this will lead to undefined behaviour.
There are two problems in your code :-
1) You haven't initialized j here int i, j, dim=13;
2) The way you used for loop is as per your requirement.Remove semicolon from the for loop statement.
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 8 years ago.
Improve this question
I'm writing a function that tries to get the square of a root.
I guess it's easier with an example:
I want to give a number to that function, say 1024 and the function should tell me 12. So always looking for the x here: 1024 = 2^x.
If I gave 255 to the function it should return 7.
Now I guess my maths is pretty okay, but I get an Error saying I didn't use a Variable in Line 6. Could you have a look?
int log_base2(int num)
{
int x = 2;
int count = 0;
for(; x <= num; x * 2 )
{
count++;
}
return count;
}
Error is in line 6 ( for(....))
for(; x <= num; x * 2 )
Here x * 2 calculates its value, and then throws the result out. What you want is probably:
for(; x <= num; x *= 2 )
The error message is perhaps because the compiler optimizes the variable x away as it's useless.
You are not modifying x anywhere. If you want x to become 2 * x in the next iteration you have to change this
for(; x <= num; x * 2 )
to
for(; x <= num; x = 2 * x )
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 following code there is declaration even before the block beginning of the main function. Is this allowed?
long long n,u,m,b;
main(e,r)
char **r; //<<<Is this possible???
{
for( ; n++ || (e=getchar()|32)>=0 ; b="ynwtsflrabg"[n%=11]-e?b:b*8+n)
for( r=b%64-25 ; e<47&&b ; b/=8)
for( n=19; n ; n["1+DIY/.K430x9G(kC["]-42&255^b||(m+=n>15?n:n>9?m%u*~-u:~(int)r?n+!(int)r*16:n*16,b=0))
u=1ll<<6177%n--*4;printf("%llx\n",m);
}
Source: I found this code on ioccc.org
Yes, C allows declarations outside of functions. These declarations define global or static variables (you need a static modifier for that).
Re-formatting your program produces this:
long long n,u,m,b;
main(e,r)
char **r; // Pre-ANSI parameter declarations; do not do that in new programs!
{
for( ; n++ || (e=getchar()|32)>=0 ; b="ynwtsflrabg"[n%=11]-e?b:b*8+n)
for( r=b%64-25 ; e<47&&b ; b/=8)
for( n=19; n ; n["1+DIY/.K430x9G(kC["]-42&255^b||(m+=n>15?n:n>9?m%u*~-u:~(int)r?n+!(int)r*16:n*16,b=0))
u=1ll<<6177%n--*4;printf("%llx\n",m);
}
There's some serious obfuscation going on here, but syntactically it's valid code.