首页 > > 详细

CSE 3430代写、C/C++编程语言辅导

CSE 3430 SP 23 LAB 1
AB DESCRIPTION

This lab has two parts, Part 1 and Part 2. For Part 1, we will write code to do calculations on data in a single statically allocated array (as described in slide set B-5), and we will read input for the program from a file using redirection of the input as described in class slide set B-3 on Input and Output in C. In Part 2, the data will consist of a data set of an unknown size, so we will need to use dynamic allocation of memory for the array (as described in slide set B-6).

For Part 2, again, we will read input for the program from a file using redirection of the input as described in slide set B-3. A great deal of the code that you write for Part 1, you will be able to reuse for Part 2, but you will also need to write some new code for Part 2 (see the description below). See the instructions below on how to make a copy of your source code file for Part 1 to a source code for Part 2 (BUT DO NOT DO THIS UNTIL YOU HAVE THOROUGHLY TESTED AND DEBUGGED THE CODE FOR Part 1, and WRITTEN COMMENTS for Part 1), so that you can then modify the code for Part 2.

Part 1.

DATA SET CALCULATOR Mandatory filename: lab1p1.c

PROBLEM:

The user of your program will use it to do some elementary calculations for a single floating point data set of size 8; the user will enter the data for the data set as input to the program (we will read it from a file using redirection of input; see below). Since the number of data sets is known (one) and the size of the data set is also known (8), you can use a static array to store the values. As we have seen in lecture, a static array in C to handle this problem can be declared as follows:

float dataSet[8];

int dataSetSize = 8;

You should declare this array and the dataSetSize variable in main, as shown above. When you call any function in the program that will need to access the array or that will call another function which needs to access the array, YOU WILL HAVE TO PASS BOTH dataSet and dataSetSize to the function as parameters.

In writing your code, you should remember the principle of reusability. In general, each of your functions should do a single thing. If there are several tasks that are clearly steps in doing a single thing, they can be put into the same function, but if you have any doubt, separate the tasks into different functions. By writing code in this way, functions are more reusable, and this is a very important property. See the class slide set B-7 C Software Design Principles.

Related to reusability is the principle that IN main, ONLY VARIABLE

DECLARATIONS AND CALLS TO OTHER FUNCTIONS SHOULD APPEAR. THEmain FUNCTION IN YOUR PROGRAM SHOULD HAVE NO CODE OTHER THAN THIS.

You should first call a function to print a single prompt to the user to enter the data for the data set (DO NOT PRINT THE PROMPT IN MAIN). Your program needs to read the user input and store the float values in the data set in the array that you declared in main, as shown above. The user will enter each of the 8 floating point values on separate lines (but remember that scanf will skip white space characters (such as new lines) when you call it to read numeric values such as floats). If you do not completely understand this description jump to the bottom of this file and check out the example data for Part 1. You will be given a file on Carmen called lab1p1in to test and debug your program code, so after writing and compiling your code as shown above, you should run it as follows using redirection:

$ lab1p1 < lab1p1in

After reading the values in the data set into the array, you should call another function which should repeatedly do the following (put this in a loop inside a function):

Prompt the user to choose one of the following options for a calculation to perform on the data set (ask the user to enter one of the six numbers, followed by enter):

Enter a number to choose one of the following options:

1) Calculate the sum of all the values.

2) Calculate the average of all the values.

3) Find the maximum value.

4) Find the minimum value.

5) Print the values in the data set.

6) Exit the program.

Your code should print a blank line both before, and at the end of the menu (to separate it from the preceding and following output). You can put code to print this menu of choices in the same function with a function that has a switch-case statement, and also put a call to scanf before the switch-case to get the user’s choice of option. After the user selects one of the six options, your program should use a switch-case statement to call a function to perform the necessary calculation,or terminate the program. The calculation function which is called should not also be used to print the result; the result should be printed out by a call to printf which is not in the calculation function (but you can call printf to print the result of the calculation inside the code for operation 5, which prints the values in the data set); for the first four calculations, though, there should be a call to printf to print the result after returning from the call to the calculation function in the switch-case code (that is, call printf in the code for the case that calls the calculation function). The program should output the result with an appropriate message which specifies the calculation which was performed, for example:

The maximum value is: 569.45

NOTE that there will be a deduction for unlabeled output! The results for options 1, 2, 3,and 4 should be printed out as floating point values with 2 digits of precision, preceded and followed by a blank line to separate the output from preceding and following output, and the result for option 5 should be to output a label, such as Data set: on one line, followed by the values in the data set in the order in which they were input, each on a separate line starting on the line after the label, with two digits of precision for each value. Also print a blank line after all of the values, to separate the output for this operation from any following output.

After your program outputs the result of the operation, it should prompt the user again to select an option for a calculation (by returning back to the top of the loop in which the switch-case statement is contained), until the user selects option 6 to exit the program.

Therefore, the function with the switch-case statement which gets the user’s choice of calculation should put the switch-case statement inside a loop, which will terminate when the user chooses option 6. Also, when the user chooses option 6 to exit, the function should return to main (but no return statement should be used in the function), and main should then return 0, and the program will terminate.

CONSTRAINTS (IMPORTANT):

Program structure (typical of C programs) should be:
-Any necessary #include directives for library header files (stdio.h and stdlib.h)

-Declarations of all functions in the program (except main) (ALL FUNCTIONS OTHER THAN main SHOULD BE DECLARED, BUT NOT DEFINED, BEFORE main).

