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.
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 2 years ago.
Improve this question
I'm quite new to programming and I'm trying to make a simple program with a loop:
#include <stdio.h>
int a = 5;
int b = 0;
int main(void)
{
for (int i = 0; i < a, i++)
{
b++;
}
printf("%i", b);
}
However, when I try to compile I get the errors: relational comparison result unused [-Werror,-Wunused-comparison] and expected ';' in 'for' statement specifier for line 8. I've tried to look at several different sources on how to construct for loops and I just can't see what I'm doing wrong. Any help would be much appreciated.
Here's an example where you can practice interpreting the error statement. As you'll see, it says expected ';' in 'for' statement specificer. That's saying there's a place where you should have a semi-colon but you don't.
In your casse, there should be a semi-colon after the i < a. Right now, you have a comma.
I think you are using , instead of ; in the for loop after the statement.
for (int i = 0; i < a; i++)
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'm trying to fill an array with integers 0-100.
Here's my code:
int main()
{
int a[100];
for(int i = 0; i < a; i++)
{
a[i] = i;
printf("\n%d\n", a[i]);
}
}
I get this error:
comparison between pointer and integer (int and int) over the for line.
I can't figure it out. Any thoughts?
The cancel condition in your for loop is wrong. i<a You are comparing an int i variable with the pointer a that points to the memory location of the array. You would need to calculate the length of the array:
for(int i=0; i<sizeof(a)/sizeof(int); i++) {
But you could have found this solution in this 9 year old answer
Yes you are comparing pointer with an integer. This is why the error.
for(size_t i=0; i<sizeof(a)/sizeof(a[0]); i++) {
is what you wanted it to be and can be done in C language. Remember that sizeof operator results in value in size_t.
If you are not aware what that sizeof is doing - it is basically total number of bytes that the array object has - divided by each element's size - resulting in number of elements.
In case you are thinking from where did that pointer come?
Note one thing here a is converted into pointer (array decaying)to the first element - so it is nothing other than a pointer to the first element.
This should work for you,
int main()
{
int a[100];
int n = sizeof(a) / sizeof(a[0]); //Get the size of the array
for(int i=0; i<n; i++) { // loop through each elements of the array
a[i] = i;
printf("\n%d\n", a[i]);
}
}
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
today i tried to do something new,but i didn't do that correct.Would anyone be able to do that and explain why is it so like that? Thank you in advance
#include<stdio.h>
void function(int a[],int n)/*The definition of function with void type,with parameters
int a[],int n */
{
int i;// declared count,type integer//
for(i=0;i<n;i++)//count goes from 0,to <n,and increment all time while goes//
printf("%d",a[i++]);// printing on the screen integers (a[i],i=i+1)//
printf("\n");// printing the newline //
}
main()
{
int a[]={1,2,3,4,5,6,7}; // declaration of array with 7 elements //
int n=5;// declaration of variable n type integer value of 5 //
function(a,n) // calling the function with parametres a,n//
} // end of sequence //
In my case i got the result of the 1,2,3,4,because i tought that the count goes from 1,to the one number less than n=5,but the IDE show the result of 135 ,i think the problem in my way is with counter...but all advices are welcome,thanks
Please make sure you are posting properly formatted valid C code.
Note that what you get is not one hundred and thirty five, but one, three, and five. You get that because you are incrementing the loop counter twice.
Here's a working, more readable version:
#include <stdio.h>
void function(int a[],int n)
{
int i;
for(i = 0; i < n; i++)
printf("%d ",a[i]);
printf("\n");
}
int main(void)
{
int a[]={1,2,3,4,5,6,7};
int n=5;
function(a,n);
return 0;
}
replace
printf("%d",a[i++]);// printing on the screen integers (a[i],i=i+1)//
with
printf("%d",a[i]);// printing on the screen integers (a[i],i=i+1)//
in your code you were incrementing i twice. Once in the while and once in the a[i++]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
I would appreciate if anyone can help me figuring out why is this fuction (fn) does not print the int array elements?
#include <stdio.h>
void fn (int arr[], int s);
int main(void)
{
int arr[] = {5, 2, 0, 9, 7}, s=5;
fn (arr, s);
}
fn(int arr[], int s)
{
int i;
for (i = 0; i < 5; i++);
{
arr[i]++;
printf("%d ", arr[i] );
}
}
for (i = 0; i < 5; i++);
The semicolon at the end of the expression terminates the for-loop before even entering the body of the loop. Hence the body is seen as a stand-alone block of code by the compiler, unrelated to the for-loop.
Since the body of the loop is detached from the for-loop, variable i (the counter of the for-loop) is out of scope since i is local to the for-loop, and can only be called inside it as it was declared inside the conditional statement of the for-loop.
To fix this problem, remove the semicolon at the end of the for-loop condition.
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 sorry if the question isn't really explanatory. I'm trying to set to 0 that matrix and thanks to the test printf I see that when i is 2 j is set to 0 insted of 3. Where am i wrong?
int matrix[3][3];
int i, j;
for(i=0;i<4;i++){
for(j=0;j<4;j++){
matrix[i][j] = 0;
printf("%d\t%d\t%d\n", matrix[i][j], i, j);
}
}
If you just want to zero out all the entries, you can use this:
memset(matrix, 0, sizeof(matrix));
You will have to #include <string.h>. If the size of the matrix isn't known at compile time by the compiler (it is declared elsewhere), then you can still use this:
memset(matrix, 0, rows*cols*sizeof(double));
In your case
memset(matrix, 0, 3*3*sizeof(double));
matrix[3][3] means its index is 0 , 1 and 2
therefore change this
for(i=0;i<4;i++){
for(j=0;j<4;j++){
to
for(i=0;i<3;i++){
for(j=0;j<3;j++){
Your loop is accessing uninitialized memory location, for eg, matrix[3][3]. It has indeterminate value and finally invokes undefined behavior.
Change
for(i=0;i<4;i++)
to
for(i = 0; i < 3; i++)
Do same the second loop.
You cam initialize your array to zero by using initializer as
int matrix[3][3] = {{0}};