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
Kartik
Never heard of it, is it something like gdb?
Kartik
Anyway, will try it!
klimi
Never heard of it, is it something like gdb?
it's tool that you can use to find invalid reads/writes
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
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
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 🙁
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?
Kartik
Can't you figure it out from the code itself? You are initializing l1 and l2 as null pointers and then accessing them as if they are valid pointers in the very next line. There could be other issues as well.
Assuming there is a Node class having val and *next as its members. If I write below code in main function, Node* N1 = new Node(); N1->val = 1; N1->next = new Node(); N1->val = 2; What happens to val if I do this? Not getting errors but results are not correct either(as expected😅).
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
Oh nice, Is this possible then? N1->next->val = 2; N1->next->next->val = 3; N1->next->next->next->val = 4; And so on...
Yes that is possible. Instead it would be easier to do something like Node* N2=N1->next; N2->val=2 and so on
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...
Ok, instead of doing this, create new nodes instead. Am i right? Thanks btw, the problem is solved👍
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 🙁
ㅤㅤㅤ
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
artemetra 🇺🇦
Any one help me with division
you define div as an int, but your function return type if float
Anonymous
I tried using float too
Anonymous
But if I divide 1/2 the result is 0
artemetra 🇺🇦
try with these
coal
that's why it looks horribly complicated
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
coal
If I divide 1/2 it is still 0
essentially because you're performing integer division
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;
artemetra 🇺🇦
1/2 is 0.5, casted to an integer it returns 0
why does a division of two ints get casted to a float🧐
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
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
artemetra 🇺🇦
the operator / when provided two int arguments returns an integer
i always thought it returned a double/float even when two ints are passed, thanks
coal
i always thought it returned a double/float even when two ints are passed, thanks
it's these kind of things you never discover until you have the issue lol
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
rijan i won't code for you, you need to solve your challenges by yourself