Mar 20, 2014 at 1:16am UTC
Please explain difference between these .
void Changethecontent(int Arr[], int Count)
{
for (int C=1;C<Count;C++)
Arr[C-1]+=Arr[C];
}
void ChangetheContent(int Arr[], int Count)
{
for(int C=0; C<Count; C++)
Arr[C]= Arr[Count – C- 1];
Mar 20, 2014 at 1:23am UTC
Please use code tags to make your code more readable.
Mar 20, 2014 at 1:24am UTC
Please use code tags my good sir
http://www.cplusplus.com/articles/jEywvCM9/
Now, they will do the same thing, however there's a slight difference, as far iterating is concerned.
1.)
1 2 3 4 5
void Changethecontent(int Arr[], int Count)
{
for (int C=1;C<Count;C++) // Starts at 1, stops when it reaches count
Arr[C-1]+=Arr[C]; // C - 1, otherwise you'd go out of bounds
}
2.)
1 2 3 4 5
void ChangetheContent(int Arr[], int Count)
{
for (int C=0; C<Count; C++) // Starts at 0, but has same condition
Arr[C]= Arr[Count – C- 1]; // No need for -1, will stay in bounds
}
Number 2 is the more practical way of iterating.
Do you still need more help?
Last edited on Mar 20, 2014 at 1:28am UTC
Mar 20, 2014 at 6:34am UTC
Note, I had a brain fart there. I didn't mean to say out of bounds, I meant skipping the first element.
Mar 20, 2014 at 12:14pm UTC
How the second programme will be calculaed? I means how the answer is getting? It is not clear
Mar 20, 2014 at 1:56pm UTC
Can you please explain this to me
Mar 20, 2014 at 7:55pm UTC
Sure thing.
The first one on its first run is taking element 0 and increasing it by the value of element 1, then element 1 + element 2, etc. until the loop ends.
The second one on its fist run is taking element 0 and sets it equal to the last element.
One the second run it takes the 2nd element and sets it equal to the 2nd to last element, etc.
Mar 21, 2014 at 12:59am UTC
Thank you @Austin and @freakengineers
tht means in the second programme answer will be
{1, 2, 3} -3#2#3# first element changes to last element and second element to the same and the last element remain nchanged
{20,30,40,50} - 50#40#40#50# firstelement changes to last element and second elementto the second last , third and fourth element remain unchanged
{100, 200}-200#200# first element changes to last element and the last element remain unchanged