Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

[DEV C++] With using of programing language of DEV C++, (it is different program

ID: 3937694 • Letter: #

Question

[DEV C++] With using of programing language of DEV C++, (it is different programming language other than normal C++.)

Please help this question out..

lengthin.txt is:

3. If we have an array 27;, i = 0, 1, ... n – 1, then the elements of the array can be thought of as elements of a multi-dimensional vector. There are a number of ways to measure the norm” or "length ” of such a vector. Two measures which are in common use are the 1-norm (the taxi-cab norm) and the 2-norm (the euclidean norm) with formulas as follows: G TEP 1 norml F i=0 n-1 norm2 = is) Write a program, length.c, which reads a sequence of no more than 100 floats from the input file lengthin.txt into an array and then calls a single function, norms, which computes both norms. The main program then writes the values of the norms to the output file lengthout .txt in the form: The array has 5 entries. The norms are: 1-norm = 9.000000 2-norm = 4.358899

Explanation / Answer

#include<stdio.h>
#include<math.h>
#define MAX 1000
int main()
{
   float float_array[MAX],n1,n2,num;
   int cnt = 0;
   void norms(float a[],int cnt,float *norm1, float *norm2);
   FILE *ifp, *ofp;

   ifp = fopen("lengthin.txt","r");
   ofp = fopen("lengthout.txt","w");

   if( ifp == NULL)
   {
       printf("Cant open file ");
       return -1;
   }

   if( ofp == NULL)
   {
       printf("Cant open output file ");
       return -1;
   }

   while(!feof(ifp))
   {
       fscanf(ifp,"%f",&num);
       float_array[cnt] = num ;
       cnt++;
   }
   norms(float_array,cnt,&n1,&n2);
   printf("1-norm = %f 2-norm = %f ", n1, n2);

   fprintf(ofp,"1-norm1 = %f ",n1);
   fprintf(ofp,"2-norm1 = %f ",n2);

   fclose(ofp);

}

void norms(float a[],int size,float *norm1, float *norm2)
{
   int i;
   float sum1 = 0, sum2 = 0;

   //calculating normal 1
   for( i = 0; i < size ; i++ )
   {
       sum1 += abs(a[i]);
   }


   //calculating normal 2for( i = 0; i < size ; i++ )
   for( i = 0; i < size ; i++ )
   {
       sum2 += (a[i] * a[i]);
   }

   *norm1 = sum1 ;
   *norm2 = sqrt(sum2) ;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote