Difference between two

Mar 20, 2014 at 1:16am
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
Please use code tags to make your code more readable.
Mar 20, 2014 at 1:24am
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
Mar 20, 2014 at 4:33am
Thank you very much for the reply. This is the complete programme for a written exam. Difference between these two

#include <iostream>
void Changethecontent(int Arr[], int Count)
{
f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
or (int C=1;C<Count;C++)
Arr[C-1]+=Arr[C];
}
void main()
{
int A[]={3,4,5},B[]={10,20,30,40},C[]={900,1200};
Changethecontent(A,3);
Changethecontent(B,4);
Changethecontent(C,2);
for (int L=0;L<3;L++) cout<<A[L]<<’#’;
cout<<endl;
for (L=0;L<4;L++) cout<<B[L] <<’#’;
cout<<endl;
for (L=0;L<2;L++) cout<<C[L] <<’#’;
)


2nd programme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
void ChangetheContent(int Arr[], int Count)
{
for(int C=0; C<Count; C++)
Arr[C]= Arr[Count – C- 1];
}
void main()
{int A[]= {1, 2, 3}, B[] = {20, 30, 40, 50}, C[]= {100, 200};
ChangetheContent(A,3);
ChangetheContent(B,4);
ChangetheContent(C,2);
for(int L=0; L<3; L++) cout<<A[L]<<’#’;
cout<<endl;
for(int L=0; L<4; L++) cout<<B[L]<<’#’;
cout<<endl;
for(int L=0; L<2; L++) cout<<C[L]<<’#’;
cout<<endl;
}
Mar 20, 2014 at 6:34am
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
How the second programme will be calculaed? I means how the answer is getting? It is not clear
Mar 20, 2014 at 1:56pm
Can you please explain this to me
Mar 20, 2014 at 7:55pm
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 20, 2014 at 9:05pm
First function is actually adding the next array element to the current element until the loop ends leaving the last element unchanged.

And the second function is making the first element equal to the last one, second element equal to the second last one and so on.

-http://freakengineers.com
Mar 21, 2014 at 12:59am
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
Topic archived. No new replies allowed.