Pavel
first time hearing matter of luck in computer science :)
There's a good term actually for that - Undefined Behavior (UB in short). If you do something that results in UB, you can't rely on the behavior of your program anymore from this moment. So as a programmer you want to avoid UB. And it's no need to examine the results of UB since they can be reproducible only in some specific conditions (e.g. phase of the moon). https://en.wikipedia.org/wiki/Undefined_behavior
Pavel
then it must be the job of the compiler to make sure memory is properly assigned, right?
Well, bad news, a lot of responsibility of not getting into UB is on you as a programmer if you write in C or C++ :)
Pavel
In C++ it's kind of better because there are more high level concepts that can check things for you, but there can be overhead (runtime, compile time, or increase of complexity) of that checks (bearable, in my opinion).
Anonymous
Well, bad news, a lot of responsibility of not getting into UB is on you as a programmer if you write in C or C++ :)
then, those people who wrote those compilers must have gone through a lot of UB
Pavel
then, those people who wrote those compilers must have gone through a lot of UB
Things that are UB are mostly documented in the language standards, compiler writers actually benefit from that because they can write more efficient compilers for C or C++ code. And for people who write code it's just a matter of not doing things that are wrong (that are UB).
Gulshan
#include <cstdlib> #include <iostream> #include <ostream> int main() { int rank; std::cin >> rank; int diagonal1{0}, diagonal2{0}; int matrix[rank][rank]; // for diagonal 1 and also for inputting matrix for (int i = 1; i <= rank; i++) { for (int j = 1; j <= rank; j++) { std::cin >> matrix[i][j]; if (i == j) { diagonal1 = diagonal1 + matrix[i][j]; } } } // for diagonal 2 int i{1}, j{rank}; while (i <= rank) { diagonal2 = diagonal2 + matrix[i][j]; j--; i++; } // std::cout << "Diagonal1 : " << diagonal1 << "\n"; // std::cout << "Diagonal2 : " << diagonal2 << "\n"; std::cout << abs(diagonal1 - diagonal2); return 0; }
Gulshan
but it is working fine in my pc
Gulshan
thanks buddy
Gulshan
didnt knew that🤔
Gulshan
But I didn't understood that.... My code should also work fine naa ? As it is working fine in my pc
Pavel
But I didn't understood that.... My code should also work fine naa ? As it is working fine in my pc
Reading/writing out of bounds of an array is undefined behavior (we just descussed it a few messages above). Try running locally under the debugger and look at the values in your array when you assign the last values. I guess it either will also crash under debug or will show you that you overwrite values from the neighboring memory (e.g. values of the next array). But maybe it's a bad advise, again because UB don't need to be investigated if we know that it's UB. But I think it will be interesting for you to see what happens.
LDNa
/ get ide
MAC
/get book
MAC
/get cbook
MAC
/get
Talula
What do you mean? You want people to Argu?
MRT
std::map<std::string,std::map<customtype1,customtype2>> xx ;
MRT
how to insert object in this map?
MRT
xx[key].insert(customtype1,customtype2) not working !
Codigo
Hi guys, I have a question. When are multidimensional arrays useful? In which kind of situation do you need more than 2 dimensions?
Codigo
Wendy
Hi guys
Wendy
Have written a function that will ask the user to input 2 number and add it together and the result should generate for loop
Anonymous
Hey can someone help me with my code?
Anonymous
Sorry for interrupting if I did
Anonymous
Okay so let me send you the code?
Ammar
https://www.nohello.com/2013/01/please-dont-say-just-hello-in-chat.html
Anonymous
Okay
Anonymous
#include<iostream> #include<fstream> #include<string> #include<cmath> using namespace std; int main() { char x; float y; string z; cout<<"Enter the acronym"<<endl; cin>>z; cout<<"Enter the first number\n"; while(std::getline(cin,x)); { if(z=="sin(x)") { cout<<sin(x); } else if(z=="cos(x)") { cout<<cos(x); } else if(z=="tan(x)") { cout<<tan(x); } } return 0; }
Anonymous
I keep getting an error around the get line function
Anonymous
I keep getting an error around the get line function
I tried so many times but I can't get rid of the error
Ammar
Consider to use std::string instead.
Anonymous
Oh my God
Ammar
Array of chars may also be appropriate.
Anonymous
open docs
I'm sorry what?
Ganesh
👋
Anonymous
I'm sorry what?
So You are coding, but don't use documentation?
Ammar
I'm sorry what?
There is a site https://en.cppreference.com It documents C++ standard library.
Anonymous
So You are coding, but don't use documentation?
Oh I do. I save it as a file not a document. The file directly goes to the code in my compiler
Anonymous
Would it work?
Wendy
Hi everyone
Anonymous
Lol wtf
Never mind. I'll send it to you. Once please
Wendy
Can some help me with my code
Anonymous
Don't send me anything
Anonymous
open docs
What does this mean then?
Wendy
Am writing a code in c++
Ammar
Why don't people just drop the question instead of saying "Can someone help with my code?"?
Anonymous
Anonymous
cppreference.com
Thanks for the help
Wendy
Ok
Wendy
A writing a function that will ask the user to input two number and add it together and the result to generate for loop
Anonymous
😂
What? Is there a problem?
Anonymous
What? Is there a problem?
nothing, I just like your joke
Anonymous
I'm not
ohh kk, I thought you were joking
Wendy
The help I need is while result is updating I want it to stop at the number that the user input and show me this is the first number the user input ad second number
Anonymous
ohh kk, I thought you were joking
Okay. Well as long as it was funny that's fine. Bye
Wendy
Is there not anybody to help
Wendy
I need help 🙄🙄🙄🙄