This logic seems correct but its not working . Is there any mistake? [duplicate]Using a for loop to reverse an array does nothingWhy use pointers?Constructor initialization-list evaluation orderWhy are elementwise additions much faster in separate loops than in a combined loop?Passing a 2D array to a C++ functionmerge sort incorrect outputHow to detect a Christmas Tree?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsPercent of correct answers program challengeC++ arrays and functions, code not working outI have written a code to find reverse of array elements but it doesnot gives required output

Where does the expression "triple-A" come from?

What is my breathable atmosphere composed of?

What exactly is a marshrutka (маршрутка)?

How can I discourage sharing internal API keys within a company?

Why is the Digital 0 not 0V in computer systems?

Linear Programming with additional "if-then"/"Default to zero" constraints?

What's the biggest organic molecule that could have a smell?

Has SHA256 been broken by Treadwell Stanton DuPont?

What did Aquinas do for recreation?

Do all humans have an identical nucleotide sequence for certain proteins, e.g haemoglobin?

What officially disallows US presidents from driving?

Where can I find vomiting people?

Is low emotional intelligence associated with right-wing and prejudiced attitudes?

How are aircraft depainted?

Can the UK veto its own extension request?

Will replacing a fake visa with a different fake visa cause me problems when applying for a legal study permit?

Relocation error, error code (127) after last updates

Selecting 2 column in an Inner join

Splice or replace

Random point on a sphere

In Germany, how can I maximize the impact of my charitable donations?

Is a suit against a Univeristy Dorm for changing policies on a whim likely to succeed (USA)?

Can a corpse possessed by a Dybbuk be turned via Turn Undead?

Leaving out pronouns in informal conversation



This logic seems correct but its not working . Is there any mistake? [duplicate]


Using a for loop to reverse an array does nothingWhy use pointers?Constructor initialization-list evaluation orderWhy are elementwise additions much faster in separate loops than in a combined loop?Passing a 2D array to a C++ functionmerge sort incorrect outputHow to detect a Christmas Tree?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsPercent of correct answers program challengeC++ arrays and functions, code not working outI have written a code to find reverse of array elements but it doesnot gives required output






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0
















This question already has an answer here:



  • Using a for loop to reverse an array does nothing [duplicate]

    1 answer



The program is to reverse the contents of an array .
For example if the array contained 1,2,3,4,5
it should display 5,4,3,2,1



for(i=0;i<n;i++)

tmp=a[i];
a[i]=a[n-1-i];
a[n-1-i]=tmp;










share|improve this question
















marked as duplicate by StoryTeller, YSC c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 28 at 10:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 3





    If you look closely, this loop reverses twice resulting in the original array.

    – Wander3r
    Mar 28 at 9:55











  • after this if i use another for loop and print the array... I am getting 1,2,3,4,5

    – PizzaLover1134
    Mar 28 at 9:55











  • can you please explain further?

    – PizzaLover1134
    Mar 28 at 9:56






  • 2





    @Targeryen take a piece of paper and a pencil and you should be able to figure out yourself what's wrong with your approach

    – Jabberwocky
    Mar 28 at 9:56






  • 1





    @suvenpandey You don't have to post your own answer as a comment.

    – CinCout
    Mar 28 at 10:03

















0
















This question already has an answer here:



  • Using a for loop to reverse an array does nothing [duplicate]

    1 answer



The program is to reverse the contents of an array .
For example if the array contained 1,2,3,4,5
it should display 5,4,3,2,1



for(i=0;i<n;i++)

tmp=a[i];
a[i]=a[n-1-i];
a[n-1-i]=tmp;










share|improve this question
















