•
•
•
•
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
![]() |
•
•
Join Date: May 2008
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
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/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
cplusplus Syntax (Toggle Plain Text)
#include<stdio.h> main() { int j,n,*p,*q,num[2][n]; float p1=1,p2=1,rezultati; printf("sa kufiza"); scanf("%d",&n); q=&num[0][0];/*assings the adress of element[0][0] to q*/ p=&num[1][0];/*assings the adress of element[1][0] to p*/ *q=1;/*assings 1 to element[0][0]*/ *p=2;/*assings 2 to element[1][0]*/ *(q+1)=1; *(p+1)=3; for(j=2;j<n;j++) {*(q+j)=(*(p+j-2))+(*(p+j-1));/*assigns values to array elements*/ *(p+j)=(*(q+j-2))+(*(q+j-1));}/*assigns values to array elements*/ for(j=0;j<n;j++) {p1=p1*(*(q+j));/*calculates p1*/ p2=p2*(*(p+j));}/*calculates p2*/ rezultati=p1/p2; printf("%f",rezultati);/*prints the result*/ system("pause"); }
Last edited by Narue : May 30th, 2008 at 11:07 am. Reason: Added code tags
•
•
Join Date: Jan 2008
Posts: 1,767
Reputation:
Rep Power: 8
Solved Threads: 218
•
•
•
•
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
c Syntax (Toggle Plain Text)
#include<stdio.h> main() { int j,n,*p,*q,num[2][n]; float p1=1,p2=1,rezultati; printf("sa kufiza"); scanf("%d",&n); q=&num[0][0];/*assings the adress of element[0][0] to q*/ p=&num[1][0];/*assings the adress of element[1][0] to p*/ *q=1;/*assings 1 to element[0][0]*/ *p=2;/*assings 2 to element[1][0]*/ *(q+1)=1; *(p+1)=3; for(j=2;j<n;j++) {*(q+j)=(*(p+j-2))+(*(p+j-1));/*assigns values to array elements*/ *(p+j)=(*(q+j-2))+(*(q+j-1));}/*assigns values to array elements*/ for(j=0;j<n;j++) {p1=p1*(*(q+j));/*calculates p1*/ p2=p2*(*(p+j));}/*calculates p2*/ rezultati=p1/p2; printf("%f",rezultati);/*prints the result*/ system("pause"); }
num[2][n];
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.
.
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?
•
•
Join Date: Feb 2008
Posts: 9
Reputation:
Rep Power: 0
Solved Threads: 1
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.
#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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Playing .Wav/MIDI files in a Visual Basic Program (Visual Basic 4 / 5 / 6)
- What's the HARDEST program you've written? (Computer Science and Software Design)
- Cool little Program to disable startup programs (Windows NT / 2000 / XP / 2003)
- Program is shutting down right after program is executed (C++)
- 3d Program (Game Development)
Other Threads in the C Forum
- Previous Thread: Cygwin with DevCpp
- Next Thread: prime number question



Linear Mode