C++ exercise

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
Hi All,

Trying to do this little C++ exercise attached.

For question:

a) my answer is p = {2,0,1}. Is it correct? I'm currently revising it. Let me know if you find otherwise.

As for the remaining questions, will try to post my answers as soon as I can but if anyone wants to post fine!:)
 

Attachments

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
Looks like C to me...
:) Me too. Have never really found the *big* difference between the two actually.

So am I correct?

initially p [] = {0,1,2} with the first 'for' used to initialize it.
line6: i = 1,2

when i = 1:
line8: k=1, j=1 then while in line 9 is false! so it jumps to line14 and p[1] = 1.

then jumping back to line6 for the 2nd iteration of the for loop:
k=2, j=2.

line9 while ((2>0) && (2>-4)) is true
p[2] = p[1] = 1 and j=1.

back to line 9 while ((1>0) && (-1>-4)) is true
p[1] = p[0] = 0 and j = 0.

back to line 9 ((0>0) && ( a[p[-1]]>-4)) is false as p[-1] does not exist so we jump to line14 and p[0] = 2

=> p = {2, 0, 1}. That's how I came up with this answer.

what do you think!?

Edit: and what can you say about the other part of the question a " In general, what does the function Do it do." ?
 
Last edited:

joeyd999

Joined Jun 6, 2011
5,283
:) Me too. Have never really found the *big* difference between the two actually.
The difference is pretty much the Object Oriented extensions, none of which are shown in your example.

It seems like you are taking C++ without the need of a C prerequisite? That seems odd to me.
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
...
It seems like you are taking C++ without the need of a C prerequisite? That seems odd to me.
I did 'VB.6' first then 'C' then 'C++' at school. I can write code using these languages BUT I am not an expert or very competent with these languages. And I can master these languages if I want to and will later on BUT I am too busy with embedded programing for now!:)

Oh and I know a little of VB.Net

This exercise is from Software engineering course!
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
Here's my answer for question b attached!
The numbers are from the lines of codes as they are.

Any comment!?
 

Attachments

WBahn

Joined Mar 31, 2012
30,052
:) Me too. Have never really found the *big* difference between the two actually.

So am I correct?

initially p [] = {0,1,2} with the first 'for' used to initialize it.
line6: i = 1,2

when i = 1:
line8: k=1, j=1 then while in line 9 is false! so it jumps to line14 and p[1] = 1.

then jumping back to line6 for the 2nd iteration of the for loop:
k=2, j=2.

line9 while ((2>0) && (2>-4)) is true
p[2] = p[1] = 1 and j=1.

back to line 9 while ((1>0) && (-1>-4)) is true
p[1] = p[0] = 0 and j = 0.

back to line 9 ((0>0) && ( a[p[-1]]>-4)) is false as p[-1] does not exist so we jump to line14 and p[0] = 2

=> p = {2, 0, 1}. That's how I came up with this answer.

what do you think!?

Edit: and what can you say about the other part of the question a " In general, what does the function Do it do." ?
There are some telltales that give a hint as to what it is doing, but do what it says and draw a flow chart for the function. That will make the telltales stand out a bit and let you view the function from a slightly higher, more abstract viewpoint which generally lets you see the higher, more abstract functionality a bit more easily.
 

WBahn

Joined Mar 31, 2012
30,052
Here's my answer for question b attached!
The numbers are from the lines of codes as they are.

Any comment!?
This is not a flowchart. In particular, you have one state with two possible paths leaving it and no indication of when you take each path.

A chart with just a bunch of line numbers that you have to go back and reference specific lines to see what is going on forces you to work at too low a level of detail. Draw your flowchart so that it is stand alone so that you can view the entire algorithm at once.
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
Confirmed with gcc -- not valid syntax. Some class!

Eric, rather than asking us if your answer is right or wrong, why not compile it and check for yourself?
That's correct! Yes I can compile it with Visual Studio...and can fix the syntax errors if any but not sure how to see the content of p though!

