如何用新值覆盖用户输入的矢量组件?
How can I override the components of a vector entered by a user with new values?
刚开始学习C,如果你能在以下方面帮助我就太好了:
我刚刚编写了一个程序,用于保存用户输入的 4 分量向量(使用名为 save_vector
的函数),打印它(使用名为 print_vector
的函数),如果任何分量为负, 它还使用函数 absolute_values
.
打印所有分量的绝对值(正数)
现在我只想使用具有绝对值的向量。我怎样才能将新的绝对值保存到同一个向量中并覆盖用户输入的值?
期待阅读任何改进这段代码的建议!谢谢! :-)
#include <stdio.h>
void print_vector(int N,float * V);
void save_vector(int N,float * V);
void absolute_values(int N, float * V);
int main(void)
{
const int n=5;
int i;
float v[n];
puts("Enter the 5 components of the vector:");
save_vector(n, v);
puts("\nThe vector is:");
print_vector(n, v);
puts("\nThe absolute vector is:");
absolute_values(n, v);
return 0;
}
void save_vector(int N, float * V)
{
int i;
for(i=0;i<N;i++)
scanf("%f",V+i);
}
void print_vector(int N, float * V)
{
int i;
for(i=0;i<N;i++)
printf(" %.2f ",*(V+i));
}
void absolute_values(int N, float * V)
{
int i;
for(i=0;i<N;i++)
{
printf(" %.2f ", ((V[i]<0)?-V[i]:V[i]));
}
}
在查看评论部分并遵循@Some 程序员老兄的建议后,将最终答案留在此处! :-)
void print_vector(int N,float * V);
void save_vector(int N,float * V);
void absolute_values(int N, float * V);
int main(void)
{
const int n=5;
int i;
float v[n];
puts("Enter the 5 components of the vector:");
save_vector(n, v);
puts("\nThe vector is:");
print_vector(n, v);
puts("\nThe absolute vector is:");
absolute_values(n, v);
return 0;
}
void save_vector(int N, float * V)
{
int i;
for(i=0;i<N;i++)
scanf("%f",V+i);
}
void print_vector(int N, float * V)
{
int i;
for(i=0;i<N;i++)
printf(" %.2f ",*(V+i));
}
void absolute_values(int N, float * V)
{
int i;
for(i=0;i<N;i++)
{
V[i]=((V[i]<0)?-V[i]:V[i]);
printf(" %f", V[i]);
}
}
刚开始学习C,如果你能在以下方面帮助我就太好了:
我刚刚编写了一个程序,用于保存用户输入的 4 分量向量(使用名为 save_vector
的函数),打印它(使用名为 print_vector
的函数),如果任何分量为负, 它还使用函数 absolute_values
.
现在我只想使用具有绝对值的向量。我怎样才能将新的绝对值保存到同一个向量中并覆盖用户输入的值?
期待阅读任何改进这段代码的建议!谢谢! :-)
#include <stdio.h>
void print_vector(int N,float * V);
void save_vector(int N,float * V);
void absolute_values(int N, float * V);
int main(void)
{
const int n=5;
int i;
float v[n];
puts("Enter the 5 components of the vector:");
save_vector(n, v);
puts("\nThe vector is:");
print_vector(n, v);
puts("\nThe absolute vector is:");
absolute_values(n, v);
return 0;
}
void save_vector(int N, float * V)
{
int i;
for(i=0;i<N;i++)
scanf("%f",V+i);
}
void print_vector(int N, float * V)
{
int i;
for(i=0;i<N;i++)
printf(" %.2f ",*(V+i));
}
void absolute_values(int N, float * V)
{
int i;
for(i=0;i<N;i++)
{
printf(" %.2f ", ((V[i]<0)?-V[i]:V[i]));
}
}
在查看评论部分并遵循@Some 程序员老兄的建议后,将最终答案留在此处! :-)
void print_vector(int N,float * V);
void save_vector(int N,float * V);
void absolute_values(int N, float * V);
int main(void)
{
const int n=5;
int i;
float v[n];
puts("Enter the 5 components of the vector:");
save_vector(n, v);
puts("\nThe vector is:");
print_vector(n, v);
puts("\nThe absolute vector is:");
absolute_values(n, v);
return 0;
}
void save_vector(int N, float * V)
{
int i;
for(i=0;i<N;i++)
scanf("%f",V+i);
}
void print_vector(int N, float * V)
{
int i;
for(i=0;i<N;i++)
printf(" %.2f ",*(V+i));
}
void absolute_values(int N, float * V)
{
int i;
for(i=0;i<N;i++)
{
V[i]=((V[i]<0)?-V[i]:V[i]);
printf(" %f", V[i]);
}
}