Thread: Ouput Error
View Single Post
  #1 (permalink)  
Old June 7th, 2007, 05:53 PM
phungleon phungleon is offline
Friend of Wrox
 
Join Date: Jul 2003
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default Ouput Error

Hi All,
I have been pulling my hair out trying to figur out this problem. Please help!!! I just started C++

I need my output to look like this:
The original vector v[] is:
v[] = { 1.05 -2.55 1.75 1.10 -0.75 2.43 -3.55 4.19 1.45 -0.05 } //The issue when I run the code is the first number 0.05 rather than 1.05
The rearranged vector v[] is:
v[] = { -3.55 -2.55 -0.75 -0.05 1.05 1.10 1.45 1.75 2.43 4.19 } //The issue when I run is, it is not sorting in this order
The length of the vector v[] is 7.11397 //Not sure what this means or how to do this


void prob2(void)

{

 FILE *infile, *outfile;

 int rows, val, rval, j, k, n;

 infile = fopen("data.dat", "r");

 outfile = fopen("output2.dat", "w");



 printf("\nThe original vector v[] is:\n");

 fprintf(outfile,"\nThe original vector v[] is:\n");

 printf("v[] = {");

 fprintf(outfile,"v[] = {");



 fscanf(infile, "%d", &rows);

 for(k=0; k<10; k++)

   {

    fscanf(infile, "%lf", &val);

    printf(" %.2f", val);

    fprintf(outfile," %.2f", val);

   }

 printf(" }\n");

 fprintf(outfile," }\n");



 printf("\nThe rearranged vector v[] is:\n");

 printf("v[] = {");



 double v[]={ 1.05, -2.55, 1.75, 1.10, -0.75, 2.43, -3.55, 4.19, 1.45, -0.05 };

 rearr(v,n);

 int x;

 for(x=0; x<10; x++)

    printf(" %.2f", v[x]);

    printf(" }");

    printf("\n");

}



double rearr(double *v, int n)

{

  int i, j, flag=1;

  int temp;

  for(i=1; i<=10 &&flag; i++)

    {

     flag = 0;

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

       {

        if(v[j+1]<v[j])

         {

          int temp=v[j];

          v[j]=v[j+1];

          v[j+1]=temp;

          flag = 1;

         }

       }

    }

return;

}







void matrix1(double **A, double **B, double *a, double *b, int size)

{

  int row, col;

  printf("\nMatrix A is:\n\n");



  for(row=0; row<size; row++)

    {

     for(col=0; col<size; col++)

       {

        printf("%.3f ", A[row][col]);

       }

    }



  for(row=0; row<size; row++)

    {

     for(col=0; col<size; col++)

       {

        B[row][col]=1.5*A[row][col]*A[row][col];

       }

    }



  printf("\nVector a is:\n\n");

  printf("a[] = ");



  for(col=0; col<4; col++)

    {

     printf("%.3f ", a[col]);

    }



  for(row=0; row<4; row++);

    {

     b[row]=((A[row][0]+A[row][1]+A[row][2]+A[row][3])*a[row])+.5*a[row];

    }

}






Reply With Quote