显示数组总和的逻辑缺陷
flaw in logic to display sum of array
Q) 从用户那里获取一维数组的输入,为其值的总和创建新数组,例如,如果传递的数组是:| 1 | | 2 | | 3 |然后它应该打印 |1| | 3| |6| , 它会加上数组的内容即 1+2 = 3, 1+2+3 = 6, 它不应该改变数组[0] value.i 试图制作程序但它有缺陷
#include <stdio.h>
void subtotal (float[], int);
int main()
{
int n,i;
printf("Enter the size of array"); // taking size of array from user
scanf("%d",&n);
float a[n];
for (i=0;i<n;i++) // loop for entering elements of array
{
printf("Enter the element of array");
scanf("%f",&a[i]);
}
subtotal(a,n); // function call
}
void subtotal (float a[],int n) // function definition
{
int i,j;
float c;
float sum=0,minus=0;
c = a[0];
for (i=0;i<n;i++) // nested loop to calculate sum of array element
{
sum = sum - minus;
for (j=0;j<=i;j++) // this loop is used to store sum value
{
sum = sum+a[i];
minus = sum;
}
a[i] = sum; // new array element a[i] will be sum;
sum = 0;
if (i==0) // if i==0 that means we don't need to change the first value of array;
{
a[i] = c; // a[0] was stored in extra variable 'c' , hence a[i] = c;
}
}
for (i=0;i<n;i++) // this loop to print the updated array
{
printf("%.2f \t",a[i]);
}
}
告诉我我可以做哪些更改来修复这些缺陷。
对于初学者来说,函数应该做一件事:根据要求更新数组。
应该输出更新后的数组的函数main。
你的功能实现不清楚,太复杂了。
例如在这个 if 语句的注释中
if (i==0) // if i==0 that means we don't need to change the first value of array;
{
a[i] = c; // a[0] was stored in extra variable 'c' , hence a[i] = c;
}
上面写着
// if i==0 that means we don't need to change the first value of
array;
同时更新 a[0]
的值。
这个if语句上面的语句也是一样
a[i] = sum; // new array element a[i] will be sum;
函数可以通过下面的演示程序所示的方式更简单地定义。
#include <stdio.h>
void subtotal( float a[], size_t n )
{
for ( size_t i = 1; i < n; i++ )
{
a[i] += a[i-1];
}
}
int main(void)
{
float a[] = { 1.0f, 2.0f, 3.0f };
const size_t N = sizeof( a ) / sizeof( *a );
subtotal( a, N );
for ( size_t i = 0; i < N; i++ )
{
printf( "%.1f ", a[i] );
}
putchar( '\n' );
return 0;
}
程序输出为
1.0 3.0 6.0
如果您需要将部分和放在另一个数组中,则函数可以按以下方式定义
#include <stdio.h>
void subtotal( float a[], size_t n, float b[] )
{
if ( n != 0 )
{
b[0] = a[0];
for ( size_t i = 1; i < n; i++ )
{
b[i] = a[i] + b[i-1];
}
}
}
int main(void)
{
float a[] = { 1.0f, 2.0f, 3.0f };
float b[sizeof( a ) / sizeof( *a )];
const size_t N = sizeof( a ) / sizeof( *a );
subtotal( a, N, b );
for ( size_t i = 0; i < N; i++ )
{
printf( "%.1f ", b[i] );
}
putchar( '\n' );
return 0;
}
程序输出同样是
1.0 3.0 6.0
使用滚动总和更新数组中除第一个元素以外的所有元素比您想象的要简单得多。以下版本的 subtotal()
会满足您的需求:
void subtotal(float a[], int n) // function definition
{
int i;
float sum = a[0]; // Initialize our sum to the FIRSTelement
for (i = 1; i < n; ++i) { // Now, for all OTHER elements ...
sum += a[i]; // ... add the original value of a[i] to the running sum ...
a[i] = sum; // ... and THEN replace that element with the running sum.
}
for (i = 0; i < n; i++) // this loop to print the updated array
{
printf("%.2f \t", a[i]);
}
}
(我要补充一点,我同意 Vlad 的建议,即函数的输出部分最好放在 main
的主体中。)
如有任何进一步的说明,请随时提出要求and/or解释。
Q) 从用户那里获取一维数组的输入,为其值的总和创建新数组,例如,如果传递的数组是:| 1 | | 2 | | 3 |然后它应该打印 |1| | 3| |6| , 它会加上数组的内容即 1+2 = 3, 1+2+3 = 6, 它不应该改变数组[0] value.i 试图制作程序但它有缺陷
#include <stdio.h>
void subtotal (float[], int);
int main()
{
int n,i;
printf("Enter the size of array"); // taking size of array from user
scanf("%d",&n);
float a[n];
for (i=0;i<n;i++) // loop for entering elements of array
{
printf("Enter the element of array");
scanf("%f",&a[i]);
}
subtotal(a,n); // function call
}
void subtotal (float a[],int n) // function definition
{
int i,j;
float c;
float sum=0,minus=0;
c = a[0];
for (i=0;i<n;i++) // nested loop to calculate sum of array element
{
sum = sum - minus;
for (j=0;j<=i;j++) // this loop is used to store sum value
{
sum = sum+a[i];
minus = sum;
}
a[i] = sum; // new array element a[i] will be sum;
sum = 0;
if (i==0) // if i==0 that means we don't need to change the first value of array;
{
a[i] = c; // a[0] was stored in extra variable 'c' , hence a[i] = c;
}
}
for (i=0;i<n;i++) // this loop to print the updated array
{
printf("%.2f \t",a[i]);
}
}
告诉我我可以做哪些更改来修复这些缺陷。
对于初学者来说,函数应该做一件事:根据要求更新数组。
应该输出更新后的数组的函数main。
你的功能实现不清楚,太复杂了。
例如在这个 if 语句的注释中
if (i==0) // if i==0 that means we don't need to change the first value of array;
{
a[i] = c; // a[0] was stored in extra variable 'c' , hence a[i] = c;
}
上面写着
// if i==0 that means we don't need to change the first value of array;
同时更新 a[0]
的值。
这个if语句上面的语句也是一样
a[i] = sum; // new array element a[i] will be sum;
函数可以通过下面的演示程序所示的方式更简单地定义。
#include <stdio.h>
void subtotal( float a[], size_t n )
{
for ( size_t i = 1; i < n; i++ )
{
a[i] += a[i-1];
}
}
int main(void)
{
float a[] = { 1.0f, 2.0f, 3.0f };
const size_t N = sizeof( a ) / sizeof( *a );
subtotal( a, N );
for ( size_t i = 0; i < N; i++ )
{
printf( "%.1f ", a[i] );
}
putchar( '\n' );
return 0;
}
程序输出为
1.0 3.0 6.0
如果您需要将部分和放在另一个数组中,则函数可以按以下方式定义
#include <stdio.h>
void subtotal( float a[], size_t n, float b[] )
{
if ( n != 0 )
{
b[0] = a[0];
for ( size_t i = 1; i < n; i++ )
{
b[i] = a[i] + b[i-1];
}
}
}
int main(void)
{
float a[] = { 1.0f, 2.0f, 3.0f };
float b[sizeof( a ) / sizeof( *a )];
const size_t N = sizeof( a ) / sizeof( *a );
subtotal( a, N, b );
for ( size_t i = 0; i < N; i++ )
{
printf( "%.1f ", b[i] );
}
putchar( '\n' );
return 0;
}
程序输出同样是
1.0 3.0 6.0
使用滚动总和更新数组中除第一个元素以外的所有元素比您想象的要简单得多。以下版本的 subtotal()
会满足您的需求:
void subtotal(float a[], int n) // function definition
{
int i;
float sum = a[0]; // Initialize our sum to the FIRSTelement
for (i = 1; i < n; ++i) { // Now, for all OTHER elements ...
sum += a[i]; // ... add the original value of a[i] to the running sum ...
a[i] = sum; // ... and THEN replace that element with the running sum.
}
for (i = 0; i < n; i++) // this loop to print the updated array
{
printf("%.2f \t", a[i]);
}
}
(我要补充一点,我同意 Vlad 的建议,即函数的输出部分最好放在 main
的主体中。)
如有任何进一步的说明,请随时提出要求and/or解释。