Ask Programming Language Expert

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

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M91607126
  • Price:- $20

Priced at Now at $20, Verified Solution

Have any Question?


Related Questions in Programming Language

Assignment - haskell program for regular expression

Assignment - Haskell Program for Regular Expression Matching Your assignment is to modify the slowgrep.hs Haskell program presented in class and the online notes, according to the instructions below. You may carry out th ...

Assignment task -q1 a the fibonacci numbers are the numbers

Assignment Task - Q1. (a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterised by the fact that every number after the first two is the sum of the ...

Question - create a microsoft word macro using vba visual

Question - Create a Microsoft Word macro using VBA (Visual Basic for Applications). Name the macro "highlight." The macro should highlight every third line of text in a document. (Imagine creating highlighting that will ...

Assignmentquestion onegiving the following code snippet

Assignment Question One Giving the following code snippet. What kind of errors you will get and how can you correct it. A. public class HelloJava { public static void main(String args[]) { int x=10; int y=2; System.out.p ...

Assignment - proposal literature review research method1

Assignment - Proposal, Literature Review, Research Method 1. Abstract - Summary of the knowledge gap: problems of the existing research - Aim of the research, summary of what this project is to achieve - Summary of the a ...

1 write a function named check that has three parameters

1. Write a function named check () that has three parameters. The first parameter should accept an integer number, andthe second and third parameters should accept a double-precision number. The function body should just ...

Assignment - horse race meetingthe assignment will assess

Assignment - Horse Race Meeting The Assignment will assess competencies for ICTPRG524 Develop high level object-oriented class specifications. Summary The assignment is to design the classes that are necessary for the ad ...

Task silly name testeroverviewcontrol flow allows us to

Task: Silly Name Tester Overview Control flow allows us to alter the order in which our programs execute. Building on our knowledge of variables, we can now use control flow to create programs that perform more than just ...

Structs and enumsoverviewin this task you will create a

Structs and Enums Overview In this task you will create a knight database to help Camelot keep track of all of their knights. Instructions Lets get started. 1. What the topic 5 videos, these will guide you through buildi ...

Task working with arraysoverviewin this task you will

Task: Working with Arrays Overview In this task you will create a simple program which will create and work with an array of strings. This array will then be populated with values, printed out to the console, and then, w ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As