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 want to print an array in c, but only if the specific element of this array is not zero.
Lets assume, foo looks like this 12345678900000000000123 i want only print 123456789123.
int foo[512];
for (i= 0; i < 512; i++) {
if (foo[i] != '0') { // This is not working
printf("%d ", foo[i]);
}
}
This is because your condition is bad.
if (foo[i] != '0')
whit this, you test whether the foo[i] is equal to a character 0 and not a number 0
for fix this use this condition
if (foo[i] != 0)
Your code has two problems.
You didn't initialized the array foo and foo has storage-class auto. The elements contain arbitrary garbage values. Using them in the loop invokes undefined behavior.
'0' is not equal to 0. '\0' is equal to 0. But better use 0 as you only want 0 in its integer nature.
Surrounding integers in apostrophes make them character constants which have specific value. Most likely these values belong to the ASCII character set. '0' for example has the ASCII value 48.
So
if (foo[i] != '0')
is high-probably equivalent to
if (foo[i] != 48)
on your system.
Side Note:
int foo[512];
for (i= 0; i < 512; i++)
You use two times the hardcoded value 512. This technique is susceptible for failures. Better use f.e. a macro constant instead, which ensures that the values are equal:
#define SIZE_ARR 512
...
int foo[SIZE_ARR];
for (i= 0; i < SIZE_ARR; i++)
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 6 months ago.
Improve this question
#include <stdio.h>
int main() {
/*function declaration*/
double invalidInput(double);
int loop_obeTask;
double obe_Task[3];
double checkInput ;
for(loop_obeTask = 1 ; loop_obeTask < 4 ; loop_obeTask++)
{
printf("OBE%d : ", loop_obeTask);
scanf("%lf",&obe_Task[loop_obeTask]);
checkInput = invalidInput(obe_Task[loop_obeTask]);
if(checkInput = 1)
{
printf("INVALID INPUT \n" );
break;
}
}
return 0;
}
double invalidInput(double inputGrade)
{
double indicator;
if(inputGrade > 100)
{
indicator = 1;
}
else{
indicator = 0;
}
return indicator;
}
Hello Guys, want to validate the input numbers using the function once the user enters > 100 IT IS INVALID INPUT but in my case, it always shows invalid input both greater and less than 100. I can't post image because my reputation less than 10.
enter image description here
enter image description here
Short answer
You're not checking the value, but assigning it: if(checkInput = 1)
You should use the == operator instead.
Long answer
Even if it does compile, your code presents numerous conceptual mistakes:
You're using double as return value of invalidInput(). Since that function is for validation only and it returns either 1 or 0, you should use int as return type instead.
Your for loop starts at index number 1, which is wrong and will lead to undefined behaviour. C array indexes starts at 0, so if you define an array with size 3, the available indexes are going to be 0, 1, 2, and the loop should look something like for (index = 0; index < MAX; index++) where MAX is 3 in this case.
Your if condition is not checking the equality of checkInput and 1. That's because you're using the assignment operator =, instead of the equality operator ==.
It's common practice to put your function declarations at the top of your file and before the main definition, both for readability and scope reasons.
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 4 years ago.
Improve this question
I have little problem here. Ever since I used srand command I no longer get output I want.
I have to create program that will create array not matter how long.. I chose 5. Then it has to create random numbers into it and 5th number in it has to be 0 and then it has to count all five numbers together. I was sucessful and I did even use some printfs to check if arr[5] is zero. It is. In output I receieve this: 5random numbers (that ones that were created into array) 0 (to check if command where i set arr[5] really set it to zero and result which only count all 5random numbers together but without changing 5th arr into 0. Any ideas why? Thanks!
Code looks like this:
#include <stdio.h>
#include <time.h>
main()
{
srand(time(NULL));
int result,i;
int arr[5];
for (i=0;i<5;i++)
{
arr[i] = rand() % 10+1;
printf("%d ",arr[i]);
}
arr[5]=0;
printf("\n");
printf("%d\n",arr[5]);
i=0;
for(;i<5;i++)
{
result +=arr[i];
}
printf("%d\n",result);
printf("%d\n",arr[5]);
}
The declaration int arr[5] gives an array with five elements numbered from 0 to 4 inclusive. Accessing the 6th element, arr[5], is undefined.
If you want five elements and a "sentinel" (the zero marker), you could define arr as
int arr[6];
You should probably also initialize result to 0 otherwise its value is undefined since it's a local variable (in fact, the meaning of the whole program is, strictly speaking, undefined in this case).
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
int attack_grid[10][10] = { {0} };
void drawAttackGrid()
{
int i, j, x = 0, y = 0;
for (i = 0; i <= 10 - 1; i++) {
for (j = 0; j <= 10 - 1; j++) {
if (attack_grid[x][y] > 0)
printf(" * ");
else if (attack_grid[x][y] < 0)
printf(" ~ ");
else
printf(" ? ");
y++;
}
printf("\n");
x++;
}
}
So I am trying to fill this 10x10 array with different characters based on the value of the coordinate in other 10x10 array which is filled with zeros only(I'm gonna change those values later so that's why I need it to be general). According to my code it should print only " ? ", but there are some " * " in the output too. Can someone explain me why do i get those " * " there, please?
Your program has undefined behavior due to the value of y.
y gets incremented in the inner loop but never gets reset to 0 when the outer loop is repeated.
In the second run of the outer loop, the value of y will start at 10 instead of starting at 0.
In the third run of the outer loop, the value of y will start at 20 instead of starting at 0.
That goes on for the remaining iterations of the outer loop.
You can remove the redundant indices x and y. Use attack_grid[i][j] instead of attack_grid[x][y]
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.