Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   I need help in writting pseudocode (http://www.programmingforums.org/showthread.php?t=15300)

Magneto Delta Mar 2nd, 2008 6:42 AM

I need help in writting pseudocode
 
Am really embarrasingly new to C/C++, a good thing you cant see me otherwise I wouldnt post here. Okay the thing is I got this book called C language by o relly, I didnt find it simple to understand. Our lecturer later gave us an assignment to code in C or C++ a program that multiplies two matrices m by n (no specific matrix size) and gives the output so I came up with this:


:

  1. // program to multiply 2 matrices ( A and B or M by N of rows and columns defined by the user)
  2. # include <stdio.h>
  3. int main ()
  4. { int d,i,j,k,l,m,n,p,q,t,u,x ;
  5.  
  6.   int A[m] [n];
  7. int B[k] [l];
  8. int C[t] [u];
  9.  
  10. i=0; d=0; j=0; j=0; k=0; l=0; m=0; n=0; n=0; p=0; q=0; t=0; u=0; x=0;
  11.  
  12. /* Entre the size of the matrices A and B */
  13.  
  14. printf (" Entre no. of rows for 1st matrix : \n");
  15. scanf (" %d", &m );
  16. printf (" Entre no. of rows for 1st matrix : \n");
  17. scanf (" %d", &m );
  18. printf (" Entre no. of rows for 2nd matrix : \n");
  19. scanf (" %d", &p );
  20. printf (" Entre no. of columns for 2nd matrix : \n");
  21. scanf (" %d", &q );
  22.  
  23. /* Entre the values of the matrices */
  24.  
  25. printf (" Entre values for 1st matrix from 1st row,1st col integer to the right end then 2nd row, 1st col to the right end.... : \n");
  26.  
  27.   while ( i<m)
  28.             { while ( j<n)
  29.                           { printf (" A[%d] [%d] = ? :" , i,j );
  30.                             scanf ("%d", &A[i] [j] ); ++j; } j=0 ; ++i}
  31.  
  32. printf (" Entre values for 2nd matrix from 1st row,1st col integer to the right end then 2nd row, 1st col to the right end.... : \n");
  33.  
  34. i=0;j=0
  35.  
  36.   while (i<p)
  37.             { while (j<q)
  38.                           { printf (" B[%d] [%d] = ? :" , i,j );
  39.                             scanf ("%d", &B[i] [j] ); ++j; } j=0 ; ++i}
  40.  
  41.           i=0; j=0;
  42.  
  43. /* Calculate the product of matrix A and matrix B */
  44.  
  45.   while (k<p)
  46.             { while ( i<m)
  47.                         { while (j<n)
  48.                                       { C[t] [u]+=A[i] [j]*B[k] [l] ;
  49.                                         ++j ; ++k ; }
  50.                                         ++i ; ++t ; k=0 ; j=0 ; }
  51.                                         t=0 ; ++u ; i=0 ; j=0 ; ++l ; k=0 ; }
  52.  
  53.  
  54.           t=0; u=0;
  55.  
  56. /* Read out the values of the obtained matrics C */
  57.  
  58.   while (t<x)
  59.             { while (u<d)
  60.                           { printf (" C[%d] [%d] = %d\t\t",t,u, C[t] [u]) ; }
  61.                             printf ("\n") ;
  62.                             ++u ; }
  63.                             u=0 ; ++t ; }


Like l said am new to this, so bear with me here. I wanted to know how to write its pseudocode, I developed a habbit of writting the code before the pseudocode. Now I need help in writting the pseudocode before the code. Am asking this because I failed in writting one. Can anyone help? If you dont mind I would appreciate I anyone can help me make it work.Its giving me an undefined A[m][n] error.

Sane Mar 2nd, 2008 8:25 AM

Re: I need help in writting pseudocode
 
The biggest problem is this part here:

:

int A[m] [n];
int B[k] [l];
int C[t] [u];


There are two problems with this:
  1. The value of m, n, k, l, t, and u, are all undefined at this point in the program. Remember that programs execute in steps, and variables are in a way "replaced" with their value each time they are referenced. Thus, m, n, k, l, and t, all "replace" the array sizes with their values at that time. Since none of those variables have even been given a value yet, random junk is substituted instead.
  2. When you are declaring an array, if you need to make the size dynamic (dynamic size is when the program does not yet know the size until after it has been compiled), then you need to use some other techniques such as "malloc" or "new".

I'd recommend putting an upper-boundary on how large the matrix can be. Then simply make the array that large.

:

#define MAX 100

int main() {
    int A[MAX] [MAX];
    int B[MAX] [MAX];
    int C[MAX] [MAX];


Look up "for loops" in your book, or online. All of your while loops should be replaced by for loops. It would make this much easier for you.

Psuedocode won't help much in this instance, since you seem to have the general idea down. You just don't yet know how to code, right?

Seif Mar 2nd, 2008 5:04 PM

Re: I need help in writting pseudocode
 
Quote:

Originally Posted by Sane (Post 141885)
  1. When you are declaring an array, if you need to make the size dynamic (dynamic size is when the program does not yet know the size until after it has been compiled), then you need to use some other techniques such as "malloc" or "new".

Don't mean to be pedantic, but thats not 100% true. The C99 standard allows variable length arrays. That is, an array of automatic storage duration whose length is determined at run time.

For example the following code is valid:

:


int foo(int len)
{
    int vla[len];
    for (int i = 0; i < vla[len]; i++)
        vla[i] = i;
    for (int i = 0; i < vla[len]; i++)
        printf("%i\n", vla[i] = i);
}


Of course VLA operands will cause problems when compiled with C++. As you mentioned you could use C or C++, you may want to consider moving to a compiler that will support C99.

of course you must also make sure that the expression you are using to specify the size of the array is defined first ;).

Sane Mar 2nd, 2008 5:10 PM

Re: I need help in writting pseudocode
 
Yes, but until C99 is standard (not to mention completely compliant *points at GCC*) across all compilers, let's try not to confuse the original poster, or rely on specific features when more reliable alternatives exist?

Jessehk Mar 2nd, 2008 5:31 PM

Re: I need help in writting pseudocode
 
I agree with Sane here, but C99 is pretty neat. How long has it been? 9 years? GCC should really get on adding missing support (especially the complex numbers and tgmath.h).

EDIT: A lot is actually missing. http://gcc.gnu.org/gcc-4.3/c99status.html

Magneto Delta Mar 3rd, 2008 4:59 PM

Re: I need help in writting pseudocode
 
@Sane "I'd recommend putting an upper-boundary on how large the matrix can be"

int A[m] [n];
int B[k] [l];
int C[t] [u];

Giving an upper boundary on how large would fail the question because the question asked of m by n size (no boundaries) actually these are meant to be arrays defined during the running of the program not before the program begins. Am sure am missing some kind of library reference or some kind of inclusion so that it may actually be arrays whose size is determined by the user

** Guys I made a mistake I should have posted this in C language not C++. Can this be transfered to the C section? **

Am using visual c++ 6.0 to code in c language

big_k105 Mar 3rd, 2008 5:27 PM

Re: I need help in writting pseudocode
 
Moved :)

