Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


 

Department of Engineering Mathematics and Internetworking

ENGM1081:  Computer Programming


ASSIGNMENT # 9

 

1. If we have a system of masses mi  at points  (xi , yi ), i =  1, 2, . . . , n, in the plane, then the moments of inertia are defined as:


moment about the x axis = Ix  =

 

moment about the y axis = Iy  =

 

yi(2)mi

i

xi(2)mi

i


polar moment of inertia = IO  =      (xi(2) + yi(2))mi

i

Write a main program, moments.c, which uses the functions readmasses() and calculatemoments() to compute the moments of inertia for data read from the file momentsin.txt.

Each line of the file momentsin.txt consists of three floats giving x, y, and m for each of the masses.

The main program opens the input file momentsin.txt and passes the file pointer to the function readmasses() which reads the data into three arrays each of size no more than 100 floats.  The arrays are read from the file and the number of masses involved, n, is returned. The function readmasses() should close the input file when it has finished reading the points.

The main program then calls the function calculatemoments() which uses the arrays and the number of masses involved to calculate the three required moments.

The main program finally writes the values of the moments to the output file, momentsout.txt, in the form:

The   system   c o n s i s t s   o f   6   p o i n t   masses .     The  moments   a r e :

Ix  =  5 2 9 . 6 8

Iy  =  2 4 0 . 1 1

I o  =  7 6 9 . 7 8

2. If two arrays have the same size, we can “add” them as we do with vectors. For example:


x1

1 . 1

3 . 7

2 . 1

1 . 0

0 . 0


x2

2 . 7

1 . 7

3 . 2

2 . 0

3 . 0


sum

3 . 8

5 . 4

5 . 3

3 . 0

3 . 0


Write a program, addarrays.c, which reads two arrays x1 and x2 from the file addarraysin.txt, and writes their sum to the file addarraysout.txt.

In more detail, the main carries out the following steps:

❼ opens the two files addarraysin.txt and addarraysout.txt.

❼ reads the pairs of values from the file addarraysin.txt into the two arrays x1 and x2

❼ calls the function add() which adds the two arrays and stores the result into the array

sum

❼ writes the size of sum followed by the values of sum to the file addarraysout.txt.

 

3.  Given two arrays, we can append one array to the end of the other. For example:

 


x1

1 . 1

2 . 3

4 . 2

 


x2

0 . 7

1 . 6

3 . 2

1 . 3


r e s u l t

1 . 1

2 . 3

4 . 2

0 . 7

1 . 6

3 . 2

1 . 3


Write a program, appendarrays.c, which reads two arrays x1 and x2 from the files array1.txt                 and array2.txt, appends x2 to the end of x1, and then writes the result to the file appendarraysout.txt. In more detail main carries out the following steps:

❼ opens the three files array1.txt, array2.txt, and appendarraysout.txt.

❼ reads the first array x1 from the file array1.txt.

❼ reads the second array x2 from the file array2.txt.

❼ calls the function append(), which appends x2 to the end of x1 resulting in a new array

.

❼ writes the size of y followed by the values of y to the file appendarraysout.txt.

4. Write a program marks.c which consists of a main function and three other functions called readmarks(), changemarks(), and writemarks().

The program begins by calling the function readmarks() to read the input file marksin.txt which consists of a series of lines containing a student ID number (an integer) and a numeric mark (a float). Once the ID numbers and marks have been read into arrays (by readmarks()), the main program asks if the user wants to adjust a mark. If so, the user enters an ID number and the new mark.   The main then calls the function  changemarks() to change the mark associated with the provided student ID. The main then asks the user if they want to adjust another mark, and so on, until the user enters a student ID number of 0.  Finally, the main calls the function writemarks() to write the marks out to a file called marksout.txt.  You may assume that the input file will consist of no more than 20 marks. In detail, your functions should perform the following:

main:  opens the input file marksin.txt and the output file marksout.txt and then calls readmarks(), passing it the input file pointer, to read the IDs and marks into two arrays. Once the arrays have been read, the main then prompts the user to enter a student ID and the revised mark, calls changemark() to revise the mark, and repeats these steps until the user enters a student ID number of 0.  If changemark() cannot find the student ID in the list of possible IDs, the main should print out an error message, then prompt for the next student ID and revised mark. The user quits revising marks by entering the student ID number of 0. Finally, the main calls writemarks() (passing it the output file pointer) to write the revised marks to a file.

readmarks(): takes a file pointer and the arrays as arguments and reads the student IDs and marks into arrays id and marks. The function must also return the number of marks read.    changemarks(): this function should take 5 values as arguments:

1) the number of marks read,

2) the ID array,

3) the marks arrays,

4) the ID number of the student whose mark is to be revised, and

5) the revised mark.

 

The function should then revise the mark of the student with the ID specified in the 4th argument. If the function cannot find the specified student ID in the ID array, it should return 0.   Otherwise the function should change the mark and return 1.   (NOTE: if this function returns 0, the main should report “That student is not in the marks list”.)

writemarks():  this function should take as arguments a file pointer, the number of marks, the ID array, and the marks array, and write the revised marks out to the output file in the same format as the input file (an ID number followed by the mark on each line).

For marking purposes, change the marks of the students with the ID numbers of 2315 and 2929 to 72 and 66, respectively.