Kennesaw State University CS 4491 Assignment 3
Kennesaw State
University
Department of
Computer Science
Advanced
Topic in Computer Science CS 4491/2
Assignment #3:
Complex Numbers and Linked List
Dalibor Labudovic
02/19/2013
Initial
Problem Statement:
1.)
Using the structure previously defined, struct
ComplexNum,
develop a C program that performs complex addition, subtraction,
multiplication, and division of the complex values of the elements of
array x with the complex values of the elements in array y. Include a
function for each complex operation that returns a new array with the
results.
2.)
Design and implement in C a linked list in which each node presents a
fight stop in a route to a destination. The data in each node is the
airport code (an integer number). The airline can add or delete
intermediate flight stops. It can also calculate and display the
number of stops form the starting to the destination airport.
Summary
and Purpose of the Assignment Activity:
The
purpose of the assignment is to critically analyze and abstract the
problem. With an abstraction of the problem, a mathematical model can
be created. Working with the mathematical algorithm and principles of
software development, a computational model is developed. Once a
reasonable computational model is reached then I was able to
constructed, compiled, tested and deployed the program.
Detail
Description of the Solution Used in the Project:
Table
of Results:
1.)
#include
<stdio.h>
#include
<stdlib.h>
struct
ComplexNum {
double realPart;
double imagPart;
};
typedef struct ComplexNum
ComplexType; // Define a new type
const int N = 10; // Number of
elements
ComplexType* add(const
ComplexType* complexArray1, const ComplexType* complexArray2);
ComplexType* subtract(const
ComplexType* complexArray1, const ComplexType* complexArray2);
ComplexType* multiply(const
ComplexType* complexArray1, const ComplexType* complexArray2);
ComplexType* divide(const
ComplexType* complexArray1, const ComplexType* complexArray2);
int
main()
{
ComplexType x[N];
ComplexType y[N];
/*
Initialize the complex arrays */
x[0].realPart = 0;
x[0].imagPart = 9;
y[0].realPart = 10;
y[0].imagPart = 19;
/*
Fill the complex arrays */
int i;
for(i = 1; i < N; i++) {
x[i].realPart =
x[i-1].realPart + 5;
x[i].imagPart =
x[i-1].imagPart + 3;
y[i].realPart =
y[i-1].realPart + 7;
y[i].imagPart =
y[i-1].imagPart + 4;
}
/* Perform operations on the
complex arrays */
ComplexType* addArray;
ComplexType* subtractArray;
ComplexType* multiplyArray;
ComplexType* divideArray;
addArray = add(x, y); // Call
function
//Print
Original Values in array
printf("Values
in array x and y:\n");
int
z0;
printf("Values of original 2
arrays, x and y:\n");
for
(z0 = 0; z0 < N; z0++) {
printf("x[%d].realPart =
%lf\n", z0, x[z0].realPart);
printf("y[%d].realPart =
%lf\n\n", z0, y[z0].realPart);
printf("x[%d].imagPart =
%lf\n", z0, x[z0].imagPart);
printf("y[%d].imagPart =
%lf\n\n", z0, y[z0].imagPart);
}
//Print
addition values in array
printf("Added
Values in array x and array y:\n");
int
z1;
for
(z1 = 0; z1 < N; z1++) {
printf("addArray[%d].realPart:
%lf\n", z1, addArray[z1].realPart);
printf("addArray[%d].imagPart:
%lf\n\n", z1, addArray[z1].imagPart);
}
subtractArray
= subtract(x, y); // Call function
//Print
Subtraction
printf("Subtrated
Values in array x and array y:\n");
int
z2;
for
(z2 = 0; z2 < N; z2++) {
printf("subtractArray[%d].realPart:
%lf\n", z2, subtractArray[z2].realPart);
printf("subtractArray[%d].imagPart:
%lf\n\n", z2, subtractArray[z2].imagPart);
}
multiplyArray
= multiply(x, y); // Call function
//Print
Multiplication vallues
printf("Multiplicaiton
Values in array x and array y:\n");
int
z3;
for
(z3 = 0; z3 < N; z3++) {
printf("multiplyArray[%d].realPart:
%lf\n", z3, multiplyArray[z3].realPart);
printf("multiplyArray[%d].imagPart:
%lf\n\n", z3, multiplyArray[z3].imagPart);
}
divideArray
= divide(x, y); // Call function
//Print
Divition values
printf("Division
values in array x and array y:\n");
int
z4;
for
(z4 = 0; z4 < N; z4++) {
printf("divideArray[%d].realPart:
%lf\n", z4, divideArray[z4].realPart);
printf("divideArray[%d].imagPart:
%lf\n\n", z4, divideArray[z4].imagPart);
}
return 0;
}
///////////////////////*
Performming Functions */////////////////////////////////
//Add
Function
ComplexType* add(const
ComplexType* complexArray1, const ComplexType* complexArray2) {
//memory
allocation
ComplexType* newArray = malloc(N *
sizeof(ComplexType));
int j;
for (j = 0; j < N; j++) {
newArray[j].realPart =
complexArray1[j].realPart + complexArray2[j].realPart;
newArray[j].imagPart =
complexArray1[j].imagPart + complexArray2[j].imagPart;
}
if (newArray == NULL)
return NULL;
return newArray;
}
//Subtract
Function
ComplexType* subtract(const
ComplexType* complexArray1, const ComplexType* complexArray2) {
//memory
alloation
ComplexType* newArray = malloc(N *
sizeof(ComplexType));
int j;
for (j = 0; j < N; j++) {
newArray[j].realPart =
complexArray1[j].realPart - complexArray2[j].realPart;
newArray[j].imagPart =
complexArray1[j].imagPart - complexArray2[j].imagPart;
}
if (newArray == NULL)
return NULL;
return newArray;
}
//Multiply
Function
ComplexType* multiply(const
ComplexType* complexArray1, const ComplexType* complexArray2) {
//memory
allocation
ComplexType* newArray = malloc(N *
sizeof(ComplexType));
int j;
for (j = 0; j < N; j++) {
newArray[j].realPart =
complexArray1[j].realPart * complexArray2[j].realPart;
newArray[j].imagPart =
complexArray1[j].imagPart * complexArray2[j].imagPart;
}
return newArray;
}
//Division
Function
ComplexType* divide(const
ComplexType* complexArray1, const ComplexType* complexArray2) {
//memory
allocation
ComplexType* newArray = malloc(N *
sizeof(ComplexType));
int j;
for (j = 0; j < N; j++) {
newArray[j].realPart =
complexArray1[j].realPart / complexArray2[j].realPart;
newArray[j].imagPart =
complexArray1[j].imagPart / complexArray2[j].imagPart;
}
return newArray;
}
2.)
#include
<stdio.h>
#include
<stdlib.h>
typedef
struct node NodeType;
typedef
NodeType * NodePtr;
struct
node {
int
flightNum;
NodePtr
link;
};
NodePtr
H = NULL;//head pointer
NodePtr
c;//current node pointer
void
addNode();
void
remNode();
int
numOfStops(int loc);
void
printList();
void
addNode() {// add a stop
c = malloc(sizeof(NodePtr));//
memory allocation
c->flightNum = H->flightNum
+ 1;
c->link = H;
H = c;
}
void
remNode() {
c
= H;
H
= H->link;
free(c);
}
int
numOfStops(int loc) {
int
stops = 0;
c
= H;
while
(c->flightNum != loc) {
stops++;
c = c->link;
}
return
stops;
}
void
printList(){
c
= H;
while(c) {
printf("Flight Number:
%d\n", c->flightNum);
c = c->link ;
}
}
int
main()
{
int
i;
for(i=0;i<10;i++) {
c = malloc(sizeof(NodePtr));
c->flightNum = i;
c->link = H;
H = c;
}
int
a;// user input
int
isTrue = 1;
while(isTrue)
{
printf("Create
new flight: Press 1.\n");
printf("Remove
flight: Press 2\n");
printf("Calculate
number of stops: Press 3.\n");
printf("Print
list: Press 4.\n");
printf("Exit:
Press 5.\n");
scanf("%d",
&a);
printf("\n");
if
(a == 1) {
printf("New flight added!\n"
);
addNode(a);
}
else
if (a == 2) {
printf("Flight removed!\n");
remNode(a);
}
else
if (a == 3) {
printf("Enter flight position
to be calculated\n");
scanf("%d",
&a);
int
stops = numOfStops(a);
printf("Number
of stops is %d\n", stops);
}
else
if (a == 4) {
printList();
}
else
if (a == 5) {
isTrue = 0;
}
}
return 0;
}
Discussion
of How the System Modeled can be Improved and Extended to Include
other Services:
- The complex number program could be improved and extended to include non static data such as user input integers to calculate the desired results.
- The air port stops calculating program could be improved and extended to include time calculation of the current time to reach destination and to recalculate with each stop added or removed.
Comments
and Conclusion:
The
interesting part of the project is the new concept of linked lists
and nodes; most of the assignment was new material and needed to be
further researched. The difficulty of understanding nodes and struct
is a learning process and will remain one of the most difficult
assignments to date.
Script:
Script
started on Thu 28 Feb 2013 03:25:51 PM EST
[dlabudov@cs3
~]$ gcc ComplexNum.c -[K[K[K[K[K[K[K[K[K[K[K[K[K[K-Wall ComplexNum.c
-lm
[dlabudov@cs3
~]$ ./a.out
Values
in array x and y:
Values
of original 2 arrays, x and y:
x[0].realPart
= 0.000000
y[0].realPart
= 10.000000
x[0].imagPart
= 9.000000
y[0].imagPart
= 19.000000
x[1].realPart
= 5.000000
y[1].realPart
= 17.000000
x[1].imagPart
= 12.000000
y[1].imagPart
= 23.000000
x[2].realPart
= 10.000000
y[2].realPart
= 24.000000
x[2].imagPart
= 15.000000
y[2].imagPart
= 27.000000
x[3].realPart
= 15.000000
y[3].realPart
= 31.000000
x[3].imagPart
= 18.000000
y[3].imagPart
= 31.000000
x[4].realPart
= 20.000000
y[4].realPart
= 38.000000
x[4].imagPart
= 21.000000
y[4].imagPart
= 35.000000
x[5].realPart
= 25.000000
y[5].realPart
= 45.000000
x[5].imagPart
= 24.000000
y[5].imagPart
= 39.000000
x[6].realPart
= 30.000000
y[6].realPart
= 52.000000
x[6].imagPart
= 27.000000
y[6].imagPart
= 43.000000
x[7].realPart
= 35.000000
y[7].realPart
= 59.000000
x[7].imagPart
= 30.000000
y[7].imagPart
= 47.000000
x[8].realPart
= 40.000000
y[8].realPart
= 66.000000
x[8].imagPart
= 33.000000
y[8].imagPart
= 51.000000
x[9].realPart
= 45.000000
y[9].realPart
= 73.000000
x[9].imagPart
= 36.000000
y[9].imagPart
= 55.000000
Added
Values in array x and array y:
addArray[0].realPart:
10.000000
addArray[0].imagPart:
28.000000
addArray[1].realPart:
22.000000
addArray[1].imagPart:
35.000000
addArray[2].realPart:
34.000000
addArray[2].imagPart:
42.000000
addArray[3].realPart:
46.000000
addArray[3].imagPart:
49.000000
addArray[4].realPart:
58.000000
addArray[4].imagPart:
56.000000
addArray[5].realPart:
70.000000
addArray[5].imagPart:
63.000000
addArray[6].realPart:
82.000000
addArray[6].imagPart:
70.000000
addArray[7].realPart:
94.000000
addArray[7].imagPart:
77.000000
addArray[8].realPart:
106.000000
addArray[8].imagPart:
84.000000
addArray[9].realPart:
118.000000
addArray[9].imagPart:
91.000000
Subtrated
Values in array x and array y:
subtractArray[0].realPart:
-10.000000
subtractArray[0].imagPart:
-10.000000
subtractArray[1].realPart:
-12.000000
subtractArray[1].imagPart:
-11.000000
subtractArray[2].realPart:
-14.000000
subtractArray[2].imagPart:
-12.000000
subtractArray[3].realPart:
-16.000000
subtractArray[3].imagPart:
-13.000000
subtractArray[4].realPart:
-18.000000
subtractArray[4].imagPart:
-14.000000
subtractArray[5].realPart:
-20.000000
subtractArray[5].imagPart:
-15.000000
subtractArray[6].realPart:
-22.000000
subtractArray[6].imagPart:
-16.000000
subtractArray[7].realPart:
-24.000000
subtractArray[7].imagPart:
-17.000000
subtractArray[8].realPart:
-26.000000
subtractArray[8].imagPart:
-18.000000
subtractArray[9].realPart:
-28.000000
subtractArray[9].imagPart:
-19.000000
Multiplicaiton
Values in array x and array y:
multiplyArray[0].realPart:
0.000000
multiplyArray[0].imagPart:
171.000000
multiplyArray[1].realPart:
85.000000
multiplyArray[1].imagPart:
276.000000
multiplyArray[2].realPart:
240.000000
multiplyArray[2].imagPart:
405.000000
multiplyArray[3].realPart:
465.000000
multiplyArray[3].imagPart:
558.000000
multiplyArray[4].realPart:
760.000000
multiplyArray[4].imagPart:
735.000000
multiplyArray[5].realPart:
1125.000000
multiplyArray[5].imagPart:
936.000000
multiplyArray[6].realPart:
1560.000000
multiplyArray[6].imagPart:
1161.000000
multiplyArray[7].realPart:
2065.000000
multiplyArray[7].imagPart:
1410.000000
multiplyArray[8].realPart:
2640.000000
multiplyArray[8].imagPart:
1683.000000
multiplyArray[9].realPart:
3285.000000
multiplyArray[9].imagPart:
1980.000000
Division
values in array x and array y:
divideArray[0].realPart:
0.000000
divideArray[0].imagPart:
0.473684
divideArray[1].realPart:
0.294118
divideArray[1].imagPart:
0.521739
divideArray[2].realPart:
0.416667
divideArray[2].imagPart:
0.555556
divideArray[3].realPart:
0.483871
divideArray[3].imagPart:
0.580645
divideArray[4].realPart:
0.526316
divideArray[4].imagPart:
0.600000
divideArray[5].realPart:
0.555556
divideArray[5].imagPart:
0.615385
divideArray[6].realPart:
0.576923
divideArray[6].imagPart:
0.627907
divideArray[7].realPart:
0.593220
divideArray[7].imagPart:
0.638298
divideArray[8].realPart:
0.606061
divideArray[8].imagPart:
0.647059
divideArray[9].realPart:
0.616438
divideArray[9].imagPart:
0.654545
[dlabudov@cs3
~]$ ls
[00m[01;32ma.out[00m
[00mdistance.c[00m [00mheight.c[00m [01;34mmydir[00m
[00mrainfall.c[00m [00mtentcost.c[00m
[00mComplexNum.c[00m
[00mfreefall.c[00m [00mLinkedList.c[00m [01;32mmysession1.txt[00m
[00mstrddev.c[00m [01;32mtypescript[00m
[m[dlabudov@cs3
~]$ gcc - [KWall LinkedList.c -lm
[dlabudov@cs3
~]$ a.ou[K[K[K[K./a.out
Create
new flight: Press 1.
Remove
flight: Press 2
Calculate
number of stops: Press 3.
Print
list: Press 4.
Exit:
Press 5.
1
New
flight added!
Create
new flight: Press 1.
Remove
flight: Press 2
Calculate
number of stops: Press 3.
Print
list: Press 4.
Exit:
Press 5.
3
Enter
flight position to be calculated
4^H
2
Number
of stops is 8
Create
new flight: Press 1.
Remove
flight: Press 2
Calculate
number of stops: Press 3.
Print
list: Press 4.
Exit:
Press 5.
4
Flight
Number: 10
Flight
Number: 9
Flight
Number: 8
Flight
Number: 7
Flight
Number: 6
Flight
Number: 5
Flight
Number: 4
Flight
Number: 3
Flight
Number: 2
Flight
Number: 1
Flight
Number: 0
Create
new flight: Press 1.
Remove
flight: Press 2
Calculate
number of stops: Press 3.
Print
list: Press 4.
Exit:
Press 5.
2
Flight
removed!
Create
new flight: Press 1.
Remove
flight: Press 2
Calculate
number of stops: Press 3.
Print
list: Press 4.
Exit:
Press 5.
4
Flight
Number: 9
Flight
Number: 8
Flight
Number: 7
Flight
Number: 6
Flight
Number: 5
Flight
Number: 4
Flight
Number: 3
Flight
Number: 2
Flight
Number: 1
Flight
Number: 0
Create
new flight: Press 1.
Remove
flight: Press 2
Calculate
number of stops: Press 3.
Print
list: Press 4.
Exit:
Press 5.
3
Enter
flight position to be calculated
1
Number
of stops is 8
Create
new flight: Press 1.
Remove
flight: Press 2
Calculate
number of stops: Press 3.
Print
list: Press 4.
Exit:
Press 5.
5
[dlabudov@cs3
~]$ exit
exit
Script done on Thu
28 Feb 2013 03:27:27 PM EST
Comments
Post a Comment