Computer Science Homework Solutions
Problem
#105201

C#

Programming Concepts for Engineers. See attached file for full problem description.

Attached file(s):
Attachments
hw3.pdf  View File
hw3.1.c.txt  View File
hw3.2.c.txt  View File
hw3.3.c.txt  View File
hw3.4.c.txt  View File
hw3.5.c.txt  View File

Attachment Content Summary (Note: view attachment at the above link before purchasing. Actual attachment content may vary slightly from that shown below.)

hw3.pdf
ENEE 114: Programming Concepts for Engineers
Fall 2006 Handout #24

Homework #3: Due Wednesday, November 22, 11:59p.m.

Problem 1
Download the "hw3.1.c" file from the course website (follow the "Homework 3 Files"
hyperlink). Fill in the structure template declaration such that the variable "data" can be
declared and initialized statically using the initialization values provided in the "hw3.1.c"
file. When declaring strings and arrays, use an array size that will just fit the worst-case
initialization value. (The first structure field, "s1", has been written for you). Finally, in
the "main" function, write code to print every field inside the "data" variable.

(Hint: in addition to nesting strings and arrays within a struct, you will also need to nest
a struct within a struct).


Problem 2
Download the "hw3.2.c" file from the course website. Modify your program in problem
1 to allocate the entire "data" variable dynamically. Instead of declaring "struct data
data[3]", you will declare "struct data *data" and use malloc to create the array of
structures. (This has already been done for you in the "hw3.2.c" file). In each structure
field, instead of declaring strings and arrays statically, you should simply declare a pointer
to a char (for strings) and a pointer to an int (for arrays of integers). Then, in "main",
you should use malloc to allocate each string and array one at a time. When allocating
strings and arrays, you should size the array to perfectly fit the initialization value for the
corresponding field from problem 1. In addition, you should initialize each structure field
in the "main" function. (The declaration, dynamic allocation, and runtime initialization
of the first field in the first structure element in "data" has already been done for you in
the "hw3.2.c" file). Finally, you should provide code to print every field inside the "data"
variable that you have dynamically allocated and initialized (this code should be identical
to the corresponding code in problem 1).


Problem 3
Write a program that opens a file, reads in two vectors, and then computes their dot
product:

N -1
dotproduct(v1, v2) = v1[i] v2[i] (1)
i=0


The name of the file should be passed into your program as a command-line argument.
The contents of the file consists of an integer, "N," that specifies the length of each of


1
the two vectors, followed by 2 sequences of floating point values, one for each vector. A
sample input file, called "hw3.3.in," has been provided on the course website.

Because your program will not know the size, "N," of the two vectors, you must use
dynamic memory allocation to allocate the arrays of floats that will hold the two vectors
as you read them in. Your program must also perform error checking. First, it should
make sure the right number of command-line arguments are passed in; otherwise, it should
print "usage: hw3.3 " to stderr, and exit abnormally. Second, it should
test that the input file was opened correctly; otherwise, it should print "Could not open
file " to stderr, and exit abnormally. Finally, it should check that malloc
does not fail each time it's called; otherwise, it should print "malloc ran out of memory"
to stderr, and exit abnormally.

Here's the sample output from the program:


z: hw3.3
usage: hw3.3
z: hw3.3 nofile
Could not open file nofile
z: hw3.3 hw3.3.in
Dot product = 424.46


Problem 4
Download the "hw3.4.c" file from the course website. This file contains a "main" function
that continuously prompts the user for an integer value, and then calls the three functions
"new data el", "insert ordered", and "print list". The program terminates when the user
enters -1. You are to implement the three functions called from main.

"new data el" dynamically allocates a "data el" link node, assigns the "data" field to the
value passed in as an argument, and returns a pointer to the link node. "insert ordered"
inserts the link node pointed to by "cur" into the linked list pointed to by "head". (Notice,
"head" is passed into "insert ordered" by reference). The link node should be inserted
into the list such that all the link nodes in the list are in ascending order based on the
values in the "data" fields of each link node. Finally, "print list" prints all the link nodes'
"data" fields in the linked list pointed to by "head".

Here is a sample output of the program:


z: hw3.4
List contents:
Input a data value: 1
List contents: 1
Input a data value: 5
List contents: 1 5
Input a data value: 10

2
List contents: 1 5 10
Input a data value: 2
List contents: 1 2 5 10
Input a data value: 7
List contents: 1 2 5 7 10
Input a data value: 15
List contents: 1 2 5 7 10 15
Input a data value: -2
List contents: -2 1 2 5 7 10 15
Input a data value: 0
List contents: -2 0 1 2 5 7 10 15
Input a data value: -1
z:

Problem 5
Download the "hw3.4.c" file from the course website. This file contains a "main" function
that continuously prompts the user for a command and an integer value. If the user enters
a -1 command, the program exits. If the user enters a 1 command, the program inserts
a link node with a "data" field set to the entered value into the linked list pointed to by
"head". If the user enters a 2 command, the program deletes the first link node whose
"data" field matches the entered value from the linked list pointed to by "head". You are
to provide the four functions called from "main": "new data el", "print list", "insert tail",
and "delete".

