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

Homework 3 Some pointer arithmetic is allowed. Which of the following arithmetic

ID: 3686297 • Letter: H

Question

Homework 3

Some pointer arithmetic is allowed. Which of the following arithmetic operators are allowed?

a)pointer + integer

b)pointer - pointer

c)pointer - integer

d)integer + pointer

e)integer * pointer

f)Given the definitions,

int *p1, *p2;

p1 = new int;

p2 = new int;

What is the difference between the following two assignments.

a) p1 = p2;

b) *p1 = *p2

3. Given the declarations below, write a code fragment that allocates a nameless variable for pointer p1 to point to.

int *p1, *p2;

4. Give three uses for the * operator. Name and describe each use.

5. Your program creates a dynamically allocated array as follows:

int *entry;

entry = new int[10];

so that the pointer variable entry is pointing to the dynamically allocated array. Write code to fill this array with 10 numbers typed in at the keyboard.

6. Write the function with the following prototype to check if an integer is a prime number.

isPrime(int *);

7. What does the following function implement? How to call the gcd function in main function? If 8 and 12 are passed to a and b respectively, what is the content of the returned pointer?

int *gcd(int a, int b)

{

            int *p;

            int c;

            p = &b;

            while (a != 0) {

                        c = a;

                        a = b%a;

                        b = c;

            }

            return p;

           

}

8. Discuss the advantages of using multiple files in a C++ program.

9. How many main steps are there in C++ compilation? Describe their main tasks respectively.

10. What the three basic preprocessor directives in header files?

Explanation / Answer

10)#include,#define,#ifndef

9)it goes through peprocess, compiler , assembler and linker

7)in order to call gcd in main function keep some variable=gcd=(x,y)

4)* operator stores the address of the variable

* operator allocates dynamic memory

* operators becomes easy during the swaping and interchanging the numbers

2a)it assigns p2 to p1

2b)it assigns address of p2 to the address of p1

1a) a is allowed because we write *(p+i)