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
Hy, I want to understand why variable b don't change and when I print it always be as I defined (zero). What's wrong with my code??
With this code I want to find the max integer number (b) that is 10^b <= n (with n given and integer as well).
This is my code (It's written in c):
#include <stdio.h>
#include <math.h>
int lg (int n);
int main() {
int a = 0;
scanf("%d", &a);
printf ("\nN=%d e M=%d\n", a, lg(a));
return 0;
}
int lg (int n) {
double b = 0.0;
for (int i=0; i==n; i++) {
if (pow(10, i) <= n) {
double b = i;
} else {
}
}
return (int)b;
}
You're declaring a new variable named b inside your loop and setting it equal to i. Since that's a different scope, it's not the same variable as the b declared before the loop. Change the line
double b = i;
to
b = i;
Your loop condition is also set to i==n;. That means your loop will only run once if your input is 0. That should probably be i < n;.
There are two variables named b. One whose scope is the function lg, and one whose scope is restricted to the if clause of the for loop. You modify the latter, but return the former. Using the keyword 'double' declares a new varaible. This is a common source of error, and simply turning up the warnings on your compiler should alert you to the problem.
You are declaring "b" variable twice.The "b" variable initialized with 0.0 have scope within the function "lg". And the "b" declared inside if codition have limited scope to that condition only.
Also check the second parameter of for loop i.e "i==n". This is statement would be always true i.e "1" if the value of input ("n") is not zero.
Error 1: i==n: if you compare i==n in a for loop, that loop will only run a time.You must fix it into i<n
Error 2: You repeated a double type. You mustn't declare a variable b in a for loop because the variable b in that loop will disappear from the loop when it stop repeating.Thus, the value of the variable b in the for loop won't assign.
You must delete the double keyword in the for loop, the variable b will change.
Related
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 5 years ago.
Improve this question
Here is a program in C language that simply displays variables that have not been initialized,
#include <stdio.h>
int main()
{
int a, b;
printf("%d%d",a,b);
return 0;
}
Output:
00
I have started learning c programming 20 minutes ago for the first time in my life, can you tell me why does it show the output as 00, now since we're supposed to initialize out variables with 0 in most cases to avoid seeing the garbage value in their place so I just wanted to know why does it happen ? Why don't we get a garbage value instead and not the plain '0' which has its own ASCII value?
int x = 0;
printf("%d", x); // -> 0
shows the value of x as an integer in memory. However, uninitialized variables cannot be trusted to have a value of zero every time. See What happens to a declared, uninitialized variable in C? Does it have a value?
If you want the character representation of that integer, use
int x = 99;
printf("%c", x); // -> c
instead. It will give the ASCII representation.
int x = 99; means 'c')
variable not assigned doesn't mean that it has no value, it just means that the value is not defined (or known if your prefer). But be careful, reading a variable that has never been initialised is undefined behaviour.
By default C does not give a value to uninitialized variables, you have to tell it what to store in each variable. If you do attempt to use these variables without giving them a value, the output will be unpredictable.
For example when I ran this on my pc I go the value: 41944324050944
Do not do this. If you do your program will throw unexpected results when you run it later. See this.
The proper way you would do this is to define a and b like this:
int a = 0;
int b = 0;
In the case of defining static storage the default value would be 0 (always) see this web page for more information.
This program will return 00 as expected:
#include <stdio.h>
int main()
{
static int a, b;
printf("%d%d",a,b);
return 0;
}
even-though a and b are not assigned a value the use static storage and therefore have a value 0.
Also if you want ASCII characters printed use %c instead of %d. See here. Also note that 00 in ASCII is null see here for printing null variables.
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 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 6 years ago.
Improve this question
I declared a two dimensional array as row and col and I left the value empty, but when I check the value in each array position, some of the indexes contain some weird number.
You are looking at uninitilized memory. It can have whatever which value to it. You should never trust the value of a variable you haven't initialized.
Access of an uninitialized value is undefined behavior. You can solve your problem quite simply by initializing the array to all zero to begin with, e.g.
int seatNo[5][5] = {{0}};
Now any subsequent access to any of the elements of seatNo will succeed because each element has been initialized to zero. As a rule, especially when you are learning C, you will save yourself grief if you simply initialize ALL your variables.
(you will also want to turn warnings on, e.g. -Wall -Wextra, at minimum, so your compiler will warn you when a variable may be uninitialized)
#include <stdio.h>
int main (void) {
int seatNo[5][5] = {{0}};
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
printf (" seatNo[%d][%d] = %d\n", i, j, seatNo[i][j]);
return 0;
}
Example
$ ./bin/initarray
seatNo[0][0] = 0
seatNo[0][1] = 0
seatNo[0][2] = 0
seatNo[0][3] = 0
seatNo[0][4] = 0
seatNo[1][0] = 0
seatNo[1][1] = 0
<snip>
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
I have a c code
int foo( int *p, int n){
int a[81];
int i,v;
for( i = 0; i < n ; i++){
a[i]=p[i]
if( n == 1 ) return a[0];
v=foo ( a, n-1);
if( v > a[n-1] )
return v;
else
return a[n-1];
}
int main ( void ){
int b[81], i;
for( i = 0; i < 81; i++){
b[i]=rand();
foo(b,81);
return 0;
}
And i need to find out how many instances of variable a will exist( max number) my answer was 81 , but it was wrong and i cant find out what number it should be. How could i determine it?
The main will call the function 82 times and each time the func will call recursively itself 80 times in a loop of decreasing n items.
So altogether it will be 81*81.
EDIT: I didn't notice the return after first iteration so actually it's pretty small number.
Set the number of elements in a to be arbitrarily high.
Build a function to assign elements to a, and only use that function to set elements of a. You could use this function to make sure that you don't attempt to write outside the bounds of a. (Else the behaviour of your program will be undefined and so any results will be meaningless).
Encode a "watermark" static int in that function to record the largest index accessed.
Run your program and note the value of the variable set up for (3).
Declare a global counter - it will be initialized to zero. In the function, at the same scope as 'a' is declared, increment the counter.
Printf() it out at the end of main().
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am doing a past exam question where i am asked to describe what the following c code implements and re write it with meaningful variables names and good programming practices.
The code is as follows:
void h(int ** a, int b){
for(int x=1; x<=b; x++)
for(y=1; y<=b; y++)
a[x-1][y-1]= (i/j)*(j/i);
}
I dont understand how this code works as:
1) y is never declared in the for loop.
2) the arithmetic of i and j doesn't make sense to me either if i and j are not declared.
I originally thought this was a intake and that i and j are just x and y. But if that was the case the question would have been very easy compared to any other i have already completed.
The code is probably intended to create an identity matrix with 1's on the leading diagonal and 0's elsewhere.
The Fortran equivalent of that code is used as the first example of bad code in the classic Elements of Programming Style by Kernighan and Plauger.
Working from memory, the Fortran code was roughly:
DO 10 I = 1, N
DO 10 J = 1, N
10 A(I,J) = (I/J)*(J/I)
(There might have been two labels, 10 and 20 say, and one or two continue statements, but I think it was like that. At the time, columns 1-5 were reserved for numeric labels, column 6 was a continuation indicator, and the program occupied columns 7-72, with columns 73-80 being an optional statement sequence number.)
Since y is used but not declared, if it compiles, y must be an external variable (either a global variable or a variable with file scope: extern int y; or int y; or static int y; outside the function).
Since i and j are used but not declared, if it compiles, they too must be external variables. And given that neither i nor j is changed, the same value (either 0 or 1, depending on whether i != j) is in fact assigned to each element of a.
The more nearly 'correct' code implementing the same (bad) algorithm should be:
void h(int **a, int b)
{
for (int x = 1; x <= b; x++)
for (int y = 1; y <= b; y++)
a[x-1][y-1] = (x/y)*(y/x);
}
Of course, this is not very C-like, but it does avoid divide-by-zero problems. However, cleaner and simpler code can be written:
void h(int **a, int b)
{
for (int x = 0; x < b; x++)
{
for (int y = 0; y < b; y++)
a[x][y] = 0;
a[x][x] = 1;
}
}
The function should be renamed to something more meaningful.
To complete the answer given above, all the undeclared variables should been understood to have been declared in a larger scope. If you are sure that it is indeed i and j (and both are int) then it will be a zero matrix or a martix of ones(if i = j ).