CSC 101 Lab Week 5
Numeric and String Arrays
There are two exercises for this lab. The first exercise is about using arrays to do some simple math calculations. The second exercise is about using arrays of strings. Both exercises involve reading data from from files and using loops.
Write a program that inputs two lists of numbers and stores them in arrays named x and y. The lists have the same length, and a maximum length of 20 numbers. The lists are stored in two files named in1.dat and in2.dat. The C functions to open and read from files are covered in Section 2.7 of the book, and summarized on Page 100 book. When reading the files, use the end-of-file sentinel style of loop discussed on pages 254 and 255 of the book.
Once the numbers are read in, your program performs and displays the results of two computations. For the first computation, compute the product of corresponding elements of x and y and store the results third array named z. Output the results of this computation in a three- column table, where the columns show the values of the arrays x, y, and z. Each column should be 12 characters wide. All numeric values should be shown to three decimal places of precision.
For the second computation, compute and display the square root of the sum of the items in z. The output is a single line that shows the result of the computation.
Make up your own data, and be sure to test your program on at least one data set with number lists of exactly 20 items. One data set should have lists of 21 or greater, and one set should have significantly shorter lists. It is up to you to determine how best to handle lists with greater than 20 elements. You should inform the user in an appropriate way, and for sure, your program should not crash or otherwise mis-behave with lists of greater than 20 elements.
Name this program array_math.c.
Write a program that prompts for the name of a text file. The program will read up to 1000 lines from the file, and up to 80 characters per line. The lines are stored in an array of strings declared in the program
Your program begins by prompting for and inputting the name of the file from which it will read. It then reads the lines from that file into the array. As the programs reads in the lines, it outputs the following message for any line longer than 80 characters:
On line i characters 81 through n were ignored.where i is the line number and n is the total number of characters in that line.
If the file has more than 1000 lines, it outputs the following message:
Lines 1001 through x were ignored.where x is the total number of lines in the file.
Once all of the lines have been read in, output the first, middle, and last lines of the file, like this:
Line 1: contents of line 1For a file with n lines, the index of the middle line is defined as the truncated integer value of n/2
Line m: contents of middle line
Line n: contents of last line
Name this program read_strings.c.
Sometime before end of lab on Wednesday May 2nd, demonstrate that your programs runs correctly. To verify that you've completed the lab, submit your work as follows:
Be sure to submit a version that has the same code as the original, with only your comments added. If you modify the code to play around with it, do the code modifications in another copy of the program.handin gfisher 101_lab5 array_math.c read_strings.c