Consider the following C declarations int alen 6 int blen

Consider the following C declarations: int alen = 6; int blen = 3; int A [6] = {1, 7, 3, 5, 9, 2}; int B[3] = {11, 12, 10}; Write C code that concatenates the contents of A with the contents in reverse of B into another vector called R. After your code executes R will have the contents {1, 7, 3, 5, 9, 2, 10, 12, 11} your solution should use one or more for or while loops. Write a C function with signature void multiScalar(int A[6][6], int factor); that multiplies a 6 times 6 matrix A by a scalar factor. Use the function description above to explain what the terms \'\'passed by value\" and \'\'passed by reference\" mean in C programming. What is the value of temp after executing the code below? Explain your answer. int temp, values[6] = {3, 4, 5, 1, 2, 8}; temp = *(values + 2);

Solution

c)

In C, everything is passed by value in the technical sense. That is, whatever you give as an argument to a function, it will be copied into that function. For example, calling a function void foo(int) with foo(x) copies the value of x as the parameter of foo. This can be seen in a simple example:

The value of x is copied into foo and that copy is incremented. The x in main continues to have its original value.

As I\'m sure you\'re aware, objects can be of pointer type. For example, int* p defines p as a pointer to an int. It is important to note that the following code introduces two objects:

The first is of type int and has the value 5. The second is of type int* and its value is the address of the first object.

When passing a pointer to a function, you are still passing it by value. The address it contains is copied into the function. Modifying that pointer inside the function will not change the pointer outside the function - however, modifying the object it points to will change the object outside the function. But why?

As two pointers that have the same value always point at the same object (they contain the same address), the object that is being pointed to may be accessed and modified through both. This gives the semantics of having passed the pointed to object by reference, although no references ever actually existed - there simply are no references in C. Take a look at the changed example:

We can say when passing the int* into a function, that the int it points to was \"passed by reference\" but in truth the int was never actually passed anywhere at all - only the pointer was copied into the function. This gives us the colloquial1 meaning of \"pass by value\" and \"pass by reference\".

The usage of this terminology is backed up by terms within the standard. When you have a pointer type, the type that it is pointing to is known as its referenced type. That is, the referenced type of int* is int.

A pointer type may be derived from a function type, an object type, or an incomplete type, called the referenced type.

While the unary * operator (as in *p) is known as indirection in the standard, it is commonly also known as dereferencing a pointer. This further promotes the notion of \"passing by reference\" in C.

b)

While declaring the prototype and defining the display_matrix function you missed to type intbefore the matrix[num_rows][num_columns].

This is producing Error: too many arguments to the display_matrix.

Make a change to the display_matrix prototype.

Update the similar change to display_matrix definition.

 Consider the following C declarations: int alen = 6; int blen = 3; int A [6] = {1, 7, 3, 5, 9, 2}; int B[3] = {11, 12, 10}; Write C code that concatenates the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site