The "new data el" and "print list" functions are the same as those you created for problem
4. The "insert tail" function inserts the link node pointed to by "cur" at the tail or end of
the linked list. Tail insertion requires finding the last link node in the linked list. One way
to do this is by starting at the head of the list and traversing the list until you find the last
link node, but this is slow. To speed up tail insertion, the linked list in your program uses
two pointers: "head" and "tail". "head" points to the head or first link node in the linked
list, as normal. "tail", however, points to the tail or last link node in the linked list. Both
pointers are passed into "insert tail" (by reference). Your function should use the "tail"
pointer to perform insertion without having to traverse the entire list from the "head"
pointer. Your "insert tail" function must properly manage the "head" and "tail" pointers
such that they always point to the first and last link nodes in the list, respectively.

The "delete" function searches the list for the first link node whose "data" field is the
same as the "value" passed into the function. If such a link node is found, it is deleted
from the linked list. If no such link node is found, the "delete" function simply returns
without modifying the list. Note, your "delete" function must also properly manage the
"head" and "tail" pointers so that they always point to the first and last link nodes in the
list, respectively.

Here's a sample output of the final program:

z: hw3.5

3
List contents:
Enter insert (1) or delete (2) followed by a value: 1 1
List contents: 1
Enter insert (1) or delete (2) followed by a value: 1 2
List contents: 1 2
Enter insert (1) or delete (2) followed by a value: 1 5
List contents: 1 2 5
Enter insert (1) or delete (2) followed by a value: 1 3
List contents: 1 2 5 3
Enter insert (1) or delete (2) followed by a value: 1 6
List contents: 1 2 5 3 6
Enter insert (1) or delete (2) followed by a value: 1 1
List contents: 1 2 5 3 6 1
Enter insert (1) or delete (2) followed by a value: 2 5
List contents: 1 2 3 6 1
Enter insert (1) or delete (2) followed by a value: 2 4
List contents: 1 2 3 6 1
Enter insert (1) or delete (2) followed by a value: 2 3
List contents: 1 2 6 1
Enter insert (1) or delete (2) followed by a value: 2 2
List contents: 1 6 1
Enter insert (1) or delete (2) followed by a value: 2 1
List contents: 6 1
Enter insert (1) or delete (2) followed by a value: 2 1
List contents: 6
Enter insert (1) or delete (2) followed by a value: 2 6
List contents:
Enter insert (1) or delete (2) followed by a value: 1 1
List contents: 1
Enter insert (1) or delete (2) followed by a value: -1 0
z:




4
hw3.1.c.txt
#include

struct data_ {
char s1[11];
};

struct data_ data[3] = {{"University",
"of",
3.5,
{1, 2, 3, 4, 5},
"Maryland",
{2.4, "This", "is", 6},
7},
{"University",
"of",
7.8,
{8, 9},
"Virginia",
{1.7, "a", "string", 10},
11},
{"Georgia",
"Institute",
4.3,
{12, 13, 14},
"Technology",
{2.2, "from", "sentence", 15},
16}};

int main (int argc, char *argv[])
{
}
hw3.2.c.txt
#include
#include

struct data_ {
char *s1;
};

struct data_ *data;

int main (int argc, char *argv[])
{
data = (struct data_ *)malloc(sizeof(struct data_)*3);

data[0].s1 = (char *)malloc(sizeof("University"));
strcpy(data[0].s1, "University");
}
hw3.4.c.txt
#include
#include

#define TRUE 1
#define FALSE 0


typedef struct data_el_ {
int data;
struct data_el_ *next;
} data_el;


int main(int argc, char *argv[])
{
int value;
data_el *head, *cur;

value = 0;
while (TRUE) {
print_list(head);
printf("Input a data value: ");
scanf("%d", &value);

if (value == -1)
break;

cur = new_data_el(value);
insert_ordered(&head, cur);
}
}
hw3.5.c.txt
#include
#include

#define TRUE 1
#define FALSE 0


typedef struct data_el_ {
int data;
struct data_el_ *next;
} data_el;


int main(int argc, char *argv[])
{
int operation, value;
data_el *head = NULL, *tail = NULL, *cur;

value = 0;
while (TRUE) {
print_list(head);
printf("Enter insert (1) or delete (2) followed by a value: ");
scanf("%d %d", &operation, &value);

switch (operation) {
case -1:
return 0;
break;
case 1:
cur = new_data_el(value);
insert_tail(&head, &tail, cur);
break;
case 2:
delete(&head, &tail, value);
break;
}
}
}
Solution
What is this?
By OTA - Overall OTA Rating
Xiao Liu, MS - 4.7/5
Purchase Cost Now
$2.19 CAD (was ~$59.85)
Included in Download
  • Plain text response
  • Attached file(s):
    • hw.tar
$2.19 Instant Download
Add to Cart
Why you can trust BrainMass.com
  • Your Information is Secure
  • Best Online Academic Help Service
  • Students find real academic Success
Related Solutions
  • Programming Concept - Please describe this program for me. (See attached files for full problem description)
  • Benefits of programming knowledge - How does knowledge of programming concepts benefits individuals working in almost any IT position? Include at least one specific example of how java programming knowledge benefits nonprogramming worki ...
  • Programming language - What are the advantages and disadvantages of programming in each of the following programming languages: Java, Pascal and LISP?
  • C++ Programming Using arrays - See attached file for full problem description. Please include all the concepts as mentioned in the question.I need both the codings, exe, input and output files.
  • Do you feel that computer programming is art or science? - 1.) Do you feel that computer programming is art or science? 2.) Which programming language appeals to you, and which language do you consider least appealing?
Browse