Anonymous
I'll check it out. Thabk you!
I am not familiar with CLion but a quick search told me that CLion indeed uses the CMakeLists.txt file as project settings as well. This is unlike other IDEs that I have worked with. So you may have to mark the directory which contains your headers as a directory that contains prokect headers and source files. Use CMake's include_directories to mark the directories you want to be searched for header files as specified in this link : https://www.jetbrains.com/help/clion/quick-cmake-tutorial.html#inc-dirs
Heitor
long fib(long n){ if (n<2){ return 1; }; else{ return fib(n-1)+fib(n-2); } }
Anonymous
long fib(long n){ if (n<2){ return 1; }; else{ return fib(n-1)+fib(n-2); } }
a better one, using DP: int fib_topDown(int n, int dp[]) { if (dp[n] == -1) { int res; if (n == 0 or n == 1) res = n; else res = fib_topDown(n - 1, dp) + fib_topDown(n - 2, dp); dp[n] = res; } return dp[n]; }
Anonymous
and without dp: int fib_spaceOptimized(int n) { if (n = 0 or n = 1) return n; int a = 0, b = 1, c; for (int i = 2; i < n; i++) { c = a + b; a = b; b = c; } return c; }
Shiva
Ok
Heitor
a better one, using DP: int fib_topDown(int n, int dp[]) { if (dp[n] == -1) { int res; if (n == 0 or n == 1) res = n; else res = fib_topDown(n - 1, dp) + fib_topDown(n - 2, dp); dp[n] = res; } return dp[n]; }
Or this... I'm on in my cell phone and I can't teste it, but that's the logic int fib(int n){ int fibs[], i=0; for (i=0; i<=n; i++); if (i == 0){fibs[i]=0}; else if (i ==1 or i==2){fibs[i]=1}; else{fibs[i]=fibs[i]+fibs[i-1]}; return fibs }
Anonymous
Just create a main function and put the fib function inside it, after debugging it
Manav
Anyone here have used linux?
Nameful
Anyone here have used linux?
It's everyday bro
Manav
I have a serious issue with linux
Karthick
What’s the issue?
Karthick
We could be helpful if u explain about your issue Manav
Anonymous
#include <stdint.h> #include <stdio.h> void main() { uint16_t dividend , divisor ; printf(" enter the dividend and divisor \n"); scanf("%u %u" , &dividend , &divisor ); uint16_t s1,s2; s1 =( 0 != dividend%2)?1:0; s2 =(0 != divisor%2)?1:0; printf( "the value of s1 and s2 is %u and %u " , s1,s2); //jus to verify the value of s1 and s2 and here i am getting s1=0
Anonymous
Whatever value I may enter the dividend but I getting it as zero. Could you please tell me the reason ?
Ammar
#include <stdint.h> #include <stdio.h> void main() { uint16_t dividend , divisor ; printf(" enter the dividend and divisor \n"); scanf("%u %u" , &dividend , &divisor ); uint16_t s1,s2; s1 =( 0 != dividend%2)?1:0; s2 =(0 != divisor%2)?1:0; printf( "the value of s1 and s2 is %u and %u " , s1,s2); //jus to verify the value of s1 and s2 and here i am getting s1=0
[ Hmm void main. How damn hard is it for professors out there to teach int main correctly? ] You have invoked undefined behavior here, in your case, %u expects uint32_t which is 4 bytes, but you write to uint16_t storage which is 2 bytes in size. So you got 2 bytes out of bound write which may overwrite other variables. The correct specifier for uint16_t is %hu.
Ammar
See also: man 3 printf
Anonymous
Ammar
Thanks worked 🙏
Good compiler should warn you about that, you should really take advantage of it. Or even activate more warn flags like -Wall -Wextra.
Ammar
???
It's manpage, if you don't have it, you can type that on Google.
Ammar
Please be nice to the seniors with patience. Thanks.
Yes, excuse for my impolite sentence.
Ammar
/report
バレンタインがいない柴(食用不可)
バレンタインがいない柴(食用不可)
I mean... Mr void main must have lived longer than you and I ... lol
Mahdi
Better option ?😅
I think in most cases that code doesn't interact with other codes it has no difference to use void or int.
Anonymous
Don't worry, I'm not calling you out. Just a few messages you can tag people in to spare saliva
Anonymous
@K_P_Shetty read this message and the following two others
Yea...I will check right now, thanks bro
Ammar
I'd personally change the last function for the reasons explained above. - From: main() { ... } - To: int main(void) { ... return 0; } Fun fact: void main() is the old way to do it which doesn't solve the problems I mentioned. You'll certainly find this style in old tutorials on the internet.
Yes, really appreciate your explanation. As always, use int main(void) if you don't utilize argc and argv. Use int fx(void) instead of int fx() to omit the zeroing rax as System V ABI mandates %rax should contain the number of vector register passed to function and blahblah. Not everybody should know the technical detail, but at least they know the best practice. Nice discussion.
Ammar
The return value of int main(void) is used as exit code these days.
Heitor
Or this... I'm on in my cell phone and I can't teste it, but that's the logic int fib(int n){ int fibs[], i=0; for (i=0; i<=n; i++); if (i == 0){fibs[i]=0}; else if (i ==1 or i==2){fibs[i]=1}; else{fibs[i]=fibs[i]+fibs[i-1]}; return fibs }
int fib(int n){ int fibs[n], i; for (i=0; i<n; i++){ if (i == 0){fibs[i]=0;} else if (i ==1 || i==2){fibs[i]=1;} else{fibs[i]=fibs[i-2]+fibs[i-1];}; printf("Num %i ==> %i\n", i, fibs[i]); }; return 1; }
Anonymous
similar reason why float and double have separate specifiers when using with scanf but not with printf
Ammar
in systems where %u really expects uint32_t (i.e. unsigned int is 32 bits), integer promotion guarantees that the uint16_t argument is converted to int32_t. no undefined behaviour there.
> integer promotion guarantees that the uint16_t argument is converted to int32_t. Yes, I am sure that's what happens with printf, but in this case scanf() does not actually accept integer, instead it accepts pointers. So the integer promotion still happens, but to uintptr_t rather than int. But that's not the main problem here, the main problem is the memory write itself. scanf with %u writes sizeof(unsigned int) in size. scanf with %hu writes sizeof(unsigned short) in size. Those size differences cause out of bound write which leads to UB.
Anonymous
sorry
Ammar
oh wait me dumb dumb, i saw printf on both lines for some reason
Ah right, I should have suggested man 3 scanf rather than man 3 printf.
Anonymous
#include<iostream.h> #include<conio.h> int main() { int a,b,x,y; numbers(a,b); swap(x,y); return 0; } void swap(int &i,int &j) { int z; z=i; i=j; j=z; cout<<"sfter swapping"<<i<<j; } void numbers(int k, int l) { cout<<"entee no"; cin>>k>>l; cout<<"before swapping"<<k<<l; }
Anonymous
Guys anything wrong with this code?
Anonymous
It's not compiling
touhou
Guys anything wrong with this code?
void swap(int &i,int &j) this ig.
Anonymous
Did not get bro
touhou
Did not get bro
Can you send the error message?
Anonymous
Use of undeclared identifier 'numbers' numbers(a,b)
touhou
Use of undeclared identifier 'numbers' numbers(a,b)
your number function def in below the main() use function prototype or put the numbers function def above main()
Anonymous
Exactly
Thanks alot bro
Mahdi
Oh but better to use int?
There is no performance difference but I think it is best practice to stick to standards
Anshul
#include <iostream> #include<vector> #include<string> using namespace std; vector<string> v; void addSub(int n,string s) { int i=0; int j=0; char ans[s.length()]; while(n>0) { int last_bit=(n&1); if(last_bit==1) { ans[i]=s[j]; i++; } j++; n=n>>1; } v.push_back((string)ans); } void findAll(string s) { int n=s.length(); for(int i=0;i<(1<<n);i++) { addSub(i,s); } } int main() { string s="abc"; findAll(s); for(auto x:v) { cout<<x<<endl; } return 0; } i wrote this code to find subsequences of a string. but it gives me wrong answer and i can't find out why
Anshul
this is the output produced a b ab cb ac bc abc. but in the output instead of cb i should get c.
...
Any books available for C programming for beginners
Anshul
/get cbook
...
In pdf form?
Anonymous
thanks I got it. But Why don't I need to cast here
Because arrays will decay to pointers, and push_back on an lvalue vector<string> has an argument const string& which can be passed a char* argument (the compiler will automatically generate a temporary string and bind it to the const string& reference)
Anshul
Thanks 👍
Anonymous
There is no performance difference but I think it is best practice to stick to standards
Ok fine, I don't get why my professor still uses that old format.
Mahdi
Ok fine, I don't get why my professor still uses that old format.
For curiosity, if it is possible ask him to see what is the intention. maybe there would be something that we are not aware of it.
Anonymous
Any c/c++ course
klimi
Any c/c++ course
https://www.edx.org/learn/c-plus-plus
SS
Can anyone help me in debugging a code?
Anonymous
What is meant by null pointer ? Why to initialise uninitialised pointer to null?