NO

Author Topic: What is wrong with this code ???  (Read 3354 times)

boral

  • Guest
What is wrong with this code ???
« on: April 19, 2013, 06:39:20 PM »
I run the following code. But after executing it, Pelles C stops responding. I reinstalled Pelles C , but the program persists. Please help.  :(

Code: [Select]
# include<stdio.h>
# include<math.h>

main()
{
float x[50], y[50], sum_x = 0, sum_y = 0, Ex2 = 0, Ey2 = 0, sum_xy = 0, lock, capture ;
float c[2][3] = {
{0, 0, 0},
{0, 0, 0}
} ;
int i, j, n, k, I ;

printf("Enter the number of pairs of values ( <= 50 ) ") ;
scanf("%d", &n) ;

for(i = 1; i <= n ; i++)
{
printf("Enter ( x%d , y%d ) ", i, i) ;
scanf("%f %f", &x[i], &y[i]);
}

for(i = 1; i <= n ; i++)
{
sum_x = sum_x + x[i] ;
sum_y = sum_y + y[i] ;
sum_xy = sum_xy + x[i]*y[i] ;
Ex2 = Ex2 + pow(x[i], 2)/n ;
Ey2 = Ey2 + pow(y[i], 2)/n ;
}

for(i = 1; i <= 2 ; i++)
{
for(j = 1; j <= 3 ; j++)
{
for(k = 1; k <= n ; k++)
{
if(j < 3)
c[i][j] = c[i][j] + pow( x[k], (i - 1) + (j - 1) ) ;

else
c[i][j] = c[i][j] + pow( x[k], i-1)*y[k] ;
}
printf("c[%d][%d] = %f\n", i,j, c[i][j]);
}
}
}
--------------------------------------------------------------------------------------
It seems that this line

Code: [Select]
else
c[i][j] = c[i][j] + pow( x[k], i-1)*y[k] ;

is giving trouble. But I can't find the error.
Please run this on Pelles C. You will feel what I am trying to say. Please help.  :(
« Last Edit: April 20, 2013, 06:11:55 AM by Stefan Pendl »

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: What is wrong with this code ???
« Reply #1 on: April 19, 2013, 07:48:57 PM »
Try running it from a separate DOS prompt. I get this, and it doesn't hang:

Enter the number of pairs of values ( <= 50 ) 12,34
Enter ( x1 , y1 ) Enter ( x2 , y2 ) Enter ( x3 , y3 ) Enter ( x4 , y4 ) Enter ( x5 , y5 ) Enter ( x6 , y6 ) Enter ( x7 ,
 y7 ) Enter ( x8 , y8 ) Enter ( x9 , y9 ) Enter ( x10 , y10 ) Enter ( x11 , y11 ) Enter ( x12 , y12 ) c[1][1] = 12.00000
0
c[1][2] = 12130806504245444375550000000000000000.000000
c[1][3] = nan
c[2][1] = nan
c[2][2] = nan
c[2][3] = inf


Note it asks only once for input, the other "Enter" do not wait!

boral

  • Guest
Re: What is wrong with this code ???
« Reply #2 on: April 20, 2013, 06:10:33 AM »
I don't know how to run it from separate DOS Prompt. Can you please guide me.
Please input in this way :-
Enter the number of pairs of values ( <= 50 ) 2  <Press Enter>
Enter ( x1 , y1 ) 1 <Press Space> 2  <Press Enter>
Enter ( x2 , y2 ) 2 <Press Space> 3  <Press Enter>

Output should be ( done manually ) :-
c[1][1] = 2
c[1][2] = 3
c[1][3] = 5
c[2][1] = 3
c[2][2] = 5
c[2][3] = 8
---------------------------------------------------------------
If output is not this then something is obviously wrong. But I can't find the error. Please help.
Also note that the first input is a single number and not 2 numbers.
« Last Edit: April 20, 2013, 06:15:47 AM by boral »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: What is wrong with this code ???
« Reply #3 on: April 20, 2013, 07:37:47 AM »
c[2][1] = 3
c[2][2] = 5
c[2][3] = 8
---------------------------------------------------------------
If output is not this then something is obviously wrong. But I can't find the error. Please help.
c 2D array size ?
Code: [Select]
float c[3][4] = { ... };
May the source be with you

boral

  • Guest
Re: What is wrong with this code ???
« Reply #4 on: April 20, 2013, 11:03:19 AM »
Yes c is a 2D array. It's size is c[2][3] with each element being initialized at 0 .

czerny

  • Guest
Re: What is wrong with this code ???
« Reply #5 on: April 20, 2013, 01:30:59 PM »
dim 2, index 0..1
dim 3, index 0..2

boral

  • Guest
Re: What is wrong with this code ???
« Reply #6 on: April 20, 2013, 03:42:24 PM »
I am an idiot. I forgot that indices start from 0 and not from 1. This is the source of all problem. Thanks a lot to everyone for being so kind and patient and pointing out the error.