Rizaldi
can someone help me explain BFS method using C program?
Nils
Hey, so I found this about C optimisation and UB:
As long as you don't invoke undefined behaviour (eg. reading outside the bounds of a variable), you are fine. As an optimiser, the compiler is unlikely to care about which branch will happen and will do as good a job of picking the one with the better chance of success.
Also, in some compilers (C99 or newer), it's possible to do some optimisations for certain types of branches. For example, the "switch" construct can be very optimised for certain types of branches. This is a compiler generated switch, not a manual switch statement.
Edit: this behaviour was changed in C99. The optimiser must be able to prove that the outcome of a non-taken branch is the same as a taken branch, and if the former can't do so, the former must be removed from the program, no matter which branch is taken.
Can someone explain that in easier words lol? Reading this just gets me confused 😂
Kartik
Hi,
I'm receiving "Segmentation fault" error in the code below.
https://pastebin.com/pafMGawk
Error most probably is in the last line of the main function, if I'm not wrong.
Can anyone explain why it is happening and what can be done to correct it?
Let me know if I'm messing up elsewhere.
Thanks
klimi
Kartik
Never heard of it, is it something like gdb?
Kartik
Anyway, will try it!
klimi
just run your program with "valgrind a.out" and it should give you some information
Anonymous
Why is recursive parsing so complicated 😭😭😭
Anonymous
Hi I'm starting to learn c programming so plz help me guys
coal
coal
you are parsing a stream of tokens, right
coal
because if you're parsing it directly like with scss i would completely understand why you find it complicated lol
Anonymous
Anonymous
Hey, so I found this about C optimisation and UB:
As long as you don't invoke undefined behaviour (eg. reading outside the bounds of a variable), you are fine. As an optimiser, the compiler is unlikely to care about which branch will happen and will do as good a job of picking the one with the better chance of success.
Also, in some compilers (C99 or newer), it's possible to do some optimisations for certain types of branches. For example, the "switch" construct can be very optimised for certain types of branches. This is a compiler generated switch, not a manual switch statement.
Edit: this behaviour was changed in C99. The optimiser must be able to prove that the outcome of a non-taken branch is the same as a taken branch, and if the former can't do so, the former must be removed from the program, no matter which branch is taken.
Can someone explain that in easier words lol? Reading this just gets me confused 😂
It is not easy to explain this in simpler words. The standard has actually used as simple words as possible to describe a very complex optimization technique. Check out gcc optimization regarding RTL if-conversion. It is a heavy subject unless you have compiler development experience
Anonymous
in what context
in the case of https://gist.github.com/mgood7123/006f114fce1d47a6b3d76b21ae7a5579
(i got a basic proof-of-concept for this recursive-parsing working so at least it IS possible
Anonymous
now imma take a break 🙂
been working on this for over a week 🙁
Nils
klimi
?
Anonymous
guys can anyone help me to solve my math task?
Dawa
//Write a program of calculator apps using user defined in c programming?
Anonymous
Hey, so I found this about C optimisation and UB:
As long as you don't invoke undefined behaviour (eg. reading outside the bounds of a variable), you are fine. As an optimiser, the compiler is unlikely to care about which branch will happen and will do as good a job of picking the one with the better chance of success.
Also, in some compilers (C99 or newer), it's possible to do some optimisations for certain types of branches. For example, the "switch" construct can be very optimised for certain types of branches. This is a compiler generated switch, not a manual switch statement.
Edit: this behaviour was changed in C99. The optimiser must be able to prove that the outcome of a non-taken branch is the same as a taken branch, and if the former can't do so, the former must be removed from the program, no matter which branch is taken.
Can someone explain that in easier words lol? Reading this just gets me confused 😂
i can understand it fine 🙂
Anonymous
Kartik
Oh nice,
Is this possible then?
N1->next->val = 2;
N1->next->next->val = 3;
N1->next->next->next->val = 4;
And so on...
Anonymous
Kartik
Anonymous
ok i now have a stack rule 🙂 https://gist.github.com/e172a4725350749c89b9bdaf3c5d8de9
Ayaan
comparison between two array using while loop
Anonymous
https://gist.github.com/168c4b17dd814fd977a2db88af5d9f5f i dont know how to correctly do this 🙁
Anonymous
ㅤㅤㅤ
int a(0) and int a = 0 are same or different
Pavel
int a(0) and int a = 0 are same or different
The result is same in this case
But in case of C++ classes which have explicit constructors or constructors with multiple arguments, the second won't work and the first will
Anonymous
int a(0) and int a = 0 are same or different
For built in types like int, float etc the result is the same but for user defined types, the former case is a direct initialization where your arguments decide which constructor is to be called based on function matching decisions. The latter case is copy initialization which could end up being a costly call in cases where optimization is not possible.
Anonymous
#include<stdio.h>
int addition(int a,int b);
int subtraction(int a,int b);
int product(int a ,int b);
float division (int a,int b);
int main()
{
int x ,y;
int calculation;
printf("Entre for x and y:");
scanf("%d%d",&x,&y);
printf("Entre: 1.for addition;2.for subtraction;3.for product;4.for devision:%d ");;
scanf("%d",&calculation);
switch(calculation)
{
case 1: addition(x,y);
printf("The calculation is :%d",addition(x,y));
break;
case 2: subtraction(x,y);
printf("The calculation is :%d",subtraction(x,y));
break;
case 3: product(x,y);
printf("The calculation is :%d",product(x,y));
break;
case 4:division(x,y);
printf("The calculation is :%d",division(x,y));
break;
default:printf("Only limited option available");
}
return 0;
}
//O1
int product(int a ,int b)
{
int multiple;
multiple=a*b;
return multiple;
}
//O2
float division (int a,int b)
{
int div;
div=a/b;
return div;
}
//O3
int subtraction(int a,int b)
{
int sub;
sub=a-b;
return sub;
}
//O3
int addition(int a,int b)
{
int sum;
sum=a+b;
return sum;
}
Anonymous
Any one help me with division
Anonymous
I tried using float too
Anonymous
But if I divide 1/2 the result is 0
coal
artemetra 🇺🇦
#include<stdio.h>
int addition(int a,int b);
int subtraction(int a,int b);
int product(int a ,int b);
float division (int a,int b);
int main()
{
int x ,y;
int calculation;
printf("Entre for x and y:");
scanf("%d%d",&x,&y);
printf("Entre: 1.for addition;2.for subtraction;3.for product;4.for devision:%d ");;
scanf("%d",&calculation);
switch(calculation)
{
case 1: addition(x,y);
printf("The calculation is :%d",addition(x,y));
break;
case 2: subtraction(x,y);
printf("The calculation is :%d",subtraction(x,y));
break;
case 3: product(x,y);
printf("The calculation is :%d",product(x,y));
break;
case 4:division(x,y);
printf("The calculation is :%d",division(x,y));
break;
default:printf("Only limited option available");
}
return 0;
}
//O1
int product(int a ,int b)
{
int multiple;
multiple=a*b;
return multiple;
}
//O2
float division (int a,int b)
{
int div;
div=a/b;
return div;
}
//O3
int subtraction(int a,int b)
{
int sub;
sub=a-b;
return sub;
}
//O3
int addition(int a,int b)
{
int sum;
sum=a+b;
return sum;
}
also you don't need to create a separate variable
int product(int a, int b) { return a*b; }
float division(int a, int b) { return a/b; }
int subtraction(int a, int b) { return a-b; }
int subtraction(int a, int b) { return a+b; }
artemetra 🇺🇦
try with these
coal
coal
that's why it looks horribly complicated
Anonymous
coal
you're writing a recursive descent parser and recursive descent lexer, lol
coal
also "replace and rescan"; you shouldn't never ever modify the original source
coal
unless you're writing something like a preprocessor, it's absolutely not necessary for parsing
Anonymous
I am newbie so
Anonymous
Anonymous
#include<stdio.h>
int addition(int a,int b);
int subtraction(int a,int b);
int product(int a ,int b);
float division (int a,int b);
int main()
{
int x ,y;
int calculation;
printf("Entre for x and y:");
scanf("%d%d",&x,&y);
printf("Entre: 1.for addition;2.for subtraction;3.for product;4.for devision:%d ");;
scanf("%d",&calculation);
switch(calculation)
{
case 1: addition(x,y);
printf("The calculation is :%d",addition(x,y));
break;
case 2: subtraction(x,y);
printf("The calculation is :%d",subtraction(x,y));
break;
case 3: product(x,y);
printf("The calculation is :%d",product(x,y));
break;
case 4:division(x,y);
printf("The calculation is :%f",division(x,y));
break;
default:printf("Only limited option available");
}
return 0;
}
//O1
int product(int a ,int b)
{
return a*b;
}
//O2
float division (int a,int b)
{
return a/b;
}
//O3
int subtraction(int a,int b)
{
return a-b;
}
//O3
int addition(int a,int b)
{
return a+b;
}
coal
1/2 is 0.5, casted to an integer it returns 0
coal
you must force a floating point value using something like casts:
return (float) a / b;
Anonymous
It woks
Anonymous
But can you elaborate more
Anonymous
I used float in every division part but why I need to write float in result.
coal
coal
the operator / when provided two int arguments returns an integer
Anonymous
Oh
coal
essentially, it returns (int) result;
coal
so you have to cast one of the sides to a float, double, or long double
Anonymous
I missed that thanks a lot
coal
you're welcome
coal
Anonymous
are you i git hub
coal
yes i am in git hub
Anonymous
I need to modify more
coal
how so
Anonymous
Need to use loop for multiple inputs for calculation
coal
do {
} while (!input.empty());
coal
you can use a do while loop to perform inputs until the input is empty
Anonymous
what about for
coal
and you can just add a if condition inside:
if (!input.empty()) {
// parse numbers and do math
}
coal
what about for
so you can leave the loop when you dont want to input anything else
Anonymous
Wait let me create repository
coal
coal
rijan i won't code for you, you need to solve your challenges by yourself