User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 425,822 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,983 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 358 | Replies: 5 | Solved
Reply
Join Date: May 2008
Posts: 5
Reputation: alban08 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
alban08 alban08 is offline Offline
Newbie Poster

need help with a program

  #1  
May 29th, 2008
i`m trying to make a program to calculate the product of this serie:
1/2 *1/3 *5/2 *5/6 *8/10 *16/13 *23/24.....

the following program gets compiled without errors but when i execute it pops up a window: prog.exe has encountered a problem..etc...
can anyone help me with this problem
  1. #include<stdio.h>
  2. main()
  3. {
  4. int j,n,*p,*q,num[2][n];
  5. float p1=1,p2=1,rezultati;
  6. printf("sa kufiza");
  7. scanf("%d",&n);
  8. q=&num[0][0];/*assings the adress of element[0][0] to q*/
  9. p=&num[1][0];/*assings the adress of element[1][0] to p*/
  10. *q=1;/*assings 1 to element[0][0]*/
  11. *p=2;/*assings 2 to element[1][0]*/
  12. *(q+1)=1;
  13. *(p+1)=3;
  14. for(j=2;j<n;j++)
  15. {*(q+j)=(*(p+j-2))+(*(p+j-1));/*assigns values to array elements*/
  16. *(p+j)=(*(q+j-2))+(*(q+j-1));}/*assigns values to array elements*/
  17. for(j=0;j<n;j++)
  18. {p1=p1*(*(q+j));/*calculates p1*/
  19. p2=p2*(*(p+j));}/*calculates p2*/
  20. rezultati=p1/p2;
  21. printf("%f",rezultati);/*prints the result*/
  22. system("pause");
  23. }
Last edited by Narue : May 30th, 2008 at 11:07 am. Reason: Added code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2008
Posts: 1,767
Reputation: VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice 
Rep Power: 8
Solved Threads: 218
VernonDozier VernonDozier is offline Offline
Posting Virtuoso

Re: need help with a program

  #2  
May 29th, 2008
Originally Posted by alban08 View Post
i`m trying to make a program to calculate the product of this serie:
1/2 *1/3 *5/2 *5/6 *8/10 *16/13 *23/24.....

the following program gets compiled without errors but when i execute it pops up a window: prog.exe has encountered a problem..etc...
can anyone help me with this problem
  1. #include<stdio.h>
  2. main()
  3. {
  4. int j,n,*p,*q,num[2][n];
  5. float p1=1,p2=1,rezultati;
  6. printf("sa kufiza");
  7. scanf("%d",&n);
  8. q=&num[0][0];/*assings the adress of element[0][0] to q*/
  9. p=&num[1][0];/*assings the adress of element[1][0] to p*/
  10. *q=1;/*assings 1 to element[0][0]*/
  11. *p=2;/*assings 2 to element[1][0]*/
  12. *(q+1)=1;
  13. *(p+1)=3;
  14. for(j=2;j<n;j++)
  15. {*(q+j)=(*(p+j-2))+(*(p+j-1));/*assigns values to array elements*/
  16. *(p+j)=(*(q+j-2))+(*(q+j-1));}/*assigns values to array elements*/
  17. for(j=0;j<n;j++)
  18. {p1=p1*(*(q+j));/*calculates p1*/
  19. p2=p2*(*(p+j));}/*calculates p2*/
  20. rezultati=p1/p2;
  21. printf("%f",rezultati);/*prints the result*/
  22. system("pause");
  23. }
I'm guessing you have a problem in line 4 when you declare your 2-dimensional array:
num[2][n];
At this point n is uninitialized. You don't get a value for n till line 7 so it's impossible to guess what n contains at line 4. I'd say you need to ask the user to enter a value for n before you declare your array, then declare your array using malloc.
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 713
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 46
jephthah's Avatar
jephthah jephthah is offline Offline
Master Poster

Re: need help with a program

  #3  
May 29th, 2008
VERNON: I think that giving beginners malloc is like giving a child a gun and saying "dont shoot anyone, mmkay?" ... ive become quite weary of fixing buggy programs written by someone who thinks they know how to use malloc() when the really don't.



ALBAN: what Vernon is getting at, is that you CAN NOT simply declare an array with a variably number of elements. the array size must be declared with a constant, hard-coded value.

whats the maximum that "sa kufiza" could possibly be? make your array at least that size, then. youre not going to run out of RAM.

once you get "sa kufiza", then only use that many elements of the array.


.
Last edited by jephthah : May 29th, 2008 at 7:14 pm.
Why so serious?
Reply With Quote  
Join Date: Feb 2008
Posts: 9
Reputation: dexblack is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
dexblack dexblack is offline Offline
Newbie Poster

Re: need help with a program

  #4  
May 29th, 2008
adding to JEPHTHAH's post.
#define N 100
int num[2][N]
Also refuse to use more than the maximum declared size and tell the user why e.g.
printf("Maximum allowed is %d\n", N);

Note: When doing floating point calculations you should always try to keep the values as small as possible, so rather than calculate rezultati after the loop, multiply it out inside the loop e.g.
{ rezultati *= (*(q+j))/(*(p+j)); }
saving yourself two stack variables and ending up with a more accurate result.
Reply With Quote  
Join Date: Feb 2008
Posts: 9
Reputation: dexblack is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
dexblack dexblack is offline Offline
Newbie Poster

Re: need help with a program

  #5  
May 29th, 2008
and initialise rezultati to 1.0 (of course)
Reply With Quote  
Join Date: May 2008
Posts: 5
Reputation: alban08 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
alban08 alban08 is offline Offline
Newbie Poster

Re: need help with a program

  #6  
May 30th, 2008
thank you all for your answers. they were all helpfull. the program worked
Last edited by alban08 : May 30th, 2008 at 5:51 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 4:21 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC