100% C prog (comparatively efficient) to find norm-2 of a vector and display the result
#include<stdio.h>
#include<math.h>
void main()
{
int i,n;
float c[4], norm2c,total=0.0;
// c is a vector whose norm has to be calculated
printf("\n enter the no of rows of the column vector 1 to 4:"); /* we are assuming max size of the vector as 4*/
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("\n enter the c[%d] element:",i); /* it can be float number */
scanf("%f",&c[i]);
total=total+c[i]*c[i];
} /* till now we have entered the values of the vector, squared each of the element and added them up*/
/* now lets take square root to get the norm */
norm2c=sqrt(total);
printf("\n norm-2 of given vector= %f", norm2c);
}//end of main
#include<math.h>
void main()
{
int i,n;
float c[4], norm2c,total=0.0;
// c is a vector whose norm has to be calculated
printf("\n enter the no of rows of the column vector 1 to 4:"); /* we are assuming max size of the vector as 4*/
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("\n enter the c[%d] element:",i); /* it can be float number */
scanf("%f",&c[i]);
total=total+c[i]*c[i];
} /* till now we have entered the values of the vector, squared each of the element and added them up*/
/* now lets take square root to get the norm */
norm2c=sqrt(total);
printf("\n norm-2 of given vector= %f", norm2c);
}//end of main
/*this prog is efficient than the just previous one as it uses 1 for loop less
ఔట్పుట్ తెరమీద enter the no of rows of the column vector 1 to 4:3
enter the c[0] element:-1
enter the c[1] element:1
enter the c[2] element:-1.0
norm-2 of given vector= 1.732051
enter the c[0] element:-1
enter the c[1] element:1
enter the c[2] element:-1.0
norm-2 of given vector= 1.732051
కామెంట్లు
కామెంట్ను పోస్ట్ చేయండి
దయచేసి మీ సలహాలను సూచనలను స్పష్టంగా పేర్కొనగలరు. plz see that ur comments are 'acceptable' in a value based society.