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

In this lab you will be building upon your knowledge of file input gained from t

ID: 3553996 • Letter: I

Question

In this lab you will be building upon your knowledge of file input gained from the last lab and also gain experience with data structures and sorting. This lab covers the following topics: Parsing command-line arguments Opening and reading files Creating data structures Sorting Program Description Write a C program, Iab6.c, that reads in a set of complex numbers from a file, sorts them by increasing order of 'magnitude" (Section 3 will define what this is and how to compute it), and then prints the sorted numbers to standard output. The file will be formatted as follows. The first line contains a single number (let's call it'n') indicating the number of complex numbers to follow in the file. The next'n' lines of the file each contain a single complex number printed in the format a + bj, where the numbers a and b will always be integers (including 0). Below is a sample run of the program, along with a listing of the input file. Sample program call and output

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <math.h>


struct Number {

int real;

int imaginary;

double magnitude;

};


int main(int argc, char *argv[])

{

FILE *file = fopen ( argv[1] , "r" );

char line [ 128 ];

fgets ( line, sizeof line, file );

//fputs ( line, stdout );

int N=0;

int n=0,neg=0;


char* sh;

sh = strtok (line," ");

//printf("%st ",sh);

char t=sh[0];

while((t-'0')>=0 && (t-'0')<=9 )

{

t=sh[n];


int h=t-'0';

//printf("%d ",h);

if(N!=0 && h!=-38)

{

N=N*10+h;

//printf("%d ",N);

}

else if(N==0 && h!=-38)

{

N=h;

//printf("%d ",N);

}

//printf("%c ",t);

n++;

}

//printf("%d ",N);

struct Number num[N];

//*num=malloc(sizeof(Number)*N);

int i=0;

if ( file != NULL )

{

while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */

{

//printf("%s",line);

char* s;

char* sq;

s = strtok (line," ");

sq = strtok (NULL, "+ j");

int e;

n=0,neg=0;

char t=s[0];


int a=0,b=0;

if(t=='-')

{

neg=1;

t=s[++n];

}

while((t-'0')>=0 && (t-'0')<=9)

{

t=s[n];


int h=t-'0';

//printf("%c ",t);

if(a!=0 && h!=-48)

{

a=a*10+h;

//printf("%d ",a);

}

else if(a==0 && h!=-48)

{

a=h;

//printf("%d ",a);

}

//printf("%c ",t);

n++;

}


if(neg==1)

a=-a;

num[i].real=a;

//printf("%d ",a);

t=sq[0];

n=0;

neg=0;

if(t=='-')

{

neg=1;

t=sq[++n];

}


a=0;


while((t-'0')>=0 && (t-'0')<=9)

{

t=sq[n];


int h=t-'0';

//printf("%c ",t);

if(a!=0 && h!=-48 )

{

a=a*10+h;

//printf("%d ",a);

}

else if(a==0 && h!=-48 )

{

a=h;

//printf("%d ",a);

}

//printf("%c ",t);

n++;

}


if(neg==1)

a=-a;

//printf("%d ",a);

num[i].imaginary=a;

num[i].magnitude=sqrt((num[i].imaginary)*(num[i].imaginary)+(num[i].real)*(num[i].real));


i++;

//fputs ( line, stdout ); /* write the line */


}

fclose ( file );



//insertion sorting loop

int j;

for (i = 1 ; i <= N - 1; i++)

{

j = i;


while ( j > 0 && num[j].magnitude < num[j-1].magnitude)

{

int temp = num[j].real;

num[j].real = num[j-1].real;

num[j-1].real = temp;


temp = num[j].imaginary;

num[j].imaginary = num[j-1].imaginary;

num[j-1].imaginary = temp;


double tem = num[j].magnitude;

num[j].magnitude = num[j-1].magnitude;

num[j-1].magnitude = tem;

j--;

}

}

//Printing the sorted numbers in the format shown in the question

for(i=0;i<N;i++)

{

printf("%d + %dj (%lf) ",num[i].real,num[i].imaginary,num[i].magnitude);

}

i=2;

free(num);

printf("%d + %dj (%lf) ",num[i].real,num[i].imaginary,num[i].magnitude);

}


else

printf("File is not in the correct format or empty");

return 0;

}

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