100% prog to compute norm-2 of a given vector and display it
#include<stdio.h>
#include<conio.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
clrscr();
printf("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]);
} // till now we have entered the values of the vector
/* now we need square each if the element and add them up*/
for (i=0;i<n;i++)
{
total=total+c[i]*c[i];
}
/* now let's take square root to get the norm */
norm2c=sqrt(total);
printf("\n norm-2 of given vector= %f", norm2c);
}
ఔట్పుట్ తెరమీద
enter the no of rows of the column vector 1 to 4:4
enter the c[0] element:1
enter the c[1] element:-1
enter the c[2] element:2
enter the c[3] element:1
norm-2 of given vector= 2.645751
enter the c[0] element:1
enter the c[1] element:-1
enter the c[2] element:2
enter the c[3] element:1
norm-2 of given vector= 2.645751
enter the no of rows of the column vector 1 to 4:3
enter the c[0] element:1.0
enter the c[1] element:0
enter the c[2] element:-1.0
norm-2 of given vector= 1.414214
enter the c[0] element:1.0
enter the c[1] element:0
enter the c[2] element:-1.0
norm-2 of given vector= 1.414214
enter the no of rows of the column vector 1 to 4:2
enter the c[0] element:0
enter the c[1] element:0.0
norm-2 of given vector= 0.000000
enter the c[0] element:0
enter the c[1] element:0.0
norm-2 of given vector= 0.000000
గమనిక = ఒక వెక్టార్ యొక్క norm-2 విలువ సున్నా 0 వస్తే ఆ వెక్టార్ లో అన్ని elements సున్నా అని గుర్తించగలరు
/*though this program gives correct output, this is not efficient program. we could have used just 1 for loop and reduced the code length, execution time*/
కామెంట్లు
కామెంట్ను పోస్ట్ చేయండి
దయచేసి మీ సలహాలను సూచనలను స్పష్టంగా పేర్కొనగలరు. plz see that ur comments are 'acceptable' in a value based society.