Where is the difference between these code? Shouldn't output be the same?What is the difference between #include <filename> and #include “filename”?What are the differences between a pointer variable and a reference variable in C++?What is the difference between g++ and gcc?Difference between 'struct' and 'typedef struct' in C++?Difference between private, public, and protected inheritanceWhat is the difference between const int*, const int * const, and int const *?Difference between static and shared libraries?What is the difference between 'typedef' and 'using' in C++11?Can code that is valid in both C and C++ produce different behavior when compiled in each language?Difference between `constexpr` and `const`
What does the Conservative Party expelling an MP mean?
What's the difference between a share and a stock?
Why is に used with this verb?
What is hot spotting in the context of adding files to tempdb?
A magician's sleight of hand
How can I implement regular expressions on an embedded device?
Is using different public keys for different peers safer than reusing the public key, beyond forward secrecy - x25519
Is a paralyzed creature limp or rigid?
ASCII Maze Rendering 3000
Does an antenna tuner remove standing waves from a transmission line?
First Number to Contain Each Letter
Is the Levitate spell supposed to basically disable a melee-based enemy?
How do I delete cookies from a specific site?
Are buttons really enough to bound validities by S4.2?
Global variables and information security
Arduino I2C Wire.onReceive hangs after a few loops
Do I have to rename all creatures in a new world?
How do I anonymously report the Establishment Clause being broken?
Does blackhole merging breaks their event horizon seggregation?
Why are all volatile liquids combustible
How can I oppose my advisor granting gift authorship to a collaborator?
Why don't they build airplanes from 3D printer plastic?
What's the point of this macro?
Where on Earth is it easiest to survive in the wilderness?
Where is the difference between these code? Shouldn't output be the same?
What is the difference between #include <filename> and #include “filename”?What are the differences between a pointer variable and a reference variable in C++?What is the difference between g++ and gcc?Difference between 'struct' and 'typedef struct' in C++?Difference between private, public, and protected inheritanceWhat is the difference between const int*, const int * const, and int const *?Difference between static and shared libraries?What is the difference between 'typedef' and 'using' in C++11?Can code that is valid in both C and C++ produce different behavior when compiled in each language?Difference between `constexpr` and `const`
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I Have written a program(first one) for an OJ problem, But output wasn't right. Then in online, I found given the second program, which looks almost like mine, but the output isn't the same! Can anyone help me to know where I am making a mistake? Thanks in advance.
Note: I ran both programs in same compiler
Input was
3
5
51927 56551 48919
14433 22446 13324
85506 99376 63634
93997 63631 83866
41720 16645 16267
4
34401 73409 48126
80906 29491 46483
79890 23963 57954
67975 70273 65231
4
17044 82753 40300
30368 51031 96851
10353 81816 19296
94218 83673 18672
1st Code(Mine)
#include<iostream>
using namespace std;
int main()
int n, f;
cin>>n;
while(n--)
cin>>f;
long long total=0;
int area;
int animals, enviroment;
for(int i=0;i<f;i++)
cin>>area>>animals>>enviroment;
total+=enviroment*area;
cout<<total<<endl;
2nd Code(from online)
#include<cstdio>
using namespace std;
int main()
int T,f,a,b,c;
long long sum;
scanf("%d",&T);
for(int i=0;i<T;i++)
scanf("%d",&f);
sum=0;
for(int j=0;j<f;j++)
scanf("%d %d %d",&a,&b,&c);
sum+=(long long)a*c;
printf("%lldn",sum);
return 0;
My output(for the 1st program)
-444446533
1595456521
1292087056
Online output(from the 2nd program, which is accepted)
16735422651
14480358409
5587054352
c++ onlinejudge
add a comment |
I Have written a program(first one) for an OJ problem, But output wasn't right. Then in online, I found given the second program, which looks almost like mine, but the output isn't the same! Can anyone help me to know where I am making a mistake? Thanks in advance.
Note: I ran both programs in same compiler
Input was
3
5
51927 56551 48919
14433 22446 13324
85506 99376 63634
93997 63631 83866
41720 16645 16267
4
34401 73409 48126
80906 29491 46483
79890 23963 57954
67975 70273 65231
4
17044 82753 40300
30368 51031 96851
10353 81816 19296
94218 83673 18672
1st Code(Mine)
#include<iostream>
using namespace std;
int main()
int n, f;
cin>>n;
while(n--)
cin>>f;
long long total=0;
int area;
int animals, enviroment;
for(int i=0;i<f;i++)
cin>>area>>animals>>enviroment;
total+=enviroment*area;
cout<<total<<endl;
2nd Code(from online)
#include<cstdio>
using namespace std;
int main()
int T,f,a,b,c;
long long sum;
scanf("%d",&T);
for(int i=0;i<T;i++)
scanf("%d",&f);
sum=0;
for(int j=0;j<f;j++)
scanf("%d %d %d",&a,&b,&c);
sum+=(long long)a*c;
printf("%lldn",sum);
return 0;
My output(for the 1st program)
-444446533
1595456521
1292087056
Online output(from the 2nd program, which is accepted)
16735422651
14480358409
5587054352
c++ onlinejudge
Try castingenviromentorareatolong longbefore the multiplication (or just make themlong longto begin with).
– Cornstalks
Mar 28 at 3:50
thanks, it solved the problem, but can u explain why I had to cast?
– mahin
Mar 28 at 3:53
add a comment |
I Have written a program(first one) for an OJ problem, But output wasn't right. Then in online, I found given the second program, which looks almost like mine, but the output isn't the same! Can anyone help me to know where I am making a mistake? Thanks in advance.
Note: I ran both programs in same compiler
Input was
3
5
51927 56551 48919
14433 22446 13324
85506 99376 63634
93997 63631 83866
41720 16645 16267
4
34401 73409 48126
80906 29491 46483
79890 23963 57954
67975 70273 65231
4
17044 82753 40300
30368 51031 96851
10353 81816 19296
94218 83673 18672
1st Code(Mine)
#include<iostream>
using namespace std;
int main()
int n, f;
cin>>n;
while(n--)
cin>>f;
long long total=0;
int area;
int animals, enviroment;
for(int i=0;i<f;i++)
cin>>area>>animals>>enviroment;
total+=enviroment*area;
cout<<total<<endl;
2nd Code(from online)
#include<cstdio>
using namespace std;
int main()
int T,f,a,b,c;
long long sum;
scanf("%d",&T);
for(int i=0;i<T;i++)
scanf("%d",&f);
sum=0;
for(int j=0;j<f;j++)
scanf("%d %d %d",&a,&b,&c);
sum+=(long long)a*c;
printf("%lldn",sum);
return 0;
My output(for the 1st program)
-444446533
1595456521
1292087056
Online output(from the 2nd program, which is accepted)
16735422651
14480358409
5587054352
c++ onlinejudge
I Have written a program(first one) for an OJ problem, But output wasn't right. Then in online, I found given the second program, which looks almost like mine, but the output isn't the same! Can anyone help me to know where I am making a mistake? Thanks in advance.
Note: I ran both programs in same compiler
Input was
3
5
51927 56551 48919
14433 22446 13324
85506 99376 63634
93997 63631 83866
41720 16645 16267
4
34401 73409 48126
80906 29491 46483
79890 23963 57954
67975 70273 65231
4
17044 82753 40300
30368 51031 96851
10353 81816 19296
94218 83673 18672
1st Code(Mine)
#include<iostream>
using namespace std;
int main()
int n, f;
cin>>n;
while(n--)
cin>>f;
long long total=0;
int area;
int animals, enviroment;
for(int i=0;i<f;i++)
cin>>area>>animals>>enviroment;
total+=enviroment*area;
cout<<total<<endl;
2nd Code(from online)
#include<cstdio>
using namespace std;
int main()
int T,f,a,b,c;
long long sum;
scanf("%d",&T);
for(int i=0;i<T;i++)
scanf("%d",&f);
sum=0;
for(int j=0;j<f;j++)
scanf("%d %d %d",&a,&b,&c);
sum+=(long long)a*c;
printf("%lldn",sum);
return 0;
My output(for the 1st program)
-444446533
1595456521
1292087056
Online output(from the 2nd program, which is accepted)
16735422651
14480358409
5587054352
c++ onlinejudge
c++ onlinejudge
asked Mar 28 at 3:48
mahinmahin
83 bronze badges
83 bronze badges
Try castingenviromentorareatolong longbefore the multiplication (or just make themlong longto begin with).
– Cornstalks
Mar 28 at 3:50
thanks, it solved the problem, but can u explain why I had to cast?
– mahin
Mar 28 at 3:53
add a comment |
Try castingenviromentorareatolong longbefore the multiplication (or just make themlong longto begin with).
– Cornstalks
Mar 28 at 3:50
thanks, it solved the problem, but can u explain why I had to cast?
– mahin
Mar 28 at 3:53
Try casting
enviroment or area to long long before the multiplication (or just make them long long to begin with).– Cornstalks
Mar 28 at 3:50
Try casting
enviroment or area to long long before the multiplication (or just make them long long to begin with).– Cornstalks
Mar 28 at 3:50
thanks, it solved the problem, but can u explain why I had to cast?
– mahin
Mar 28 at 3:53
thanks, it solved the problem, but can u explain why I had to cast?
– mahin
Mar 28 at 3:53
add a comment |
1 Answer
1
active
oldest
votes
enviroment and area are both int, which presumably means they are 32-bit values that can store values in the range -2,147,483,648 to 2,147,483,647 (inclusive).
Multiplying two large values can exceed this range. For example, squaring 2,147,483,647 will produce a value that requires more than 32-bits to store. Exceeding this range results in undefined behavior.
long long is (at least) 64-bits and can store values in the range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (inclusive). It's large enough to hold the value of 2,147,483,647 squared.
Thus, you need to make sure the multiplication is done using long long variables. You can do this by either casting enviroment and/or area to long long before the multiplication, or you can just make them long long.
P.S. enviroment is misspelled (it's missing an n: environment).
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55389876%2fwhere-is-the-difference-between-these-code-shouldnt-output-be-the-same%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
enviroment and area are both int, which presumably means they are 32-bit values that can store values in the range -2,147,483,648 to 2,147,483,647 (inclusive).
Multiplying two large values can exceed this range. For example, squaring 2,147,483,647 will produce a value that requires more than 32-bits to store. Exceeding this range results in undefined behavior.
long long is (at least) 64-bits and can store values in the range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (inclusive). It's large enough to hold the value of 2,147,483,647 squared.
Thus, you need to make sure the multiplication is done using long long variables. You can do this by either casting enviroment and/or area to long long before the multiplication, or you can just make them long long.
P.S. enviroment is misspelled (it's missing an n: environment).
add a comment |
enviroment and area are both int, which presumably means they are 32-bit values that can store values in the range -2,147,483,648 to 2,147,483,647 (inclusive).
Multiplying two large values can exceed this range. For example, squaring 2,147,483,647 will produce a value that requires more than 32-bits to store. Exceeding this range results in undefined behavior.
long long is (at least) 64-bits and can store values in the range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (inclusive). It's large enough to hold the value of 2,147,483,647 squared.
Thus, you need to make sure the multiplication is done using long long variables. You can do this by either casting enviroment and/or area to long long before the multiplication, or you can just make them long long.
P.S. enviroment is misspelled (it's missing an n: environment).
add a comment |
enviroment and area are both int, which presumably means they are 32-bit values that can store values in the range -2,147,483,648 to 2,147,483,647 (inclusive).
Multiplying two large values can exceed this range. For example, squaring 2,147,483,647 will produce a value that requires more than 32-bits to store. Exceeding this range results in undefined behavior.
long long is (at least) 64-bits and can store values in the range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (inclusive). It's large enough to hold the value of 2,147,483,647 squared.
Thus, you need to make sure the multiplication is done using long long variables. You can do this by either casting enviroment and/or area to long long before the multiplication, or you can just make them long long.
P.S. enviroment is misspelled (it's missing an n: environment).
enviroment and area are both int, which presumably means they are 32-bit values that can store values in the range -2,147,483,648 to 2,147,483,647 (inclusive).
Multiplying two large values can exceed this range. For example, squaring 2,147,483,647 will produce a value that requires more than 32-bits to store. Exceeding this range results in undefined behavior.
long long is (at least) 64-bits and can store values in the range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (inclusive). It's large enough to hold the value of 2,147,483,647 squared.
Thus, you need to make sure the multiplication is done using long long variables. You can do this by either casting enviroment and/or area to long long before the multiplication, or you can just make them long long.
P.S. enviroment is misspelled (it's missing an n: environment).
answered Mar 28 at 4:06
CornstalksCornstalks
28.2k11 gold badges60 silver badges118 bronze badges
28.2k11 gold badges60 silver badges118 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55389876%2fwhere-is-the-difference-between-these-code-shouldnt-output-be-the-same%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Try casting
enviromentorareatolong longbefore the multiplication (or just make themlong longto begin with).– Cornstalks
Mar 28 at 3:50
thanks, it solved the problem, but can u explain why I had to cast?
– mahin
Mar 28 at 3:53