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.
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 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++)
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
I just started learning C from yesterday and I encountered an issue and I am not getting why it is happening.
The code is this
#include<stdio.h>
int a=0;
void main() {
if (a=13) {
printf("Number Is Equal\n");
}
else {
printf("Not Equal\n");
}
}
It should show not equal but it is still showing Number is equal and i tried other numbers too in place of int a;
If i assign the value of int a=13 and then if i run the statement if(a=13) then it is true but if i do the same with 0 on both place then it shows not equal.
You're assigning 13 to a using the assignment operator, =. Use == for comparisons.
example:
int a=0;
void main()
{
// Here, a is being checked to see if it is equal to the int 13.
// a=13 would be assigning 13 to a, and then checking to see if
// a is "truthy", or, not 0, which is why it was true for you every time.
if (a == 13) {
printf("Number Is Equal\n");
}else{
printf("Not Equal\n");
}
}
NOTE: The C Standard specifies that main() will return type int, (e.g. int main(){return 0;}) when in a hosted environment. You can deviate from this when you're in a freestanding environment, at which point this becomes implementation defined. It is not likely that you are using a freestanding environment. The odds are that you are in a hosted environment where your use of type void for main would violate the C Standard.
Here
int a=0;
if (a=13) { }
It should show not equal ?
No, if condition always gets true due to the assignment a=13 which is non zero, you need to use == instead.
Change this
if (a=13) { } /* = is assignment operator, condition remains always true */
to
if (a == 13) {} /* use comparison operator == */
You are writing a=13, which assigns the value 13 to a. Since this is non-zero it is also considered true (zero would translate to false).
You should write a == 13 instead, using double equal signs, for comparisons.
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 am trying to learn how to code by myself and is experiencing some difficulty in calculations. Can someone please explain why the pf always return 0 in the below?
int main(void)
{
//solicit input from users
long long int num = get_long_long("Credit card no: ");
eprintf("%lld\n",num);
//initialize
int i =0;
int j =0;
int counter =0;
string status;
//find length of input
while (num>0)
{
num/= 10;
counter++;
}
printf("counter is %i\n",counter);
//Identify card type by prefix
int power=(counter-2);
eprintf("power is %i\n", power);
int dp = pow(10,power);
eprintf("divofp is %i\n", dp);
//prefix=num
long long int pf=(num/dp);
eprintf("pf is %lld\n",pf);
}
pf will always be zero, because num is set to zero at the end of your while loop.
Therefore num/anything will always equal zero.
A good method of debugging, is to step through the code line by line, and look at the values of your variables at each point in time.
This can help you narrow down problems like this.
The problem is where you get the length of your number:
while (num>0)
num/=10;
num will always be 0 after this and thus your final expression will result in 0 because 0/x = 0 (x != 0).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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 a problem when passing the decimal number from function - take a look at the code.
This function where I am searching for number in arguments of function main on position 2 works just fine, but if I call the function it doesn't return the decimal part. For example input is 15.25 - function take 15.25, but when that value is returned to a variable it shows 15.00. Do you have any ideas why?
BTW: I made that function much simpler for this example.
int arg_find(char *argv[]) {
double result;
int i = 0;
double dec = 1;
while ((argv[2][i] >= '0' && argv[2][i] <= '9') || argv[2][i] == '.') {
// lets say in argv[2] is 14.25
if (argv[2][i] == '.') {
decimal = true;
i++;
}
if (decimal == true) {
dec *= 10;
result += (argv[2][i] - '0')/dec;
i++;
} else {
result = result * 10 + (argv[2][i] - '0');
i++;
}
}
}
Now, when I call the function
double result2;
result2 = arg_find(argv); //in result2 is 14
The reason is very simple and obvious because your function arg_find returns an integer not a float hence you are facing the problem.
You may try to define it as:
double arg_find(char *argv[])
instead of
int arg_find(char *argv[])
Define your function as
double arg_find(char *argv[])