I am having a problem implementing the pivot_r portion of the program. I have all of the rest as we have been working on this program for a while but the darn pivot_r portion is killing me. Please any help would be GREAT!

/*-------------------------------------------------------------*/
/*  Program chapter5_8                                         */
/*                                                             */
/*  This program uses Gauss elimination to determine the       */
/*  mesh currents for a circuit.                               */
#include <stdio.h>
#include <math.h>
#define N 3  /*  number of unknown currents  */
int main(void)
{
   /*  Declare variables and function prototypes.  */
   int index;
   double r1, r2, r3, r4, r5, v1, v2, a[N][N+1], soln[N];
   void eliminate(double a[N][N+1], int n, int index);
   void back_substitute(double a[N][N+1], int n,
                        double soln[N]);                  
   void pivot_r(double a[N][N+1], int j);   /* MODIFICATION */
   
   /*  Get user input.  */
   printf("Enter resistor values in ohms: \n");
   printf("(R1, R2, R3, R4, R5) \n");
   scanf("%lf %lf %lf %lf %lf",&r1,&r2,&r3,&r4,&r5);
   printf("Enter voltage values in volts: \n");
   printf("(V1, V2) \n");
   scanf("%lf %lf",&v1,&v2);
   /*  Specify equation coefficients.  */
   a[0][0] = r1 + r2;
   a[0][1] = a[1][0] = -r2;
   a[0][2] = a[2][0] = a[1][3] = 0;
   a[1][1] = r2 + r3 + r4;
   a[1][2] = a[2][1] = -r4;
   a[2][2] = r4 + r5;
   a[0][3] = v1;
   a[2][3] = -v2;
   /*  Perform elimination step.  */
   for (index=0; index<=N-2; index++){
      pivot_r(a, index);          /* MODIFICATION */
      eliminate(a,N,index);
   }
   /*  Perform back substitution step.  */
   back_substitute(a,N,soln);
   /*  Print solution.  */
   printf("\n");
   printf("Solution: \n");
   for (index=0; index<=N-1; index++)
      printf("Mesh Current %d: %f \n",index+1,soln[index]);
   /*  Exit program.  */
   return 0;
}
/*------------------------------------------------------------*/
/*  This function performs the elimination step.              */
void eliminate(double a[N][N+1], int n, int index)
{
   /*  Declare variables.  */
   int row, col;
   double scale_factor;
   /*  Eliminate variable from equations.  */
   for (row=index+1; row<=n-1; row++)
   {
      scale_factor = -a[row][index]/a[index][index];
      a[row][index] = 0;
      for (col=index+1; col<=n; col++)
         a[row][col] += a[index][col]*scale_factor;
   }
   /*  Void return.  */
   return;
}
/*-------------------------------------------------------------*/
/*  This function performs the back substitution.              */
void back_substitute(double a[N][N+1], int n, 
                       double soln[N])
{
   /*  Declare variables. */
   int row, col;
   /*  Perform back substitution in each equation.  */
   soln[n-1] = a[n-1][n]/a[n-1][n-1];
   for (row=n-2; row>=0; row--)
   {
      for (col=n-1; col>=row+1; col--)
         a[row][n] -= soln[col]*a[row][col];
      soln[row] = a[row][n]/a[row][row];
   }
   /*  Void return.  */
   return;
}
/*-------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/*  THIS IS WHERE I NEED HELP                */
/*--------------------------------------------------------------------*/
void pivot_r(double a[N][N+1], int j)
{
   /*  Declare variables.  */
   
   /* .... */
   /*  Void return.  */
   return;
}
/*--------------------------------------------------------------------*/

THANKS!!!!

I am having a problem implementing the pivot_r portion of the program. I have all of the rest as we have been working on this program for a while but the darn pivot_r portion is killing me. Please any help would be GREAT!