-Definition of main

-Definitions of all other functions (the order does not matter)

Requirements for main:
-main should only do the following:

-Declare variables that are needed by other functions in the program. You will need the following variables in main for the code for Part 1:

float dataSet[8];

int dataSetSize = 8;

-Call other necessary functions

-NOTE: Reading of input, getting user choice of calculation to perform, and calculations should NOT be done in main (THERE WILL BE A DEDUCTION IF THE CODE IS WRITTEN TO DO THESE THINGS IN main); all of these should be done in functions separate from main.

You can use a statically declared array for Part 1, as explained above, because the number of arrays is known (namely, 1), and the size of the array is known when the code is written. · Remember that C programs should exhibit the Unix/Linux feature of reusability of functions; each function should do only one well-defined thing (or perhaps two things that are so closely related that they would ALWAYS be done together).
Use a separate function to do each type of calculation (some of these functions might call other ones; for example, the code for option 2 might call the function for option 1).
Use a function to get the input from the user about the float values in the data set.
Also use a separate function to get the user’s choice of the calculation to perform. Use a different function for each of the 5 calculation options, other than option 6, which will just terminate the loop, and return to main.
Be sure to document what each function does. Documentation is a necessary part of software for organizations. You should put a brief comment before each function definition explaining what the function does (not how it does it), and a comment for each block of code documenting what the code in the block does (not how it does it).
Even though you will have a number of functions, all code must be in a single file named lab1p1.c
Your program should be able to work if it receives input by redirection of stdin to a file (use the test input file provided to verify this, and when testing your program). The grader will grade your lab using redirection of input, so you should make sure your lab works when it is executed this way. NOTE: Please do not test your code by typing input from the keyboard! C code in industry is never, ever tested this way, for many reasons, but one very practical reason is that it is MUCH MORE WORK, and you cannot be sure that the input is the same each time you test the program!
Be sure to follow the software design principles in C identified in the class slide set B-7 C Software Design Principles posted on Carmen.
You should pay attention to the order in which you write code for the functions in the program. This is a critical part of writing, testing, and debugging software. We always want to write and test functions on which other functions depend before writing and testing the functions which depend on them. For the program for Part 1, main, and the function which reads input from the input file into the array should be written first, along with the function to print out the elements of the array (see the description of that function below),and the function with the switch-case statement embedded in a loop. DO NOT WRITE the code for any other functions before you write, test, and debug these first four functions. For other functions, until you are ready to write them, you can use “stub” functions. This is always done by writers of C code in the outside world. A stub function is a function definition with a return type and parameter names and types, but A CODE BLOCK WHICH IS EMPTY – with no statements. Here is an example:
int sumArray (float *array, int size) {

/* no code, because it is a stub function */

}If this function is called in the program, IT WILL IMMEDIATELY RETURN, and it will DO NOTHING. Thus, any calls to the stub function will not interfere with testing and debugging any other functions that this sumArray function depends on. Of course, as mentioned above, you also need to have a declaration of this function at the top of your code file after your #include directives, as mentioned above under “Constraints” related to program structure.

Part 2.

DATA SET CALCULATOR – MODIFIED Mandatory filename: lab1p2.c

IMPORTANT: DO NOT work in any way on Part 2 until you have completed your code for Part 1 and tested and debugged it thoroughly! Failing to follow this advice will make the work MUCH harder, and will make it much more likely that you will not be able to complete Part 2 correctly!

After you finish, test, and debug Part 1 thoroughly (you have verified all code works correctly,and added all comments), you should copy your C source code file for Part 1 to a source file for Part 2, using the following command in your cse3430 lab1 directory on stdlinux:

$ cp lab1p1.c lab1p2.c

This will copy the source code file lab1p1.c to a new file named lab1p2.c, but you will still have lab1p1.c in your files (it will not be deleted, only copied). The code for Part 1 will work correctly for Part 2, except for the modifications described below.

ALSO IMPORTANT: Do not start writing code for Part 2 till you have watched the Zoom video on Part 2, which is linked in the Zoom video links.pdf file on Carmen, and is labeled Lab 1 Part

MODIFIED PROBLEM:
The user of your program will use it to do some elementary floating point calculations for a single data set of an UNKNOWN SIZE. The data set consists of a list of floating point values, but the number of values in the data set will also be specified by the user in the input to the program, so you do not know in advance how many values there will be in the data set. THEREFORE, YOU CANNOT USE A STATIC ARRAY, as you did in Part 1, to store these values. As we will see in lecture, a static array in C is one declared as follows:

float array[8]; This kind of array can only be used in C when the length or size of the array, 8 in this example, IS KNOWN when writing the code. If the size of the array is not known while writing the code, the kind of array declared above, a static array, CANNOT be used

(There are sometimes students who doubt this, and think there is some trick that can be used to make this work, but PLEASE BELIEVE ME, this kind of array CANNOT be used when the size of the array is unknown at coding time!); instead, A DYNAMIC ARRAY must be used, and the space to store the elements of the array must be allocated at run time (after the program starts running). This must be done by calling a library function, malloc or calloc, to allocate the space. These functions and their use are covered in the class slides (B-6 C Pointers Part 2.pptx). The variables we will declare instead in main for the modified problem are these:

float *dataSetPtr = NULL; /* Replace declaration in lab1p1.c with this one */

int dataSetSize = 0;

We will discuss in class why, in Lab 1 Part 2, these variables are declared this way in main.

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!