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

#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");
}

Recommended Answers

All 5 Replies

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

#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");
}

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.

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.


.

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.

and initialise rezultati to 1.0 (of course)

thank you all for your answers. they were all helpfull. the program worked:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.