/*-------------------------------------------------------------*/
/* Program chapter5_8 */
/* */
/* This program uses Gauss elimination to determine the */
/* mesh currents for a circuit. */
#include <stdio.h>
#include <math.h>
#define N 3 /* number of unknown currents */
int main(void)
{
/* Declare variables and function prototypes. */
int index;
double r1, r2, r3, r4, r5, v1, v2, a[N][N+1], soln[N];
void eliminate(double a[N][N+1], int n, int index);
void back_substitute(double a[N][N+1], int n,
double soln[N]);
void pivot_r(double a[N][N+1], int j); /* MODIFICATION */

/* Get user input. */
printf("Enter resistor values in ohms: \n");
printf("(R1, R2, R3, R4, R5) \n");
scanf("%lf %lf %lf %lf %lf",&r1,&r2,&r3,&r4,&r5);
printf("Enter voltage values in volts: \n");
printf("(V1, V2) \n");
scanf("%lf %lf",&v1,&v2);
/* Specify equation coefficients. */
a[0][0] = r1 + r2;
a[0][1] = a[1][0] = -r2;
a[0][2] = a[2][0] = a[1][3] = 0;
a[1][1] = r2 + r3 + r4;
a[1][2] = a[2][1] = -r4;
a[2][2] = r4 + r5;
a[0][3] = v1;
a[2][3] = -v2;
/* Perform elimination step. */
for (index=0; index<=N-2; index++){
pivot_r(a, index); /* MODIFICATION */
eliminate(a,N,index);
}
/* Perform back substitution step. */
back_substitute(a,N,soln);
/* Print solution. */
printf("\n");
printf("Solution: \n");
for (index=0; index<=N-1; index++)
printf("Mesh Current %d: %f \n",index+1,soln[index]);
/* Exit program. */
return 0;
}
/*------------------------------------------------------------*/
/* This function performs the elimination step. */
void eliminate(double a[N][N+1], int n, int index)
{
/* Declare variables. */
int row, col;
double scale_factor;
/* Eliminate variable from equations. */
for (row=index+1; row<=n-1; row++)
{
scale_factor = -a[row][index]/a[index][index];
a[row][index] = 0;
for (col=index+1; col<=n; col++)
a[row][col] += a[index][col]*scale_factor;
}
/* Void return. */
return;
}
/*-------------------------------------------------------------*/
/* This function performs the back substitution. */
void back_substitute(double a[N][N+1], int n,
double soln[N])
{
/* Declare variables. */
int row, col;
/* Perform back substitution in each equation. */
soln[n-1] = a[n-1][n]/a[n-1][n-1];
for (row=n-2; row>=0; row--)
{
for (col=n-1; col>=row+1; col--)
a[row][n] -= soln[col]*a[row][col];
soln[row] = a[row][n]/a[row][row];
}
/* Void return. */
return;
}
/*-------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* THIS IS WHERE I NEED HELP
the accuracy of the gauss elimination technique can be improved using a process called pivoting. to perform row pivoting, we first reorder the equations so that the equation with the largest absolute value for the first coefficient is the first equation. we than eliminate the first variable from the equations that follow the first equation. then, starting with the second equation, we reorder the equations such that the second equation has the largest coefficient(in absolute value) for the second variable. we then eliminate the second variable from all equations after the second equation. the process continues similarly for the rest of the variables. assume that a symbolic constant N contains the number of equations.

-write a function that receives a two-dimensional array and a pivot value that specifies the coeffecient of interest, j. the function should the reorder all equations starting with the jth equation such that the jth equation will have the largest coefficient(in absolute value) in the jth position. assume that the function can reference the size of the array as N by N+1 and that the corresponding function prototype is:

void pivot_r(double a[N][N+1], int j); */
/*--------------------------------------------------------------------*/
void pivot_r(double a[N][N+1], int j)
{
/* Declare variables. */

/* .... */
/* Void return. */
return;
}
/*--------------------------------------------------------------------*/

THANKS!!!!

this is all the info I have currently

Member Avatar for iamthwee

Have a look at this, it should be easy to port to C.

#include <iostream>
#include <cmath>
using namespace std;

void gauss(int N, // number of unknowns
           float A [20] [21], // coefficients and constants
           float result[20],
           bool& err)