marked as duplicate by StoryTeller, YSC c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 28 at 10:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 3





    If you look closely, this loop reverses twice resulting in the original array.

    – Wander3r
    Mar 28 at 9:55











  • after this if i use another for loop and print the array... I am getting 1,2,3,4,5

    – PizzaLover1134
    Mar 28 at 9:55











  • can you please explain further?

    – PizzaLover1134
    Mar 28 at 9:56






  • 2





    @Targeryen take a piece of paper and a pencil and you should be able to figure out yourself what's wrong with your approach

    – Jabberwocky
    Mar 28 at 9:56






  • 1





    @suvenpandey You don't have to post your own answer as a comment.

    – CinCout
    Mar 28 at 10:03













0












0








0


1







This question already has an answer here:



  • Using a for loop to reverse an array does nothing [duplicate]

    1 answer



The program is to reverse the contents of an array .
For example if the array contained 1,2,3,4,5
it should display 5,4,3,2,1



for(i=0;i<n;i++)

tmp=a[i];
a[i]=a[n-1-i];
a[n-1-i]=tmp;










share|improve this question

















This question already has an answer here:



  • Using a for loop to reverse an array does nothing [duplicate]

    1 answer



The program is to reverse the contents of an array .
For example if the array contained 1,2,3,4,5
it should display 5,4,3,2,1



for(i=0;i<n;i++)

tmp=a[i];
a[i]=a[n-1-i];
a[n-1-i]=tmp;





This question already has an answer here:



  • Using a for loop to reverse an array does nothing [duplicate]

    1 answer







c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 9:55









Jabberwocky

29.9k10 gold badges43 silver badges80 bronze badges




29.9k10 gold badges43 silver badges80 bronze badges










asked Mar 28 at 9:53









PizzaLover1134PizzaLover1134

94 bronze badges




94 bronze badges





marked as duplicate by StoryTeller, YSC c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 28 at 10:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











marked as duplicate by StoryTeller, YSC c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 28 at 10:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by StoryTeller, YSC c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 28 at 10:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 3





    If you look closely, this loop reverses twice resulting in the original array.

    – Wander3r
    Mar 28 at 9:55











  • after this if i use another for loop and print the array... I am getting 1,2,3,4,5

    – PizzaLover1134
    Mar 28 at 9:55











  • can you please explain further?

    – PizzaLover1134
    Mar 28 at 9:56






  • 2





    @Targeryen take a piece of paper and a pencil and you should be able to figure out yourself what's wrong with your approach

    – Jabberwocky
    Mar 28 at 9:56






  • 1





    @suvenpandey You don't have to post your own answer as a comment.

    – CinCout
    Mar 28 at 10:03












  • 3





    If you look closely, this loop reverses twice resulting in the original array.

    – Wander3r
    Mar 28 at 9:55











  • after this if i use another for loop and print the array... I am getting 1,2,3,4,5

    – PizzaLover1134
    Mar 28 at 9:55











  • can you please explain further?

    – PizzaLover1134
    Mar 28 at 9:56






  • 2





    @Targeryen take a piece of paper and a pencil and you should be able to figure out yourself what's wrong with your approach

    – Jabberwocky
    Mar 28 at 9:56






  • 1





    @suvenpandey You don't have to post your own answer as a comment.

    – CinCout
    Mar 28 at 10:03







3




3





If you look closely, this loop reverses twice resulting in the original array.

– Wander3r
Mar 28 at 9:55





If you look closely, this loop reverses twice resulting in the original array.

– Wander3r
Mar 28 at 9:55













after this if i use another for loop and print the array... I am getting 1,2,3,4,5

– PizzaLover1134
Mar 28 at 9:55





after this if i use another for loop and print the array... I am getting 1,2,3,4,5

– PizzaLover1134
Mar 28 at 9:55













can you please explain further?

– PizzaLover1134
Mar 28 at 9:56





can you please explain further?

– PizzaLover1134
Mar 28 at 9:56




2




2





@Targeryen take a piece of paper and a pencil and you should be able to figure out yourself what's wrong with your approach

– Jabberwocky
Mar 28 at 9:56





@Targeryen take a piece of paper and a pencil and you should be able to figure out yourself what's wrong with your approach

– Jabberwocky
Mar 28 at 9:56




1




