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

(1.)Write a function called monus that takes two integer arguments a and b and r

ID: 3775148 • Letter: #

Question

(1.)Write a function called monus that takes two integer arguments a and b and returns their difference if a is larger and returns 0 otherwise. So monus(7,3) returns 4, and monus (3,7)

returns 0, for example.

int monus(int a, int b) {

                                                                                                                                               

(2.)Suppose I wanted a version of monus that didn’t return a value, but instead modified a third parameter. I try to use your monus function like this:

void mymonus(int a, int b, int c) {

            c = monus(a,b);

}

But, I find that it doesn’t work. When I call my function like this:

int x = -3;

mymonus(5,4,x);

x is still -3. Explain why and show how to correct my error.

                                                                                                                                               

(3.)Assume that I wanted to ensure that an array could not be modified in a function I am writing. How would I declare my array parameter?

a. int const a[ ]

b. const int a[ ]

c. int a[const]

d. int a[ ]

                                                                                                                                               

(4.)If you try to use an index outside of the range of an array ( like a[20] ) when a has only 10 elements, the C compiler will generate an error.

a. True                             b.   False

                                                                                                                                               

(5.)If you wanted to print the data at location 3 of the integer array a, which of the following would work? Circle all that are correct.

a. printf (“%d”, *a+3);

b. printf (“%d”, a+3);

c. printf (“%d”, *(a+3));

d. printf (“%d”, a[3]);

e. printf (“%d”, &a[3]);

                                                                                                                                               

(6.)Write a function that takes an integer array and its size, and prints the array in reverse order.

void revprintf ( int a[ ], int asize ) {

                                                                                               

(7.)Write a function that takes an array and its size as parameters and returns 1, if it contains neither a 2 not a 3, and returns 0 otherwise.

int no23( int a[ ], int asize) {

                                                                                                                                               

(8.)Show the content of array a after the following code is executed:

int a[ ] = {2,4,6,8};

int i;

for ( i=1; i<4; i++ )

            a[i] = a[i] + a[i-1] ;

                                                                                                                                               

(9.)Show the content of array a after the following code is executed:

int a[ ] = {2,4,6,8};

int i;

for ( i=3; i>0; i-- )

            a[i] = a[i] + a[i-1] ;

                                                                                                                                               

(10.)Write a function that copies array a, with size sizea, int array b, with size sizeb. If sizea is bigger than sizeb, copy only sizeb number of elements so that you don’t overrun array b. If sizea is smaller than sizeb, then copy all elements from array a and fill the rest of array b with 0.

void     acopy ( int a[ ], int sizea, int b[ ], int sizeb )

           

                                                                                                                                               

Explanation / Answer

1.)Write a function called monus that takes two integer arguments a and b and returns their difference if a is larger and returns 0 otherwise. So monus(7,3) returns 4, and monus (3,7)
returns 0, for example.

int monus(int a, int b) {
if(a>b)
return a-b;
else
return 0;
}


Ans 2) you are getting error as you are calling mymonus(5,4,x); outside of the mymonus function hence the value of x will remain the same outside of mymonus function as c=monus(a,b); has the scope only in mymonus(int a, int b, int c).


To correct this error :
Use call by reference i.e passed the address of a variable:
corrected code:
void mymonus(int a, int b, int* c) {
*c = monus(a,b);
}


int x = -3;
mymonus(5,4,&x);

After doing these changes you will get the correct output of x.


3)Assume that I wanted to ensure that an array could not be modified in a function I am writing. How would I declare my array parameter?

const int a[ ]

4.)If you try to use an index outside of the range of an array ( like a[20] ) when a has only 10 elements, the C compiler will generate an error.

a. True

(5.)If you wanted to print the data at location 3 of the integer array a, which of the following would work? Circle all that are correct.
Correct options:
a. printf (“%d”, *a+3);
c. printf (“%d”, *(a+3));
d. printf (“%d”, a[3]);


6.)Write a function that takes an integer array and its size, and prints the array in reverse order.
void revprintf ( int a[ ], int asize ) {
int i;
for(i=asize-1;i>=0;i--)
printf ("%d",a[i]);
}


(7.)Write a function that takes an array and its size as parameters and returns 1, if it contains neither a 2 not a 3, and returns 0 otherwise.
int no23( int a[ ], int asize) {
int flag=1;
int i;
for(i=0;i<asize;i++){
if(a[i]==2 || a[i]== 3)
flag=0;
}
return flag;
}
  
(8.)Show the content of array a after the following code is executed:
int a[ ] = {2,4,6,8};
int i;
for ( i=1; i<4; i++ )
a[i] = a[i] + a[i-1] ;

Content of array:2 6 12 20

(9.)Show the content of array a after the following code is executed:
int a[ ] = {2,4,6,8};
int i;
for ( i=3; i>0; i-- )
a[i] = a[i] + a[i-1] ;          
          
Content of array:2 6 10 14

(10.)Write a function that copies array a, with size sizea, int array b, with size sizeb. If sizea is bigger than sizeb, copy only sizeb number of elements so that you don’t overrun array b. If sizea is smaller than sizeb, then copy all elements from array a and fill the rest of array b with 0.
void acopy ( int a[ ], int sizea, int b[ ], int sizeb ){
if(sizea >= sizeb){
int i;
for(i=0;i<sizeb;i++)
a[i]=b[i];
}
else{
int i;
for(i=0;i<sizea;i++)
b[i]=a[i];
int j;
for(j=sizea;j<sizeb;j++)
b[j]=0;
}
  
}          


Note:Please do ask in case of any doubt,thanks.