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;








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









share|improve this question
























  • 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


















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









share|improve this question
























  • 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














0












0








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









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 28 at 3:48









mahinmahin

83 bronze badges




83 bronze badges















  • 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


















  • 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

















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













1 Answer
1






active

oldest

votes


















3
















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).






share|improve this answer
























    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
    );



    );













    draft saved

    draft discarded


















    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









    3
















    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).






    share|improve this answer





























      3
















      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).






      share|improve this answer



























        3














        3










        3









        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).






        share|improve this answer













        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).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 28 at 4:06









        CornstalksCornstalks

        28.2k11 gold badges60 silver badges118 bronze badges




        28.2k11 gold badges60 silver badges118 bronze badges





















            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.



















            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해