I NEED HELP WITH THIS PROBLEM!!!! ANYONE!!!!

/*3. Write the function: double max_norm(double *v, int n) to compute
the maximum norm of vector v[] with n elements. The max norm of a vector is
computed by taking the maximum absolute value of its elements.
In prob3() compute x1[]=a[][]*x0[], call max_norm() to compute the max_norm
x1[]. In order to normalize (or convert it to a unit vector), divide each
element of x1[] by the norm. Print the normalized x1[] using %12.4e. Then
copy x1[] as x0[] (to perform updating) and repeat the process nine more times
. */
double a[5][5]={ 1.0, 0.2, 0.6, 0.3,-0.2,
0.2, 3.0,-0.1, 0.2, 0.7,
0.6,-0.1, 2.0, 0.7, 0.1,
0.3, 0.2, 0.7, 5.5, 0.2,
-0.2, 0.7, 0.1, 0.2, 4.5 };
double x1[5], x0[5]={0.1, -0.5, 0.1, 1.0, 0.9};

Dani AI

Generated

This is the classic power-method loop: compute x1 = A * x0, find the maximum absolute entry (the max norm), divide x1 by that norm, copy x1 back into x0, and repeat. A clear bug in ’s snippet is assigning x1[i] inside the inner product loop — that stores a running sum instead of the finished dot product. Move the x1[i] = dot; assignment after the inner loop so each x1[i] receives the complete sum.

Below is a simple, safe implementation of the required max_norm function. Include <math.h> and link the math library if your toolchain requires it.

#include <math.h>

double max_norm(const double *v, int n)
{
    if (n <= 0) return 0.0;
    double m = fabs(v[0]);
    int i;
    for (i = 1; i < n; ++i) {
        double fv = fabs(v[i]);
        if (fv > m) m = fv;
    }
    return m;
}

After computing x1, call double norm = max_norm(x1, 5);. If norm == 0.0 skip the division to avoid divide-by-zero. Then divide each x1[j] by norm, copy x1 into x0, and repeat the cycle ten times. This iteration will converge to the dominant eigenvector (power iteration). For background see the power-iteration overview (Power iteration) and the C fabs reference (fabs — cppreference). , the above should show where to call the function; , the joke aside, the fix is short and concrete.

Recommended Answers

All 4 Replies

Exactly, where are you having problems with?
Be more specific and post relevant portion of code you have tried already and it is not working.

I'm completely lost....I don't understand how to write the first function then call on it???

...then ask your teacher to divide your homework grade by 2 and send the second half to the author of the best answer...

/*3. Write the function: double max_norm(double *v, int n) to compute
the maximum norm of vector v[] with n elements. The max norm of a vector is
computed by taking the maximum absolute value of its elements.
In prob3() compute x1[]=a[][]*x0[], call max_norm() to compute the max_norm
x1[]. In order to normalize (or convert it to a unit vector), divide each
element of x1[] by the norm. Print the normalized x1[] using %12.4e. Then
copy x1[] as x0[] (to perform updating) and repeat the process nine more times. */

double a[5][5]={ 1.0, 0.2, 0.6, 0.3,-0.2,
                         0.2, 3.0,-0.1, 0.2, 0.7,
                         0.6,-0.1, 2.0, 0.7, 0.1,
                         0.3, 0.2, 0.7, 5.5, 0.2,
                        -0.2, 0.7, 0.1, 0.2, 4.5 };
        double x1[5], x0[5]={0.1, -0.5, 0.1, 1.0, 0.9};
// ***Enter your statements
        int i, k;
        double dot;
        for(i=0; i<5; i++){
                for(k=0, dot=0.0; k<5; k++){
                        dot +=a[i][k] * x0[k];
                        x1[i]=dot;
                }
        }

        return;
}
double max_norm(double *v, int n)
{
// ***Enter your statements


return;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.