This question already has answers here:
C code won't finish running
(4 answers)
Closed 9 years ago.
The purpose of this code is to construct a char Hadamard matrix of the size of my choosing.
This question is related to a previous question I asked. The answer given there was an integer not char matrix, but the code here is pretty much the same format.
The code compiles but when executed it doesn't finish and I don't know why. When executed infinite 2's are printed.
I get the same result when swap the dynamic Hadamard matrix section for one of a fixed size.
Note: I've no idea what your program does, but obviously this is wrong. You failed to actually change the control variable in your for-loop (which can be done in the final expression or the loop body itself).
Change this:
for (ind=1;ind<=sizeH;ind*2)
to this:
for (ind=1;ind<=sizeH;ind*=2) // << note *=
Related
This question already has answers here:
Recursion or iteration?
(14 answers)
Closed 2 years ago.
What is the difference between loops and recursion and which one is more preferred for solving a problem?
Loop is repeated execution of a block of code until the given condition is false.Loop has a linear flow from initial condition to terminating condition.
On the other hand recursion is a method/function being called by itself. Recursion takes place creating stack of task ie, each calling function returns to its caller function after execution.
Loop and recursion aren't exactly alternatives to have preferences, they are used for different type of tasks.
This question already has answers here:
What is the full "for" loop syntax in C?
(6 answers)
Closed 5 years ago.
My question is simple,thus I will not go in deep
can we use for() loop without condition like this
for(;;space+=1)
{
printf(" ");
break;
}
Of course you can. An empty condition is taken to evaluate to 1.
for (;;){/*ToDo - your code here*/} is idiomatic C.
Yes it is perfectly correct to do so.
But since you have provided a break immediately after printf, it will only execute once. I'm not sure whether this is what you wanted. But if so, then this works fine.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I've written a program in C that iteratively solves Laplace's equation on a square domain by successive over-relaxation, but although it was working perfectly a few days ago, I've come back to it and now it's doing really weird things.
The program involves creating two 7x7 arrays, one to hold the values of phi (the dependent variable in Laplace's equation) and one to hold the residuals from the successive over-relaxation. The array of residuals is filled with zeroes initially because I thought that was probably a good idea, but thinking about it now it shouldn't make much difference either way.
The problem is that certain values in the array (specifically R[4][6]) jump around randomly to enormous numbers, even though I've set them to zero and then not touched them. I know this because I put another line in to output the value of the residual at (4,6) to the screen after each iteration. This problem is causing my method of checking for convergence to fail, and also somehow causing the final solution (plotted as a surface using gnuplot) to look correct except for small peaks and valleys in the surface.
I'm coding in C, Dev-C++ 4.9.9.2 is the compiler I'm using (the files are definitely saved as C files and not C++ ones too), outputting the data into a DATA file which comes up in notepad when opened, and finally using gnuplot to produce the 3D contour plots that are my results. I'm working on windows 7.
I've put the code here: https://gist.github.com/anonymous/8220425 and you need to enter 1.35 for alpha when the program asks you for it.
If anyone could help at all I would be very grateful!
printf("Residual at (4,6)=%lf\n",R[4,6]);
You mean:
printf("Residual at (4,6)=%lf\n",R[4][6]);
The issue here is that R[4,6] basically means R[6] (4,6 means: evaluate 4, then evaluate 6), which is some sort of pointer rather than a double. So it will print random-looking values depending on where your array has been allocated in memory.
This question already has answers here:
EXC_BAD_ACCESS signal received
(36 answers)
Closed 9 years ago.
I am trying to finish an assignment for my C programming class on Xcode. It's simple. It reads a text file, which contains a list of student IDs and their test scores. And then it calculates the overall score based on these data, and writes them to another text file. However, I keep getting this Bad Access error message that says:
Thread 1: EXC_BAD_ACCESS(code=1, address=0x7fff00000de4)
I don't know what it means. What should I do?
The EXC_BAD_ACCESS means that you tried to attempt the incorrect address in memory.
You should start with the following:
Check that all pointers are initialized before using
If you have an array, so possibly you are trying to access the element that is out of bounds
And of course, use the debugger.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Debugging a program that crashes 10 times in different places
You are given a the source to a application which is crashing when
run. After running it 10 times in a debugger, you find it never
crashes in the same place. The application is single threaded, and
uses only the C standard library. What programming errors could be
causing this crash? How would you test each one?
Pointer error. You're trying to read/write an undefined pointer - do a find and replace for free(x) -> free(x);x=NULL.
Edit: Should also check for assignment; are you doing anything like this?
int *a;
a++;