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

Assignment Help Programming Languages
Reference no: EM13936242

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 <in file name>" to stderr, and exit abnormally. Second, it should test that the input file was opened correctly; otherwise, it should print "Could not open
file <in file name>" 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 <in file name>
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

Reference no: EM13936242

Questions Cloud

How many product lines should the organization identify : What are the advantages and disadvantages of a matrix model for GHE in terms of direct and indirect costs as well as benefits, such as improved coordination? How many product lines should the organization identify
Political and legal trends affecting fast food consumption : Discuss the demographic, sociocultural, economic, technological, natural, political and legal trends affecting fast food consumption
Compare costs calculate for table two to costs calculated : Assume that fixed costs remain at $250. When the price of a variable input changes which other costs will increase? Compare the costs you calculate for table two to the costs calculated in table one to find your answers.
Briefly the differences in output of the various versions : Use the "time" option on linux to capture the experimental run-time of the various versions of your algorithms, on various sizes of inputs. The sizes to report are 50 elements, 50 commands; 50 elements 1000 commands; 1000 elements 1000 commands; a..
Write a program that opens a file and reads in two vectors : Write a program that opens a file, reads in two vectors, and then computes their dot product
Compare output levels in settings characterized by cournnot : Two firms compete in a market to sell a homogeneous product with inverse demand function P = 400 - 2Q. Each firm produces at a constant marginal cost of $50 and has no fixed costs. Use this information to compare the output levels and profits in sett..
How this disease is affecting different racial groups : Compare and contrast how this disease is affecting different racial and ethnic groups. On the basis of your calculations, what information can you conclude
Javascript techniques to improve usability : Design a suitable applied web form example which has a work context. The form that needs to be created is a contact form for a Student Forum. Design the form which must have user input and a range of form furniture Ie. Buttons/ user Input fields. ..
Required to analyse the ethical dilemma : Identify a recent (within the last six (6) months) ethical dilemma or ethically questionable situation relating to ICT that you are familiar with. This situation can be either in the media (for example one you have sourced from a newspaper, or onl..

Reviews

Write a Review

Programming Languages Questions & Answers

  Develop the interactive calculator in bash shell script

Develop the interactive calculator completely in bash shell script (Linux programming). This calculator program must have the following features: Entry of expression acceptable to expr

  Validation and test documentation

The final project consists of a currency conversion application. This application-similar to simple, practical programs on many travel or financial Web sites-includes the following elements: Validation and test documentation

  Explain programming language is machine independent

Explain what does it mean to say that programming language is machine independent? What do you mean by operator precedence? Depending on operator precedence, what values could be related with expression 6 + 2 * 3?

  Write a program to automate the scoring of dives

The state diving commission wants to computerize the scoring at its diving competitions.

  Prepare a shift cipher

In cryptography, a Caesar cipher, also called a shift cipher, encrypts a plaintext string by shifting the letters by a fixed number of positions shift_by.

  Write an application that prints out the receipt

Write an application that prints out the receipt details for these shopping baskets...

  Create a multi-threaded competition

Create a multi-threaded competition in which opposing Robin Hoods will attack one another and try to take each other's gold coins.

  Write method that accepts as parameter reference

Write a method maxVal that accepts as parameter the reference to the head node of a linked list of integers. The method should return the largest value in the list.

  Program read data from input file and select specified items

Program should be able to read data from input file and select the specified items. Write data for school-age males to file male.out and data for school-age females to female.out.

  Program to display aggregate information for state

For each state, display aggregate information for that state, including: the total number of counties in the state, the total number of tax returns.

  Find use of dom to change the contents of an html text box

This statement will include the names of files or sections of code within a file for which he or she has been solely responsible, and the Web address of files that are visible on the group Web server space. A response such as ‘It was basically a j..

  Draw application which reads inventory records file

Draw an application which reads inventory records file and produces a report whihc show item-number, item-description, and price of every item on each day, one through 7.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd