GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Jun-2007, 18:04
joesmithf1 joesmithf1 is offline
New Member
 
Join Date: Jun 2007
Posts: 2
joesmithf1 is on a distinguished road

Stuck on Codes


Hi

I have been pulling my hair out trying to figure this out. Please help!!!

Here is my project description:

By using a pointer to pointers **A and **B and the function calloc()
allocate the memory for the 4x4 matrices A[][] and B[][].
By using the pointers *a and *b and the function malloc()
allocate the memory for the 4-dimensional vectors a[] and b[].
Read the components of a and A from the given input file matrix.dat
(which is in the public/final directory; you need to copy it into your directory).
The first line of the input file contains the components of a. The rows of
matrix A are the last four lines of the input file. Print the components
of a[] and A[][]. By calling the function pmatrix(A,B,a,b,n), n=4,
which you will construct, determine the components of the matrix B=1.5*A*A, and the
vector b=(A*a)+0.5*a. Print the components of the matrix B and the vector b in the main.
Free dinamically allocated memory.
.................................................. ..............................
Output should look like:


Vector a is:
a[] = -1.200 1.200 -2.000 -0.500
Matrix A is:
2.700 1.500 3.200 2.400
-3.200 0.700 -2.600 4.300
1.400 0.600 2.100 -1.800
1.500 1.700 -2.600 -0.700
Matrix B is:
..... ...... ...... ......
..... ...... ...... ......
..... ...... ...... ......
..... ...... ...... ......
Vector b is:
b[] = ..... ..... ..... .....


My codes and I am stuck.

CPP / C++ / C Code:
//void prob3(double ***A, double ***B, double **a, double **b)
void prob3(void)
{
  FILE *matrix;
  int k;
  double ***A, ***B, **a, **b;
  matrix=fopen("matrix.dat", "r");

  *A= (double**)malloc(sizeof(double*)*4);
  *B= (double**)malloc(sizeof(double*)*4);
  *a= (double*)malloc(sizeof(double)*4);
  *b= (double*)malloc(sizeof(double)*4);

  for(k=0; k<4; k++)
    {
     fscanf(matrix, "%lf %lf %lf %lf", &(*A)[k][0], &(*A)[k][1], &(*A)[k][2], &(*A)[k][3]);
    }
     for(k=0; k<4; k++)
       {
        fscanf(matrix, "%lf", &(*a)[k]);
       }
  matrix1(*A, *B, *a, *b, 4);


}

void problem3()
{
  double **A, **B, *a, *b;
  int row, col;

  printf("\nMatrix B is:\n\n");
  for(row=0; row<4; row++)
     {
      for(col=0; col<4; col++)
        {
         printf("%.3f  ", B[row][col]);
        }
     }
  printf("\nVector b is: \n\n");
  printf("a[] = ");
  for(col=0; col<4; col++)
    {
     printf("%.3f  ", b[col]);
    }
  for(row=0; row<4; row++)
    {
     free(A[row]);
     free(B[row]);
    }

  free(A);
  free(B);
  free(a);
  free(b);
}
Last edited by LuciWiz : 08-Jun-2007 at 07:03. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 08-Jun-2007, 08:43
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,821
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Stuck on Codes


Something like the following:

CPP / C++ / C Code:
    /* suppose we want an array of int: a[5][3] */
    int **a;
    num_rows = 5;
    num_cols = 3;

    /*  allocate storage for an array of pointers */
    a = malloc(num_rows * sizeof(int *));
    if (a == NULL) {
        printf("Can't allocate %d bytes for a\n", num_rows * sizeof(int *));
        return 1;
    }

    /* for each pointer, allocate storage for an array of ints */
    for (i = 0; i < num_rows; i++) {
        a[i] = malloc(num_cols * sizeof(int));
        if (a[i] == NULL) {
            printf("Can't allocate %d bytes for a[%d]\n", i,
                   num_cols * sizeof(int));
            for (j = 0; j < i; j++) {
                free(a[j]);
            }
            free(a);
            return 1;
        }
    }


Regards,

Dave
  #3  
Old 10-Jun-2007, 22:11
joesmithf1 joesmithf1 is offline
New Member
 
Join Date: Jun 2007
Posts: 2
joesmithf1 is on a distinguished road

Re: Stuck on Codes


thank you very much!
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking source codes of image, audio and video files onauc C Programming Language 5 26-Feb-2005 22:47
Browser's Language Interpreters onauc Open Discussion Forum 0 18-Nov-2004 19:53
embedded C++ codes mak90thug C++ Forum 0 25-Jan-2004 22:50
New bbcode - [PHP]codes[/PHP] admin GIDForums™ 0 25-Aug-2002 10:27

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:35.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.