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;
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;
c++
marked as duplicate by StoryTeller, YSC
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.
|
show 2 more comments
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;
c++
marked as duplicate by StoryTeller, YSC
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
|
show 2 more comments
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;
c++
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++
c++
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
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
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
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
|
show 2 more comments
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
|
show 2 more comments
4 Answers
4
active
oldest
votes
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++)
add a comment
|
You can reverse like this
for (int i = 0; i < n--; i++)
tmp = a[i];
a[i] = a[n];
a[n] = tmp;
add a comment
|
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.
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
add a comment
|
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;
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
add a comment
|
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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++)
add a comment
|
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++)
add a comment
|
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++)
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++)
answered Mar 28 at 9:55
Suven PandeySuven Pandey
6883 silver badges16 bronze badges
6883 silver badges16 bronze badges
add a comment
|
add a comment
|
You can reverse like this
for (int i = 0; i < n--; i++)
tmp = a[i];
a[i] = a[n];
a[n] = tmp;
add a comment
|
You can reverse like this
for (int i = 0; i < n--; i++)
tmp = a[i];
a[i] = a[n];
a[n] = tmp;
add a comment
|
You can reverse like this
for (int i = 0; i < n--; i++)
tmp = a[i];
a[i] = a[n];
a[n] = tmp;
You can reverse like this
for (int i = 0; i < n--; i++)
tmp = a[i];
a[i] = a[n];
a[n] = tmp;
answered Mar 28 at 10:04
Deb SDeb S
4154 silver badges13 bronze badges
4154 silver badges13 bronze badges
add a comment
|
add a comment
|
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.
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
add a comment
|
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.
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
add a comment
|
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.
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.
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
add a comment
|
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
add a comment
|
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;
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
add a comment
|
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;
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
add a comment
|
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;
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;
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
add a comment
|
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
add a comment
|
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