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

Question 6 (a) The C functiorn int strlen (char string[]); returns the length of

ID: 3910179 • Letter: Q

Question

Question 6 (a) The C functiorn int strlen (char string[]); returns the length of a null-terminated string. i. What is a null-terminated string? [2 marks] ii. Write an implementation of the strlen function [5 marks] (b) Write a C function with signature void multiScalar(int AC 1[5], int rows, int factor); that multiplies a rows x 5 matrix A by a scalar factor. (c) Explain what void means in the C function signature. (d) main is making 3 calls to the function multiScalar as shown below [6 marks] [2 marks] int main () 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15); int factor [3] 05] int new [4] [5] int x2; multiScalar(factor, 3, x); multiScalar(new, 4, 5); multiScalar(factor, 4, 4); For each call, will that line compile or generate an error? If it compiles, briefly explain the outcome of that call. [9 marks]

Explanation / Answer

Hello there, I am providing answer of each problem given by you,

a]

i) NULL terminated string:

    A null-terminated string is a character string stored as an array containing the characters and terminated with a null character ('', called NUL in ASCII).

ii) Implementation of strlen function:

    int my_strlen(char string[])
    {

int i;
        for(i=0;string[i]!='';i++);
        return i;
     }

b]

Implementation of multiScalar function:

void multiScalar(int A[][5],int rows,int factor)
{
   for(int i = 0; i<rows;i++)
   {
       for(int j=0;j<5;j++)
       {
           A[i][j]*=factor;
       }
   }
       // Displaying modified values of matrix A
   for(int i = 0; i<rows;i++)
   {
       for(int j = 0;j<5;j++)
       {
           printf("%d ",A[i][j]);
       }
       printf(" ");
        }
}

c]

void keyword in C:

    In C programming,
    1) When void is used as a function return type, it indicates that the function does not return a    

        value.It can print values or it can change values of variables but it doesn't return any value.

    2) When void appears in a pointer declaration, it specifies that the pointer is universal.

    3) When void used in a function's parameter list, void indicates that the function does not require

        any argument or it takes no parameters.

d]

For each call ,that lines will compile and compiler will not return any error.

For second call of multiScalar when we pass the matrix "new" to the function multiScalar, the matrix "new" will get initialised by garbage values in C.It happens because we only declared matrix "new" and didn't initialised it.

In third call of multiscalar,Since matrix "factor" contains only 3 rows and we passed rows value 4, so the 4th row of matrix "factor" will also be the garbage values multiplied by scalar factor 4.

So the output for that second call of multiscalar will be matrix with garbage values.

2 4      6       8      10
12       14    16     18     20
22       24    26     28     30

18             0              -2047521600 65078    1329347600
65534           31550462 0                2      0
-1083739430 44038      -2047405248    65078   0

8        16       24      32              40
48      56       64      72      80
88      96       104    112            120
0        72       0       -139152640 260952

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Feel free to ask qeries regarding above solution, I am always here to help you!!!

Thank you!!!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote