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 7 years ago.
Improve this question
I've did a program for replacing even numbers with 0 and putting them at the end of the array.
Input: 1 2 3 4 5 6
Output: 1 3 5 0 0 0
The constrains are to use only one loop and two variables apart from the array. I've did it with a while loop but for some unknown reason it loops infinitely. Could someone please clarify?
#include<stdio.h>
#include<stdlib.h>
int main(){
int arr[] = {1,2,3,4,5,6};
int i=0,j=5;
while(i<6){
//Loops till the zero is swapped to the end of the array
if(j!=5){
arr[j] = arr[j] + arr[j+1];
arr[j+1] = arr[j] - arr[j+1];
arr[j] = arr[j] - arr[j+1];
j++; continue;
}
//Checks for even number
if(arr[i]%2==0 && arr[i]!=0){
arr[i]==0;
j=i;
continue;
}
printf("%d ",arr[i]);
i++;
}
return 0;
}
What is causing the infinite loop is this line:
arr[i]==0;
This doesn't update the value of arr[i] when i = 1, so arr[1] is always 2, and this produces that i is stuck always with the value 1. Should be an assignment no a comparison expression:
arr[i]=0;
Note: If you enable the warnings when compiling, the line in question should produce one.
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
This is code written by me where I have to print a single integer denoting the minimum possible capacity of a tram (0 is allowed). It's a problem from codeforces. The answer in CodeBlocks is showing 6 (the right answer) but in codeforces compiler I'm getting another output.
Why is this happening?
#include <stdio.h>
int main() {
int n, i, j, max = 0, sum = 0;
int pssnger_left;
scanf("%d", &n);
int a[n][2];
for (i = 0; i < n; i++) {
for (j = 0; j < 2; j++) {
scanf("%d", &a[i][j]); // declaring the value of array
}
}
pssnger_left = a[0][0] + a[0][1];
for (i = 1; i < n; i++) {
sum = pssnger_left - a[i][0];
sum = sum + a[i][i];
pssnger_left = sum;
if (max < sum)
max = sum;
}
printf("%d", max);
}
Input:
4
0 3
2 5
4 2
4 0
Output:
4221555
Answer:
6
Checker Log
wrong answer expected 6, found 4221555
Here is the link of the problem: https://codeforces.com/problemset/problem/116/A
Different compiler, different answer, 99% of the cases is explained by Undefined Behaviour, UB.
In the shown code here it is a[i][i];.
For any i > 1 that is not what you want it to be
and for most high i it illegally accesses beyond a.
-> Undefined Behaviour.
A hint on how I spotted this:
Whenever I see [i][i], actually whenever I see [same][same],
I think "diagonal in a square". And in your code I immediatly thought "What square?", because when seeing int a[n][2]; I thought "Long narrow table." and got a conflict of shapes there.
The problem is not basically with the compiler.
You are doing
sum = sum + a[i][i];
but you have declared a 2D array of n x 2 i.e. int a[n][2]
see in some compilers it shows out of bound because you are accessing an element which is not there in the array
and in compilers it just loops in the already existing array for ex if your array has 5 elements and you are trying to access 6th element then it will go back to 1st index,
so this might be whats happening here.
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 3 years ago.
Improve this question
just playing around with for loops, and wanted to see what the results are.
I'm assuming both produce the same results, but I'm wrong.
int sum ,sum2 , i , j;
for( sum = 0, i = 1 ; i <= 5 ; sum+=i , i++ )
printf("%d\t",sum);
printf("\n");
for( sum2 = 0, j = 1 ; j <= 5 ; j++ ) {
sum2 +=j;
printf("%d\t",sum2); }
0 1 3 6 10
1 3 6 10 15
In the first loop sum is incremented at the end of the iteration, so after the call to printf, while in the second loop sum2 is incremented before the call to printf.
If you follow the execution pointer carefully, in the first loop sum += i occurs before i is incremented.
In the second loop, sum2 += j occurs after j has been incremented.
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 4 years ago.
Improve this question
Yesterday, I had to do a math introduction test, where I had to calculate the cardinality of a summation. I was lazy to write it out and I thought I'd just write a small C program to solve it. I felt confident that'll work, since I did much more complex programs, but I just can't get it to work.
#include <stdio.h>
int main(){
int i = 1;
int n = 2 * i + 1;
while(n <= 36){
printf("%d\n", n);
i++;
}
return 0;
}
In theory, there should have been the sequence "3, 5, 7, 9, ...", but all I get is "3, 3, 3, 3, ...". It's only not working if I'm using the variable n, if I replace it with i within while everything works as I would expect.
What am I missing?
This:
int n = 2 * i + 1;
Is not a formula for n which gets calculated every time n is used. It sets n to the value of 2 * i + 1 == 2 * 1 + 1 == 2 + 1 == 3 at the time the statement is encountered and that's it. So n never changes inside of the loop and you end up with an infinite loop.
Move the assignment to inside the loop:
while(n <= 36){
printf("%d\n", n);
i++;
n = 2 * i + 1;
}
There is no change of value n in the while loop and so it never fail the condition n <= 36. Maybe you want to do this way
while(n <= 36){
printf("%d\n", n);
i++;
n = 2 * i + 1;
}
It always gives 3 as a result because you do not update n at each iteration. n is calculated once at the beginning. You should calculate n again each time i is incremented.
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 4 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
#define SIZE 13
int main(){
int die1, die2, sum, i, occurrences[SIZE], j=2;
for(i=0; i<36000; i++){
die1=1+rand()%6;
die2=1+rand()%6;
sum=die1+die2;
++occurrences[sum];
}
printf("%10s","Sums");
for(i=1; i<=12; i++){
printf("%4d", i);
}
printf("\n%10s","Occurrences");
for(i=2; i<=12; i++){
("%4d",occurrences[i]);
}
return 0;
}
Why doesn't this code work? The program has to sum all the random numbers generated by two dies and then it has to print the occurrences of the sums. Why doesn't it work? The output is this:
Sums 1 2 3 4 5 6 7 8 9 10 11 12
Occurrences
The array occurrences[SIZE] is not initialized and has indeterminate values, so the addition ++occurrences[sum]; will invoke undefined behavior. You should initialize it like occurrences[SIZE] = {0}.
The line ("%4d",occurrences[i]); is just a expression using comma operator surrounded by parenthesis, so it won't do printing. It looks like printf should be added before the parenthesis.
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 7 years ago.
Improve this question
This one has me stumped. Here is my code to build an array, b[i] of doubles, from 0 to N where N = 126.
int N = 126;
double b[N];
int i;
for(i = 0; i < N; i++);
{
b[i] = (double)i;
printf("b[%lf] = %d\n",b[i], i);
}
For some reason, this is what I get:
b[126.000000] = 126
and nothing else. The initial condition of i being at 0 is ignored, and for some reason it sets i to be the value of N. Strange!
I'm a bit of a c novice so I must be missing something obvious. Any help greatly appreciated!
Andy.
The mistake is at you using the ; at the end of the for loop statement. That is why the program is simply executing the remaining statements as if they are in no loop, and at that time i has become 126.
Remove the ; on the end of the for loop, it is running through the loop without doing anything then executing the body for the last value of i(which is N = 126)