Will give it a shot...trying to answer the other theoretical questions, if I can. If I cannot I drop/leave it...

Thanks!
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
This is not a flowchart. In particular, you have one state with two possible paths leaving it and no indication of when you take each path.

A chart with just a bunch of line numbers that you have to go back and reference specific lines to see what is going on forces you to work at too low a level of detail. Draw your flowchart so that it is stand alone so that you can view the entire algorithm at once.
What I drew is the 'control flow graph' actually as required.
Well...:rolleyes:
 

WBahn

Joined Mar 31, 2012
30,052
What I drew is the 'control flow graph' actually as required.
Well...:rolleyes:
So a "control flow graph" doesn't have to give any indication of what determines which of two branches you take when you leave a node?

And a "control flow graph" doesn't need to every terminate, despite the fact that the code it is for clearly does?
 

WBahn

Joined Mar 31, 2012
30,052
Since part of the problem is figuring out what the code does, it is not unreasonable for it to be uncommented. Having typos is less forgivable, but may well have a reasonable explanation if we knew the circumstances.
 

WBahn

Joined Mar 31, 2012
30,052
The first step is to rewrite the code so that it is reasonably formatted

Rich (BB code):
void Do_it(int *a, int *p, int N)
{
   int i, j, k;

   for (i = 0; i < N; i++)
      p = i;

   for (i = 1; i < N; i++)
   {
      k = p;
      j = i;
      while ( (j>0) && (a[p[j-1]] > a[k]) )
      {
         p[j] = p[j-1];
         j--;
      }
      p[j] = k;
   }
}


Now, I haven't tried to compile or run this; as a result I might miss something in what I do next.

Looking at the code the second loop can be simplified quite a bit.

First, notice that within the for() loop, the value of i is never changed and the value of k is never changed after it is initialized.

Furthermore, the value of k is set equal to p at the start of the loop and that after that point the only elements of the p[] array that get changed are at p[j]. Then note that j starts out at i and only goes down from there. Thus, no value in the p[] array at an index greater than i is changed during any pass of the loop. As a consequence, in each pass of the loop when k is set equal to p, the value in p is the value it had before the for() loop started, which the prior for() loop set equal to i. Since k is set equal to i and then never changed, and since i never changes, we can simply use i and get rid of k completely. As a result, We can thus simplify the code as follows:

Rich (BB code):
void Do_it(int *a, int *p, int N)
{
   int i, j, k;

   for (i = 0; i < N; i++)
      p = i;

   for (i = 1; i < N; i++)
   {
      j = i;
      while ( (j>0) && (a[p[j-1]] > a) )
      {
         p[j] = p[j-1];
         j--;
      }
      p[j] = i;
   }
}


The next thing to note is that the inner while loop is nothing but a classic for() loop. So we can write the code as:

Rich (BB code):
void Do_it(int *a, int *p, int N)
{
   int i, j, k;

   for (i = 0; i < N; i++)
      p = i;

   for (i = 1; i < N; i++)
   {
      while (j = i; (j>0) && (a[p[j-1]] > a); j-- )
         p[j] = p[j-1];
      p[j] = i;
   }
}


The next thing to note is that the contents of the a[] array are never changed.

What role does the p[] array play?

The keys to this are that it is initialized to values from 0 through N-1 and that it is used in the expression a[p[j-1]]. This means that the values stored in the p[] array are being used as indices into the a[] array. This is consistent with the values that the p[] array is initialized to, namely the index values for an N-element array.

Is that enough to let you see what is happening and what the code is doing?
 

WBahn

Joined Mar 31, 2012
30,052
Looks like I was right...

Compiler found same answer p = {2, 0, 1}
I missed this post, so sorry for the late reply.

Did you figure out what the algorithm is doing, in general?

For instance, if you start with N=9 and the a1[] array being {-16, 7 , 3, 5, -2, 19, 13, 27, 1}, can you write down, by inspection, what the p[] array will contain?
 
Top