The following function is meant to return the maximum value

The following function is meant to return the maximum value in vec, which contains len entries. float max(float vec[], int len) {int i; float max; for (i = 0; i max) {max - vec [i];}} return max;} However it does not work as intended. Explain why, and show the change you would make to fix it. Modify the function from the previous question so that it returns the maximum absolute value in vec. Write a C function called diagonalStripes that takes as input two parameters: A: a two dimensional array with 7 columns. rows: the number of rows of A. The matrix is initialized so that the diagonal is set to 0, and the adjacent diagonal increase the previous value by one. So, for example, after executing the C code: int A[5][7]; diagonalStripes(A, 5); the contents of A will be: 0123456 1012345 2101234 3210123 4321012 Note that in C, array indexes start from zero.

Solution

a)

float max(float vec[],int len)

     {

       //Declare the required variables.

        int i;

        float max=0;

           for(i=0;i<len;i++)

          {

              if(vec[i]>max)

              {

                   max=vec[i];

              }

           }

          return max;

       }

In the above program the local variable max1 should initialize to zero, otherwise it takes some garbage values. In that case you may not get exact output.

b)

      The following function is find the absolute value for   

      the above function.

          float max(float vec[],int len)

          {

          int i;

            float max=-10,m=0;

            for(i=0;i<len;i++)

           {

              if(vec[i]>max)

              {

                 max=vec[i];

                 m=fabs(max);

              }

             }

          return m;

           }

    The fabs() function is available in math.h header file,

    So we include the header file in your program.

c)

int diagonalStripes(int A[],int len)

{

    int i,j;

     for(i=0;i<len;i++)

     {

      for(j=0;j<5;j++)

      {

       if(i==j)

        {

          A[i][j]=j;

        }

       }

     }

    for(i=0;i<len;i++)

      {

       for(j=0;j<5;j++)

       {

        printf(“%d”,A[i][j])

      }

     }

   }

 The following function is meant to return the maximum value in vec, which contains len entries. float max(float vec[], int len) {int i; float max; for (i = 0;
 The following function is meant to return the maximum value in vec, which contains len entries. float max(float vec[], int len) {int i; float max; for (i = 0;

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site