1





@suvenpandey You don't have to post your own answer as a comment.

– CinCout
Mar 28 at 10:03





@suvenpandey You don't have to post your own answer as a comment.

– CinCout
Mar 28 at 10:03












4 Answers
4






active

oldest

votes


















5
















To reverse an array you dont iterate over entire array. That would lead to reversing array twice.
Try for(i=0;i<n/2;i++)






share|improve this answer
































    0
















    You can reverse like this



    for (int i = 0; i < n--; i++)

    tmp = a[i];
    a[i] = a[n];
    a[n] = tmp;






    share|improve this answer
































      -1
















      You are reversing the array in-place, hence after the first step you have the first and the last element in its correct position. After iterating over half the elements you have the first half and the second half in its corret position. Now, iterating over all elements, you swap each element twice which results in the original array.






      share|improve this answer

























      • Thank you for explaining further

        – PizzaLover1134
        Mar 28 at 10:01







      • 1





        seems like someone is on a voting spree, if you know what is wrong would be cool to let me know

        – formerlyknownas_463035818
        Mar 28 at 10:03


















      -1
















      int i = 0, j = n - 1;
      for (i = 0; i < n/ 2; i++, j--)

      int temp = array[i];
      array[i] = array[j];
      array[j] = temp;






      share|improve this answer

























      • Although this code might (or might not) solve the problem, a good questions should also explain why this code helps and how it solves the problem.

        – BDL
        Mar 28 at 10:28



















      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      5
















      To reverse an array you dont iterate over entire array. That would lead to reversing array twice.
      Try for(i=0;i<n/2;i++)






      share|improve this answer





























        5
















        To reverse an array you dont iterate over entire array. That would lead to reversing array twice.
        Try for(i=0;i<n/2;i++)






        share|improve this answer



























          5














          5










          5









          To reverse an array you dont iterate over entire array. That would lead to reversing array twice.
          Try for(i=0;i<n/2;i++)






          share|improve this answer













          To reverse an array you dont iterate over entire array. That would lead to reversing array twice.
          Try for(i=0;i<n/2;i++)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 28 at 9:55









          Suven PandeySuven Pandey

          6883 silver badges16 bronze badges




          6883 silver badges16 bronze badges


























              0
















              You can reverse like this



              for (int i = 0; i < n--; i++)

              tmp = a[i];
              a[i] = a[n];
              a[n] = tmp;






              share|improve this answer





























                0
















                You can reverse like this



                for (int i = 0; i < n--; i++)

                tmp = a[i];
                a[i] = a[n];
                a[n] = tmp;






                share|improve this answer



























                  0














                  0










                  0









                  You can reverse like this



                  for (int i = 0; i < n--; i++)

                  tmp = a[i];
                  a[i] = a[n];
                  a[n] = tmp;






                  share|improve this answer













                  You can reverse like this



                  for (int i = 0; i < n--; i++)

                  tmp = a[i];
                  a[i] = a[n];
                  a[n] = tmp;







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 28 at 10:04









                  Deb SDeb S

                  4154 silver badges13 bronze badges




                  4154 silver badges13 bronze badges
























                      -1
















                      You are reversing the array in-place, hence after the first step you have the first and the last element in its correct position. After iterating over half the elements you have the first half and the second half in its corret position. Now, iterating over all elements, you swap each element twice which results in the original array.






                      share|improve this answer

























                      • Thank you for explaining further

                        – PizzaLover1134
                        Mar 28 at 10:01







                      • 1





                        seems like someone is on a voting spree, if you know what is wrong would be cool to let me know

                        – formerlyknownas_463035818
                        Mar 28 at 10:03















                      -1
















                      You are reversing the array in-place, hence after the first step you have the first and the last element in its correct position. After iterating over half the elements you have the first half and the second half in its corret position. Now, iterating over all elements, you swap each element twice which results in the original array.






                      share|improve this answer

























                      • Thank you for explaining further

                        – PizzaLover1134
                        Mar 28 at 10:01







                      • 1





                        seems like someone is on a voting spree, if you know what is wrong would be cool to let me know

                        – formerlyknownas_463035818
                        Mar 28 at 10:03













                      -1














                      -1










                      -1









                      You are reversing the array in-place, hence after the first step you have the first and the last element in its correct position. After iterating over half the elements you have the first half and the second half in its corret position. Now, iterating over all elements, you swap each element twice which results in the original array.






                      share|improve this answer













                      You are reversing the array in-place, hence after the first step you have the first and the last element in its correct position. After iterating over half the elements you have the first half and the second half in its corret position. Now, iterating over all elements, you swap each element twice which results in the original array.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 28 at 9:59









                      formerlyknownas_463035818formerlyknownas_463035818

                      24.6k4 gold badges32 silver badges82 bronze badges




                      24.6k4 gold badges32 silver badges82 bronze badges















                      • Thank you for explaining further

                        – PizzaLover1134
                        Mar 28 at 10:01







                      • 1





                        seems like someone is on a voting spree, if you know what is wrong would be cool to let me know

                        – formerlyknownas_463035818
                        Mar 28 at 10:03

















                      • Thank you for explaining further

                        – PizzaLover1134
                        Mar 28 at 10:01







                      • 1





                        seems like someone is on a voting spree, if you know what is wrong would be cool to let me know

                        – formerlyknownas_463035818
                        Mar 28 at 10:03
















                      Thank you for explaining further

                      – PizzaLover1134
                      Mar 28 at 10:01






                      Thank you for explaining further

                      – PizzaLover1134
                      Mar 28 at 10:01





                      1




                      1





                      seems like someone is on a voting spree, if you know what is wrong would be cool to let me know

                      – formerlyknownas_463035818
                      Mar 28 at 10:03





                      seems like someone is on a voting spree, if you know what is wrong would be cool to let me know

                      – formerlyknownas_463035818
                      Mar 28 at 10:03











                      -1
















                      int i = 0, j = n - 1;
                      for (i = 0; i < n/ 2; i++, j--)

                      int temp = array[i];
                      array[i] = array[j];
                      array[j] = temp;






                      share|improve this answer

























                      • Although this code might (or might not) solve the problem, a good questions should also explain why this code helps and how it solves the problem.

                        – BDL
                        Mar 28 at 10:28















                      -1
















                      int i = 0, j = n - 1;
                      for (i = 0; i < n/ 2; i++, j--)

                      int temp = array[i];
                      array[i] = array[j];
                      array[j] = temp;






                      share|improve this answer

























                      • Although this code might (or might not) solve the problem, a good questions should also explain why this code helps and how it solves the problem.

                        – BDL
                        Mar 28 at 10:28













                      -1














                      -1










                      -1









                      int i = 0, j = n - 1;
                      for (i = 0; i < n/ 2; i++, j--)

                      int temp = array[i];
                      array[i] = array[j];
                      array[j] = temp;






                      share|improve this answer













                      int i = 0, j = n - 1;
                      for (i = 0; i < n/ 2; i++, j--)

                      int temp = array[i];
                      array[i] = array[j];
                      array[j] = temp;







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 28 at 10:00









                      Nitesh AgarwalNitesh Agarwal

                      34 bronze badges




                      34 bronze badges















                      • Although this code might (or might not) solve the problem, a good questions should also explain why this code helps and how it solves the problem.

                        – BDL
                        Mar 28 at 10:28

















                      • Although this code might (or might not) solve the problem, a good questions should also explain why this code helps and how it solves the problem.

                        – BDL
                        Mar 28 at 10:28
















                      Although this code might (or might not) solve the problem, a good questions should also explain why this code helps and how it solves the problem.

                      – BDL
                      Mar 28 at 10:28





                      Although this code might (or might not) solve the problem, a good questions should also explain why this code helps and how it solves the problem.

                      – BDL
                      Mar 28 at 10:28



                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript