mito
what's the use of #define I see it in a lot of code in github projects..
klimi
preprocessor stuff? um... that's the things that preprocessor reads and act some way (like another programming language)
mito
please, explain like im 5.
°Moohaamed°
what's the use of #define I see it in a lot of code in github projects..
I think it look like changing syntax, When you say.. #define R return 0; That's mean when you say R , it's like you say return 0;
klimi
(string substitution)
klimi
https://en.cppreference.com/w/cpp/preprocessor https://www.geeksforgeeks.org/cc-preprocessors/ https://en.wikipedia.org/wiki/C_preprocessor
°Moohaamed°
so its like variable, but for storing piece of code ?
No exactly.. Ex: #define hee int hee number = 3; it means you made an integer variable called number.
Anonymous
Why is the result different for same dataset in torchtext.legecy.text when i change the position of data in the csv file?
mito
No exactly.. Ex: #define hee int hee number = 3; it means you made an integer variable called number.
suppose if I do #define for_loop for(int i=0; i<n; i++) is it possible to give value to n when i use for_loop ? if so, how can I ?
°Moohaamed°
You will make a variable called n and will give him the value you want.. And when you say for_loop that's mean you make a for loop from 0 to the n 🤝 Code it and you will understand well 🔥
mito
#define for_loop(n)
tnx, can I use multiple parameters ?
mito
yes you can.
if I did #define for_loop(for) for(int i=0; i<n; i++) will it replace the for ? just asking.
Vlad
if I did #define for_loop(for) for(int i=0; i<n; i++) will it replace the for ? just asking.
#define loop(N) for(int i = 0; i < N; ++i) loop(20) { printf("%d", i); }
Vlad
But obviously this approach has a lot of issues
Vlad
What if i was declared above? What if somebody put a++ into the argument of loop?
Anonymous
What if i was declared above? What if somebody put a++ into the argument of loop?
Why would i being defined above be a problem in your example?
Vlad
loop(20) doesn't say shit that it declares i
mito
What if i was declared above? What if somebody put a++ into the argument of loop?
but the programmer will know what to put there, right...
Vlad
So somebody might assume that he uses the variable that he declared above
Suka
what are those?
usually something related with boundary checking or different data type (in case someone forget the macro hehe). but now linter or compiler can give some warning. cmiiw
Vlad
And that's a silent bug in your code
Anonymous
can i forward this out of context?
Not sure i understand what you mean?
Anonymous
Depends on what programmer is expecting
I was referring to your example alone. I thought you were saying that it would lead to a redefinition error. And as far as programmers using macro invocations are concerned, they ought to know what the macro replacement would be before using it. So if the macro doesnt say shit, then the programmer should dig that shit up
Vlad
Cause shit is hidden and you cant be sure
Anonymous
Yes macros can be evil but they are a necessary evil at times and programmers should be using it only when they know what they are doing. Usually if you choose good names for your macro, in most cases it would be evident to the user what the purpose of the macro is.
mito
So it basically kills readability lol
+ I was reading a code . they used that #define for everything... I couldn't keep the names and their respective statement in my mind when reading the actual code..
Diypa
Need some help. Thank you
Diypa
string diypa571(string text) { string str = text; int i = 0, alphabet[26] = {0}, j; while (str[i] != '\0') { if (str[i] >= 'A' && str[i] <= 'Z') { j = str[i] - 'A'; ++alphabet[j]; } ++i; } for (i = 0; i < 26; i++) { cout<< char(i + 'A')<<" : "<< alphabet[i] << endl; } }
Diypa
I want this in a separate function.
Diypa
for (i = 0; i < 26; i++) { cout<< char(i + 'A')<<" : "<< alphabet[i] << endl; }
Diypa
Did try this, but no success... void display(){ int i =0; for (i = 0; i < 26; i++) { cout<< char(i + 'A')<<" : "<< alphabet[i] << endl; } }
Papa
#include<iostream> using namespace std; int get(int arr[],int index,int n) { if(index<=n) {return arr[index]; } } int set(int arr[],int a,int k) { { return (arr[a]=k); } } int maximum(int arr[],int n) { int max=0; for(int i=0;i<=n;i++) { if(arr[i]>max) {max=arr[i]; } } return max; } int minimum(int arr[],int n) { int min=arr[0]; for(int i=0;i<n;i++) {if(arr[i]<min) { min=arr[i]; } } return min; } int main() { cout<<"enter the size of array who want to create"<<endl; int n; cin>>n; int arr[n]; for(int i=0;i<=n;i++) { cin>>arr[i]; } int index; cout<<"enter the index you want to see"<<endl; cin>>index; cout<<get( arr, index, n); int a; cout<<endl<<"enter the index you want to replace"<<endl; cin>>a; int k; cout<<"enter the key element"<<endl; cin>>k; set(arr,a,k); cout<<endl<<"congratulations you have replaced your element"<<endl; cout<<"do you want to see the current array (y/n)"<<endl; char option; cin>>option; if(option='y') { for(int i=0;i<=n;i++) {cout<<arr[i]<<endl; } } else { cout<<"thank you"<<endl; } maximum(arr,n); minimum(arr,n); }
Papa
can anyone tell me why maximum and minimum function are not execi=uting
Papa
executing
Diypa
Nothing is wrong with your code...
Anonymous
#include<iostream> using namespace std; int get(int arr[],int index,int n) { if(index<=n) {return arr[index]; } } int set(int arr[],int a,int k) { { return (arr[a]=k); } } int maximum(int arr[],int n) { int max=0; for(int i=0;i<=n;i++) { if(arr[i]>max) {max=arr[i]; } } return max; } int minimum(int arr[],int n) { int min=arr[0]; for(int i=0;i<n;i++) {if(arr[i]<min) { min=arr[i]; } } return min; } int main() { cout<<"enter the size of array who want to create"<<endl; int n; cin>>n; int arr[n]; for(int i=0;i<=n;i++) { cin>>arr[i]; } int index; cout<<"enter the index you want to see"<<endl; cin>>index; cout<<get( arr, index, n); int a; cout<<endl<<"enter the index you want to replace"<<endl; cin>>a; int k; cout<<"enter the key element"<<endl; cin>>k; set(arr,a,k); cout<<endl<<"congratulations you have replaced your element"<<endl; cout<<"do you want to see the current array (y/n)"<<endl; char option; cin>>option; if(option='y') { for(int i=0;i<=n;i++) {cout<<arr[i]<<endl; } } else { cout<<"thank you"<<endl; } maximum(arr,n); minimum(arr,n); }
First of all you are exceeding the array limits. You should use <n and not <=n. Second you are using a variable length array which are supported by some compilers as an extension and is not a standard requirement. Use vector instead. And the reason why max and min element dont get printed is because maximum and minimum function dont print anything. They just return the maximum and minimum element. You have to print them out yourself.
Anonymous
thnx but its not working
It would if you did it correctly. Did you change the condition in the for loop to <n
Anonymous
yes bro
Show us the edited code.
Talula
yes bro
#include<iostream> using namespace std; int get(int arr[],int index,int n) { if(index<=n) {return arr[index]; } } int set(int arr[],int a,int k) { { return (arr[a]=k); } } int maximum(int arr[],int n) { int max=0; for(int i=0;i<=n;i++) { if(arr[i]>max) {max=arr[i]; } } return max; } int minimum(int arr[],int n) { int min=arr[0]; for(int i=0;i<n;i++) {if(arr[i]<min) { min=arr[i]; } } return min; } int main() { cout<<"enter the size of array who want to create"<<endl; int n; cin>>n; int arr[n]; for(int i=0;i<=n;i++) { cin>>arr[i]; } int index; cout<<"enter the index you want to see"<<endl; cin>>index; cout<<get( arr, index, n); int a; cout<<endl<<"enter the index you want to replace"<<endl; cin>>a; int k; cout<<"enter the key element"<<endl; cin>>k; int v = get(arr, k, n); set(arr,a,v); cout<<endl<<"congratulations you have replaced your element"<<endl; cout<<"do you want to see the current array (y/n)"<<endl; char option; cin>>option; if(option='y') { for(int i=0;i<=n;i++) {cout<<arr[i]<<endl; } } else { cout<<"thank you"<<endl; } maximum(arr,n); minimum(arr,n); } This works, you have to find the value and replace the value, you don't put index of it but the value you want to replace using set.
Talula
You're using index and trying to replace the index
Talula
It is value you have to replace with set.
Papa
yeah, i got it bro thanks all
Anonymous
You're using index and trying to replace the index
His question was why the maximum and minimum elements were not getting printed. And you have made the same mistakes he has in his code.
Talula
I just changed here... ```int v = get(arr, k, n); set(arr,a,v);```
Murugaa
Write a complete C++ program which can multiply three matrices. Example A=■(&&@&&@&&) B=■(&&@&&@&&) C=■(&&@&&@&&) Your program need to read matrix A , Matrix B, and Matrix C from data file. The size of all the matrices must be at least 3 x 3 Your program must also store the result into result file.
Murugaa
can anyone help with this?
Talula
Because he isn't printing any max or min... it is just being called.
Murugaa
please need urget
Papa
Which is what I told him earlier.
pls dont fight, my dobt is clear and the code is working fine
Papa
doubt*
Talula
pls dont fight, my dobt is clear and the code is working fine
cout<<endl<<"Max"; cout<<maximum(arr,n); cout<<endl<<"Min"; cout<<minimum(arr,n);
Anonymous
Not sure i understand what you mean?
joke. i as pronoun instead of identifier :/
Talula
And yes as she said... change < to <=
Adarsh
how to return a reversed vector from any method?
Anonymous
how to return a reversed vector from any method?
auto rvec = vec | ranges::views::reverse | ranges::to_vector;
Anonymous
if you want it in a separate vector, that is
Anonymous
auto rvec = vec | ranges::views::reverse | ranges::to_vector;
He is most likely not using C++20. So you should maybe suggest Eric Niebler's library as well