# C++ Exam Question

Page 1 of 1

## 10 Replies - 1120 Views - Last Post: 26 May 2012 - 04:40 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=278852&amp;s=3d6cdd44860f94aa82d97653e0c6f5f7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 dunsta

Reputation: 1
• Posts: 79
• Joined: 08-April 10

# C++ Exam Question

Posted 10 May 2012 - 05:51 PM

Hi,
I am currently going through old C++ exams and sometimes I need assurance that I have interpretted the question properly or that I have found the correct answer. I hope that if I go through some question and my answers( one at a time, but all in this post)I can get some feedback.

Firstly
Q) Post decrement the variable x by 1, then subtract it from the variable total and assign the result back into total
A)
```x--;
total-=x;
```

Is This A Good Question/Topic? 0

## Replies To: C++ Exam Question

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11449
• Posts: 43,165
• Joined: 27-December 08

## Re: C++ Exam Question

Posted 10 May 2012 - 05:54 PM

That is indeed correct!

### #3 dunsta

Reputation: 1
• Posts: 79
• Joined: 08-April 10

## Re: C++ Exam Question

Posted 10 May 2012 - 07:42 PM

I will keep replying here (and hopefully getting feedback) on further questions, unless instructed otherwise by the powers that be.

Q) print 100.7365 with a precision of 3.
Now I assume a precision of 3, means 3 decimal places.
but if I use
```	double y = 100.7365;
cout<<setprecision(3)<<y<<endl;
```

I get 101.

but if I use
```	double y = 100.7365;
cout<<fixed<<setprecision(3)<<y<<endl;

```

I get 100.737

Which do you think would be correct ?

### #4 turboscrew

Reputation: 113
• Posts: 723
• Joined: 03-April 12

## Re: C++ Exam Question

Posted 10 May 2012 - 10:35 PM

The first is correct.

101 is 3-digit precision
100.737 is 3 decimal precision.

### #5 dunsta

Reputation: 1
• Posts: 79
• Joined: 08-April 10

## Re: C++ Exam Question

Posted 21 May 2012 - 03:58 PM

I need to create a function using this definition
```void reverseArray(int * a, const int size)
```

to reverse the array using pointer notation and pointer arithmetic to access the members of the array.
I came up with this
```void reverseArray(int *a, const int size){
cout<<"You found the func()"<<endl;
int b[size];

for(int i=0;i<size;i++){
//cout<<*(a+(size-(i+1)))<<endl;
int temp = *(a+(size-(i+1)));
b[i]=temp;
}

for(int i=0;i<size;i++){
*a++ = b[i];
}
}
```

the function does the job but I would like to know
* 1 Have I fullfilled the requirements re/ pointer arithmetic?
* 2 Is there a more concise way to reverse the array?

Thanks.

This post has been edited by dunsta: 21 May 2012 - 03:59 PM

### #6 raspinudo

Reputation: 61
• Posts: 232
• Joined: 19-September 11

## Re: C++ Exam Question

Posted 21 May 2012 - 06:11 PM

I whipped up another solution for ya. This way uses no extra space, whether it be an extra array or a temp variable. It uses the concept of bitwise xor'ing which you can read up further upon here:wiki

The basic of it, is that xoring two numbers together 3 times will swap them:
```a = 5 // which is 0000 0101 in binary
b = 3 // which is 0000 0011 in binary

a ^= b; //states a = a XOR b => 0000 0110
b ^= a; //states b = b XOR a => 0000 0101 which we recall is 5!
a ^= b // states a = a XOR b => 0000 0011 which we recall is 3!

```

here is the code
```#include <iostream>
using namespace std;

int main(){
int arr[5] = {1,2,3,4,5};
int size = sizeof(arr)/sizeof(arr[0]);

//print array
for(int i = 0; i < size; i++){
cout << "[" << arr[i] << "]";
}
cout << endl;

//reverse array
for(int i = 0; i < size/2; i++){
*(arr+i) ^= *(arr+(size-(i+1)));
*(arr+(size-(i+1))) ^= *(arr+i);
*(arr+i) ^= *(arr+(size-(i+1)));
}

//print array
for(int i = 0; i < size; i++){
cout << "[" << arr[i] << "]";
}
cout << endl;

}
~

```

If the bitwise magic is a bit confusing, just give it a bit of time to sink it. It can be very useful to you in programming.

### #7 dunsta

Reputation: 1
• Posts: 79
• Joined: 08-April 10

## Re: C++ Exam Question

Posted 21 May 2012 - 07:30 PM

Thanks Raspinudo, that is a great response.
However, with my exam just around the corner I am not going to look into this concept just yet.
I was more looking for some feedback on my solution and maybe a more "common" approach to solving the problem (from the view of a "beginner" C++ student)

### #8 dunsta

Reputation: 1
• Posts: 79
• Joined: 08-April 10

## Re: C++ Exam Question

Posted 21 May 2012 - 07:41 PM

ok I came up with a better function.
But is this considered 'pointer arithmetic' ?

```void reverseArray(int *a, const int size){
int temp;
int i=0;
int si = size;
while(i<si){
temp = a[i];
a[i] = a[si-1];
a[si-1] = temp;

si--;
i++;
}
}
```

### #9 raspinudo

Reputation: 61
• Posts: 232
• Joined: 19-September 11

## Re: C++ Exam Question

Posted 21 May 2012 - 07:45 PM

dunsta, on 21 May 2012 - 07:41 PM, said:

ok I came up with a better function.
But is this considered 'pointer arithmetic' ?

```void reverseArray(int *a, const int size){
int temp;
int i=0;
int si = size;
while(i<si){
temp = a[i];
a[i] = a[si-1];
a[si-1] = temp;

si--;
i++;
}
}
```

I wouldn't consider this pointer arithmetic per say. Your first solution was, because it was directly manipulating the pointer a. i.e. *(a+1) //direct manipulation.

edit:
with that said, the bracket notation is also dealing with pointers, it is just syntactical sugar to make things easier on programmers.

This post has been edited by raspinudo: 21 May 2012 - 07:47 PM

### #10 dunsta

Reputation: 1
• Posts: 79
• Joined: 08-April 10

## Re: C++ Exam Question

Posted 25 May 2012 - 04:43 PM

does this look right?

i. Write a function prototype foo that takes a reference to an object of the class BigObject and returns void.
```void foo(BigObject &B)/>;
```

### #11 jimblumberg

Reputation: 4736
• Posts: 14,768
• Joined: 25-December 09

## Re: C++ Exam Question

Posted 26 May 2012 - 04:40 AM

Yes.