10 Replies - 936 Views - Last Post: 26 May 2012 - 04:40 AM Rate Topic: -----

#1 dunsta  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10469
  • View blog
  • Posts: 38,809
  • Joined: 27-December 08

Re: C++ Exam Question

Posted 10 May 2012 - 05:54 PM

That is indeed correct! :)
Was This Post Helpful? 1
  • +
  • -

#3 dunsta  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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 ?
Was This Post Helpful? 0
  • +
  • -

#4 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 615
  • 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.
Was This Post Helpful? 1
  • +
  • -

#5 dunsta  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 61
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 dunsta  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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)
Was This Post Helpful? 0
  • +
  • -

#8 dunsta  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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++;
	}
}

Was This Post Helpful? 0
  • +
  • -

#9 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: C++ Exam Question

Posted 21 May 2012 - 07:45 PM

View Postdunsta, 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

Was This Post Helpful? 1
  • +
  • -

#10 dunsta  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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)/>;

Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 4019
  • View blog
  • Posts: 12,408
  • Joined: 25-December 09

Re: C++ Exam Question

Posted 26 May 2012 - 04:40 AM

Yes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1