// Solve system of N linear equations with N unknowns
// using Gaussian elimination with scaled partial pivoting
// First N rows and N+1 columns of A contain the system
// with right-hand sides of equations in column N+1
// err returns true if process fails; false if it is successful
// original contents of A are destroyed
// solution appears in column N
{
    int indx[20];
    float scale[20];
    float maxRatio;
    int maxIndx;
    int tmpIndx;
    float ratio;
    float sum;

    for (int i = 0; i < N; i++) indx[i] = i; // index array initialization

    // determine scale factors

    for (int row = 0; row < N; row++)
    {
        scale[row] = abs(A[row][0]);
        for (int col = 1; col < N; col++)
        {
            if (abs(A[row][col]) > scale[row]) scale[row] = abs(A[row][col]);
        }
    }

// forward elimination

    for (int k = 0; k < N; k++)
    {
        // determine index of pivot row
        maxRatio = abs(A[indx[k]] [k])/scale[indx[k]];
        maxIndx = k;
        for (int i = k+1; i < N; i++)
        {
            if (abs(A[indx[i]] [k])/scale[indx[i]] > maxRatio)
            {
                maxRatio = abs(A[indx[i]] [k])/scale[indx[i]];
                maxIndx = i;
            }
        }
        if (maxRatio == 0) // no pivot available
        {
            err = true;
            return;
        }
        tmpIndx =indx[k]; indx[k]=indx[maxIndx]; indx[maxIndx] = tmpIndx;

        // use pivot row to eliminate kth variable in "lower" rows
        for (int i = k+1; i < N; i++)
        {
            ratio = -A[indx[i]] [k]/A[indx[k]] [k];
            for (int col = k; col <= N; col++)
            {
                A[indx[i]] [col] += ratio*A[indx[k]] [col];
            }
        }
    }

    // back substitution

    for (int k = N-1; k >= 0; k--)
    {
        sum = 0;
        for (int col = k+1; col < N; col++)
        {
            sum += A[indx[k]] [col] * A[indx[col]] [N];
        }
        A[indx[k]] [N] = (A[indx[k]] [N] - sum)/A[indx[k]] [k];
    }

/*
    cout << endl;
    for (int r=0; r<N; r++)
    {
        cout << indx[r];
        for (int c=0; c<=N; c++) cout<<"  " << A[r][c];
        cout << endl;
    }
*/
    for (int k = 0; k < N; k++) result[k] = A[indx[k]] [N];
}



int main()
{
    float A[20][21];
    float X[20];
    bool err;
    A[0][0] = 5; 
    A[0][1] = 2; 
    A[0][2] = 1; 
    A[0][3] = 17;

    A[1][0] = 1; 
    A[1][1] = 4; 
    A[1][2] = 2; 
    A[1][3] = 16; 

    A[2][0] = 2; 
    A[2][1] = 1; 
    A[2][2] = 4; 
    A[2][3] = 11; 

    gauss(3, A, X, err);
    for (int i=0; i<3; i++) cout << X[i] << "  ";
    std::cin.get();
    return 0;
}

Thanks for the help, is there anyone that can please put it in the same format as I have. I am finishing my first semester of programming and have never seen that other type.

Thanks

Member Avatar for iamthwee

I am finishing my first semester of programming and have never seen that other type

You mean port it to C? Come on kiddo, how hard can that be. However, if you beg I'll do it for you... a ha ha.

:lol:

One other thing wat the hell is this?

a[0][0] = r1 + r2;
a[0][1] = a[1][0] = -r2;
a[0][2] = a[2][0] = a[1][3] = 0;
a[1][1] = r2 + r3 + r4;
a[1][2] = a[2][1] = -r4;
a[2][2] = r4 + r5;
a[[COLOR="Red"]0[/COLOR]][3] = v1;
a[2][3] = -v2;

Is the indexing for your 2d array correct? :eek: Well maybe, but you should test this using plain ol' simultaneous equations before you try it will your circuit values.

this is what the program looks like right now:

/*-------------------------------------------------------------*/
/*  Program chapter5_8                                         */
/*                                                             */
/*  This program uses Gauss elimination to determine the       */
/*  mesh currents for a circuit.                               */
#include <stdio.h>
#include <math.h>
#define N 3  /*  number of unknown currents  */
int main(void)
{
/*  Declare variables and function prototypes.  */
int index;
double r1, r2, r3, r4, r5, v1, v2, a[N][N+1], soln[N];
void eliminate(double a[N][N+1], int n, int index);
void back_substitute(double a[N][N+1], int n,
double soln[N]);
void pivot_r(double a[N][N+1], int j);   /* MODIFICATION */


/*  Get user input.  */
printf("Enter resistor values in ohms: \n");
printf("(R1, R2, R3, R4, R5) \n");
scanf("%lf %lf %lf %lf %lf",&r1,&r2,&r3,&r4,&r5);
printf("Enter voltage values in volts: \n");
printf("(V1, V2) \n");
scanf("%lf %lf",&v1,&v2);
/*  Specify equation coefficients.  */
a[0][0] = r1 + r2;
a[0][1] = a[1][0] = -r2;
a[0][2] = a[2][0] = a[1][3] = 0;
a[1][1] = r2 + r3 + r4;
a[1][2] = a[2][1] = -r4;
a[2][2] = r4 + r5;
a[0][3] = v1;
a[2][3] = -v2;
/*  Perform elimination step.  */
for (index=0; index<=N-2; index++){
pivot_r(a, index);          /* MODIFICATION */
eliminate(a,N,index);
}
/*  Perform back substitution step.  */
back_substitute(a,N,soln);
/*  Print solution.  */
printf("\n");
printf("Solution: \n");
for (index=0; index<=N-1; index++)
printf("Mesh Current %d: %f \n",index+1,soln[index]);
/*  Exit program.  */
return 0;
}
/*------------------------------------------------------------*/
/*  This function performs the elimination step.              */
void eliminate(double a[N][N+1], int n, int index)
{
/*  Declare variables.  */
int row, col;
double scale_factor;
/*  Eliminate variable from equations.  */
for (row=index+1; row<=n-1; row++)
{
scale_factor = -a[row][index]/a[index][index];
a[row][index] = 0;
for (col=index+1; col<=n; col++)
a[row][col] += a[index][col]*scale_factor;
}
/*  Void return.  */
return;
}
/*-------------------------------------------------------------*/
/*  This function performs the back substitution.              */
void back_substitute(double a[N][N+1], int n,
double soln[N])
{
/*  Declare variables. */
int row, col;
/*  Perform back substitution in each equation.  */
soln[n-1] = a[n-1][n]/a[n-1][n-1];
for (row=n-2; row>=0; row--)
{
for (col=n-1; col>=row+1; col--)
a[row][n] -= soln[col]*a[row][col];
soln[row] = a[row][n]/a[row][row];
}
/*  Void return.  */
return;
}
/*-------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/*  YOU ARE ASKED TO IMPLEMENT THE FOLLOWING FUNCTION                 */
/*--------------------------------------------------------------------*/
void pivot_r(double a[N][N+1], int j)
{
/*  Declare variables.  */


/* ..missing info here.. */
/*  Void return.  */
return;
}
/*--------------------------------------------------------------------*/
Member Avatar for iamthwee

No offence, but you haven't really shown any effort of at least trying to do that yourself.

In fact, it appears that your teacher has written most of that program for you and has left the pivoting as an exercise for you to do. All you are doing is begging me to do that part for you. NO!

Have a look at the pivoting strategy I'm using here:

// forward elimination
for (int k = 0; k < N; k++)
{
// determine index of pivot row
maxRatio = abs(A[indx[k]] [k])/scale[indx[k]];
maxIndx = k;
for (int i = k+1; i < N; i++)
{
if (abs(A[indx

] [k])/scale[indx] > maxRatio)
{
maxRatio = abs(A[indx] [k])/scale[indx];
maxIndx = i;
}
}
if (maxRatio == 0) // no pivot available
{
err = true;
return;
}
tmpIndx =indx[k]; indx[k]=indx[maxIndx]; indx[maxIndx] = tmpIndx;



}

Break it down, and try to understand what's going on. Then change it to meet the requirements of your assignment. Sorry but that's all I'm willing to hand out. So no, I tell a lie, if you beg me I won't do it for ya.

:lol:

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.