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 8 years ago.
Improve this question
Came across this code the other day:
for(i=0; i<r1; ++i)
for(j=0; j<r1; ++j)
for(k=0; k<r1; ++k){
mult[i][j] += a[i][k] * b[k][j];
}
Now, is this just a different way of writing:
for(i=0; i<r1; ++i){
for(j=0; j<r1; ++j){
for(k=0; k<r1; ++k){
mult[i][j] += a[i][k] * b[k][j];
}
}
}
yes that is correct .. Put brackets around the first implementation to see for yourself.
PS - there is no one right way .. but I would highly recommend using brackets always ( especially if you are new to programming ) .
It goes a long way to help you understand and debug the code
Yes. The syntax for for is
for (init-statement; condition; iteration_expression)
statement
and for is itself an statement. Therefor it can make a recursive compound.
You can even drop last {}:
for(i=0; i<r1; ++i)
for(j=0; j<r1; ++j)
for(k=0; k<r1; ++k)
mult[i][j] += a[i][k] * b[k][j];
Indentation is very important to make the code readable.
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 1 year ago.
Improve this question
I am not sure if this kind of questions are appropriate here, but...
By changing (or adding) one character make this program print '*' exactly 20 times
void main(){
int i, n=20;
for (i=0; i<n; i--)
printf("*");
}
Any ideas?
It seems you mean the following change from
for (i=0; i<n; i--)
^^^
to
for (i=0; i<n; n--)
^^^
Here is one character i is substituted for one character n.
Another way (if it is allowed by the puzzle) is to add one character '-' like
for (i=0; -i<n; i--)
^^^
Note: By the way pay attention to that according to the C Standard the function main without parameters shall be declared like :)
int main( void )
either do i++ or (i-- & n-=2or any other positive number greater the 1)
That is pretty easy, just change the i-- to n--.
Maybe next Time a Harder Puzzle? ;)
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 3 years ago.
Improve this question
Following selection sort code is generating problem in a[p]>a[j] line why?Although if i put a[i] instead of a[p] it works fine
for (i=0; i<n; i++){
p=i;
for (j=i+1; j<n; j++) {
if(a[p] > a[j]) {
p=j;
t=a[i];
a[i]=a[p];
a[p]=t;
}
}
}
The line p=j; in the inner loop changes p. This causes p to be different from i later, when if(a[p]>a[j]) is evaluated with a new j.
Your code does not need p at all. The test can be merely if (a[i] > a[j]), and the code inside the loop can be:
t = a[i];
a[i] = a[j];
a[j] = t;
Looking at your code, it seems you think you needed to record i or j in p temporarily and then use p. That is not necessary. To use the values of i or j, simply use i or j.
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'm trying to populate my 2D array with random integers.
//rows, cols are randomly generated ints between 1 and 10
int rows = rand()%10 +1;
int cols = rand()%10 +1;
int arr[rows][cols];
for (i = 0; i <rows; i++){
for (j=0; i<cols; j++){
arr[i][j] = rand()%10;
}}
Every time I try to run this code, it gives me a segmentation default.
I edited the post to give the actual code I am running
I suppose it's because the i < ... which should be a j < ... in for (j=0; i<cols; j++);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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
Improve this question
I have an array contain 100 elements. Can anyone help me to figure out how to write loops that perform this:
data[0] = 1/(2*3*4)
data[1] = 1/(4*5*6)
data[2] = 1/(6*7*8)
...
data[99] = 1/(200*201*202)
data[0]-data[1]+data[2]-data[3]+data[4]-data[5]+...+data[98]-data[99]
I just can't understand how to start. Any suggestions would be appreciated!
Try this
double c=0;
for (int i=0;i<100;i++)
{
c=i*2+2;
data[i]=1/(c*(c+1)*(c+2));
}
for (int i = 0; i < 100; i+=2)
{
op+= data[i] - data[i+1];
}
My suggestions how to start, if you really want them and want to manage this by your own:
Generalize your algorithm:
Find a function f(x) such that data[i] = f(i)
Just write the algorithm in your native language.
Then learn basic operators of C language, including loop construct.
Write your "native language algorithm" in C language.
Just in one loop:
int total = 0;
for(size_t i=0; i<100; ++i){
int temp = (i+1)*2;
data[i] = 1/(temp*(temp+1)*(temp+2));
total = total + (i%2==0?data[i]:-data[i]);
}
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 declare 2d dynamic array. when run program this error shown:
Unhandled exception at 0x012219c4 in SW-Serial.exe: 0xC0000005: Access violation writing location 0xabababab.
the part of my program that error occured:
double** SWArray;
SWArray = (double**) malloc(lenA*sizeof(double*));
for (int i = 0; i <= lenA; i++)
SWArray[i] = (double*) malloc(lenB*sizeof(double));
for(int i=0;i<=lenA;i++){
SWArray[0][i]=0;
}
for(int j=0;j<=lenB;j++){
SWArray[j][0]=0;
}
picture of this problem
Arrays start from 0 in C. Wherever you say i <= lenA it should be i < lenA. Same goes for j and lenB. Also, the second loop doesn't really make sense. Did you mean lenB instead of lenA ?
You have lenA And lenB mixed up. It should be:
SWArray[i][0] and SWArray[0][j] in your loops.
And you loops should use < not <=
Both the loops are indexing in the wrong dimension and also are accessing elements beyond the size for which memory is allocated. You should change i<= to i< in both the loops
The correct way should be:
for(int i=0;i<lenA;i++){
SWArray[i][0]=0;
}
for(int j=0;j<lenB;j++){
SWArray[0][j]=0;
}