Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 2nd, 2008, 6:42 AM   #1
Magneto Delta
Newbie
 
Join Date: Mar 2008
Posts: 3
Rep Power: 0 Magneto Delta is on a distinguished road
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:


c Syntax (Toggle Plain Text)
  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.
Magneto Delta is offline   Reply With Quote
Old Mar 2nd, 2008, 8:25 AM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,035
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
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?
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.
Sane is offline   Reply With Quote
Old Mar 2nd, 2008, 5:04 PM   #3
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 228
Rep Power: 3 Seif is on a distinguished road
Re: I need help in writting pseudocode

Quote:
Originally Posted by Sane View Post
  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 .
Seif is offline   Reply With Quote
Old Mar 2nd, 2008, 5:10 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,035
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
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?
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.
Sane is offline   Reply With Quote
Old Mar 2nd, 2008, 5:31 PM   #5
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 646
Rep Power: 4 Jessehk is on a distinguished road
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
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Mar 3rd, 2008, 4:59 PM   #6
Magneto Delta
Newbie
 
Join Date: Mar 2008
Posts: 3
Rep Power: 0 Magneto Delta is on a distinguished road
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
Magneto Delta is offline   Reply With Quote
Old Mar 3rd, 2008, 5:27 PM   #7
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,667
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
Re: I need help in writting pseudocode

Moved
__________________
BIG K aka Kyle
Programming Forums
Kyle K Online

Please do not PM or email me programming questions. Post them in the forums instead.
big_k105 is offline   Reply With Quote
Old Mar 3rd, 2008, 5:47 PM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,035
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Re: I need help in writting pseudocode

Quote:
Originally Posted by Magneto Delta View Post
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 View Post
@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".
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.
Sane is offline   Reply With Quote
Old Mar 5th, 2008, 3:05 PM   #9
Magneto Delta
Newbie
 
Join Date: Mar 2008
Posts: 3
Rep Power: 0 Magneto Delta is on a distinguished road
Re: I need help in writting pseudocode

Quote:
Originally Posted by big_k105 View Post
Moved
Thanks

Quote:
Originally Posted by Sane View Post
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
Magneto Delta is offline   Reply With Quote
Old Apr 25th, 2008, 10:48 AM   #10
jamesdman
Newbie
 
Join Date: Apr 2005
Location: NC,USA
Posts: 17
Rep Power: 0 jamesdman is on a distinguished road
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.

Last edited by jamesdman; Apr 25th, 2008 at 11:00 AM.
jamesdman is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB 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
Flow chart and pseudocode assignment JimR Other Programming Languages 19 Nov 9th, 2008 9:26 PM
tips for writingh pseudocode redux Software Design and Algorithms 2 Sep 23rd, 2007 7:23 PM
Pseudocode Hass Java 6 Nov 22nd, 2005 2:18 PM
urgent pseudocode faceboy62 Visual Basic 10 Nov 15th, 2005 5:07 PM
Pseudocode Help Rob.K Other Programming Languages 2 Mar 15th, 2005 3:42 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:53 PM.

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