Write a program that opens a file reads in two vectors and


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:

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

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

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
Attachment:- hw3.1.c.zip

Solution Preview :

Prepared by a verified Expert
Programming Languages: Write a program that opens a file reads in two vectors and
Reference No:- TGS01245564

Now Priced at $20 (50% Discount)

Recommended (92%)

Rated (4.4/5)