![]() |
| | #1 |
| Registered User Join Date: May 2008
Posts: 29
| This function suppose to make the mean value zero and the Standard deviation 1, Only it makes them both zero. Somebody has an idea why? Thanks! Code: void norm_b(double b[30][30] , int i, int j)
{
double sum=0,avg, avg2;
int m,n;
for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
sum+=b[m][n];
avg=sum/(i*j);
for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
b[m][n]=b[m][n]-avg;
for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
sum+=pow(b[m][n],2);
avg2=sum/(i*j);
for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
b[m][n]=b[m][n]/sqrt(avg2);
}
|
| Livnat is offline | |
| | #2 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| Do you know what sum is supposed to be at various stages? Test it with a known set of data, then run the code with the debugger and examine the program state as it runs. When reality != expectation, you've found a bug. Without a debugger, try putting printf statements at key points to print key values.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #3 |
| Woof, woof! Join Date: Mar 2007 Location: Australia
Posts: 3,139
| You do know that array elements are sequential (regardless of dimensions too), yes? In this case, rows and columns hold no bearing. Thus it can be written as, Code: void norm_b(double * b, size_t n)
{
/* ... */
size_t i = 0;
for(i = 0; i < n; i++)
sum += b[i];
/* ... */
return;
}
Also, you might want to work on your indenting |
| zacs7 is offline | |
| | #4 |
| Registered User Join Date: May 2008
Posts: 29
| Thanks Salem for the advise. I printed the first sum and its zero no matter what array I input. I don't understand why... Hi zacs7 I'm bit afraid of pointers in this point so I prefer doing it the old two dimensional way... Any ways this is the full code, I left it out because it's long... Code: #include <stdio.h>
#include <math.h>
void scan_b(double b[30][30] , int , int );
void print_b(double b[30][30] , int , int );
void norm_b(double b[30][30], int , int );
double std_b(double b[30][30], int , int );
double avg_b(double b[30][30], int , int );
int main()
{
int k,l;
double c[30][30];
printf("please enter the number of rows and columns\n");
scanf("%d%d" , &k, &l);
scan_b(c ,k,l);
print_b (c,k,l);
norm_b( c , k, l);
printf ("the Standard deviation is: %lf\n" ,std_b( c , k, l));
printf ("the mean value is: %lf\n" ,avg_b( c , k, l));
system ("PAUSE");
return 0;
}
void scan_b(double b[30][30] , int i, int j)
{
int m,n;
printf("please enter a %d*%d array\n",i,j);
for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
scanf("%d" , &(b[m][n]));
}
void print_b(double b[30][30] , int i, int j)
{
int m,n;
for (m=0 ; m<i ; m++)
{
printf("\n");
for (n=0 ; n<j ; n++)
printf("%d" , b[m][n]);
}
printf("\n");
}
void norm_b(double b[30][30] , int i, int j)
{
double sum=0,avg, avg2;
int m,n;
for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
sum+=(b[m][n]);
printf("sum=%lf",sum);
avg=sum/(i*j);
printf("avg=%lf",avg);
for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
b[m][n]=b[m][n]-avg;
for (m=0 ; m<i ; m++)
{
printf("\n");
for (n=0 ; n<j ; n++)
printf("%d" , b[m][n]);
}
printf("\n");
for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
sum+=pow(b[m][n],2);
printf("sum2=%lf",sum);
avg2=sum/(i*j);
printf("avg2=%lf",avg);
for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
b[m][n]=b[m][n]/sqrt(avg2);
for (m=0 ; m<i ; m++)
{
printf("\n");
for (n=0 ; n<j ; n++)
printf("%d" , b[m][n]);
}
printf("\n");
}
double avg_b(double b[30][30], int i, int j)
{
double sum=0,avg, avg2;
int m,n;
for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
sum+=b[m][n];
avg=sum/(i*j);
return avg;
}
double std_b(double b[30][30] , int i, int j)
{
double sum2,sum,std,avg;
int m,n;
for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
sum+=b[m][n];
avg=sum/(i*j);
for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
sum2=pow((b[m][n]-avg),2);
std=sqrt(sum2/(i*j));
return std;
}
|
| Livnat is offline | |
| | #5 |
| CSharpener Join Date: Oct 2006
Posts: 5,242
| you should reread the format specifiers for printf and scanf %lf used for doubles in scanf, in printf %f should be used %d used for integers - using it for doubles can cause different errors including crashes
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
| | #6 |
| Registered User Join Date: May 2008
Posts: 29
| Thanks that helped! The Standard deviation is still not 1... I'll continue working on it later |
| Livnat is offline | |
| | #7 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| If you're using gcc, then use gcc -W -Wall -ansi -pedantic -O2 prog.c Fix anything it complains about, including printf/scanf format errors.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
![]() |
| Tags |
| array, function, standard deviation |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Dynamic Array Allocation function | P4R4N01D | C++ Programming | 6 | 05-15-2009 02:04 AM |
| Dikumud | maxorator | C++ Programming | 1 | 10-01-2005 06:39 AM |
| Passing my array to function | pooty tang | C Programming | 8 | 09-15-2004 12:19 PM |
| Struct *** initialization | Saravanan | C Programming | 20 | 10-09-2003 12:04 PM |
| I need help with passing pointers in function calls | vien_mti | C Programming | 3 | 04-24-2002 10:00 AM |