Sane Mar 3rd, 2008 5:47 PM

Re: I need help in writting pseudocode
 
Quote:

Originally Posted by Magneto Delta (Post 141956)
Giving an upper boundary on how large would fail the question because the question asked of m by n size (no boundaries)

Then you fail to see the point of setting an upper boundary. You set a boundary to the maximum amount you anticipate the user will enter. The remainder of what you don't need (the portion of the matrix outside of the inputted size) is simply not used. If the user tries to enter more than the set boundary, you ask again. In my example, we anticipate that the user will never want to enter a matrix greater than or equal to 100 x 100. We still get the size inputs from the user. Then, we simply don't use the portion of the matrix that we don't need.

Think of it this way: your computer does not have enough memory to handle a 1,000,000 x 1,000,000 matrix. Thus, the user can not enter 1,000,000. Therefore, there is an obvious restriction on the size. This restriction is your upper-boundary, and in the event that the user enters a very large number, it will have to be enforced one way or another in order to prevent the program from crashing. My suggestion of creating an upper-boundary, always making the array that large, and then only using the values within the inputted sizes-- is the easiest way to handle everything in one fell swoop.

Quote:

Originally Posted by Magneto Delta (Post 141956)
@actually these are meant to be arrays defined during the running of the program not before the program begins. Am sure am missing some kind of library reference or some kind of inclusion so that it may actually be arrays whose size is determined by the user

I already told you this too. Please re-read my post. Particularly, the part about "malloc". But again, setting a constant boundary is much easier for someone new to C, and is by no means a "failure for the question".

Magneto Delta Mar 5th, 2008 3:05 PM

Re: I need help in writting pseudocode
 
Quote:

Originally Posted by big_k105 (Post 141957)
Moved :)

Thanks

Quote:

Originally Posted by Sane (Post 141960)
Then you fail to see the point of setting an upper boundary. You set a boundary to the maximum amount you anticipate the user will enter. The remainder of what you don't need (the portion of the matrix outside of the inputted size) is simply not used. If the user tries to enter more than the set boundary, you ask again. In my example, we anticipate that the user will never want to enter a matrix greater than or equal to 100 x 100. We still get the size inputs from the user. Then, we simply don't use the portion of the matrix that we don't need.

Think of it this way: your computer does not have enough memory to handle a 1,000,000 x 1,000,000 matrix. Thus, the user can not enter 1,000,000. Therefore, there is an obvious restriction on the size. This restriction is your upper-boundary, and in the event that the user enters a very large number, it will have to be enforced one way or another in order to prevent the program from crashing. My suggestion of creating an upper-boundary, always making the array that large, and then only using the values within the inputted sizes-- is the easiest way to handle everything in one fell swoop.



I already told you this too. Please re-read my post. Particularly, the part about "malloc". But again, setting a constant boundary is much easier for someone new to C, and is by no means a "failure for the question".

Alright I will look into it and see what I can come up with :)

jamesdman Apr 25th, 2008 10:48 AM

Re: I need help in writting pseudocode
 
Writing pseudocode is worth a couple chapters of its own... I have a programming logic book I'll sell you cheap. Simple Program Design : A step-by-Step Approach Fifth Edition.
Its usualy a prerequisite to any actual programming class.


All times are GMT -5. The time now is 4:18 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC