Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Is this the correct statement ? short sh; short *ptr = &sh ; 2. int num [ 100

ID: 3567333 • Letter: 1

Question

1.   Is this the correct statement ?

short sh;

short *ptr = &sh ;

2. int num [ 100 ] = { 19, 12, 23, 43, 87, 34 } ;

num++;

Is the above statement correct? Explain why or why not ?

3. Is

int *ptr = num ;

correct ? Consider num is an array declared above

. Explain why or why not ?

4. ptr = ptr + 5 ;

What is the value *ptr now ? Write a program to verify.

5. ptr--;

what is the value of *ptr ?

6. What is the difference between char * and int * ?

  

7. Show me an example to swap two variable passing by value and passing by reference ? Show me how the values are getting affected ?

8. Can you imagine passing an array by values if the array contains 10MB ?    Because copying the value into the function actual parameters is a time consuming process, arrays are passed by reference.

can you declare an array in the main function

int swap_array[2] = { 50, 60 } ;

Declare a swap function that takes this array like - int swap ( int num [ ] )

Call this function in your main function and verify if arrays are passed by reference or by value ?

     

Explanation / Answer

1. Yes this is the correct statement

2. No it will give an error because you require an lvalue for an incremental operation

3. Yes ptr will point to the 1st element of the array
because the name of the array indicates the address of the 0th element of the array

4.The value of ptr will be 34
assuming num as the above array
#include<stdio.h>

int main(){
int num[100] = {19,12,23,43,87,34};
   int *ptr = num;
   printf("%d",*ptr+5);
}

5.Value of ptr-- will be 19

6. char* is a pointer to a character variable and int* is a pointer to an integer
variable i.e on performing the incremental operation char * will be incremented by 1 space while
int* will be incremented by the size of an int whatever it may be depending on the
architectuure of the system

7.#include <iostream>
using namespace std;

// function declaration
void swapByReference(int &x, int &y)
{
int temp;
temp = x; /* save the value at address x */
x = y; /* put y into x */
y = temp; /* put x into y */
}

void swapByValue(int x, int y)
{
int temp;
temp =x;
x = y;
y = temp;
}

int main (){
// local variable declaration:
int a = 100;
int b = 200;

cout << "Swapping by call by reference : ";
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;

/* calling a function to swap the values using variable reference.*/
swapByReference(a, b);
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;

cout << " Swapping by call by Value : ";
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;

/* calling a function to swap the values using variable value.*/
swapByValue(a, b);
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;

return 0;
}


8. #include<iostream>

using namespace std;

int swap(int num[]){
int temp = num[0];
   num[0] = num[1];
   num[1] = temp;
}

int main(){
int swap_array[2] = {50 , 60};
   cout << "Before swap, value of swap_array[0] :" << swap_array[0] << endl;
cout << "Before swap, value of swap_array[1] :" << swap_array[1] << endl;
   swap(swap_array);
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;

}