C++代写 代写C++程序 C++作业代写 C++程序代写

CPP高中作业代写

CSE 2421 SU 2020 LAB 2

Due: Thursday, May 28th, by 11:30 p.m. on Carmen

Objectives:

· Pointers

· Pointer arithmetic

· Dynamic memory allocation

· Deallocation of dynamically allocated memory

· Switch-case statements

· Functions: pass-by-reference, pass-by-value, parameters, return values and reusability · Arrays (dynamically allocated)

REMINDERS and GRADING CRITERIA:

➢ This is an individual lab. You should make a lab2 directory in your cse2421 folder on stdlinux (assuming you followed the instructions for lab1; if you did not make a cse2421 directory before, you should do that now).

➢ Every lab requires a Readme file (for this lab, it should be called README_LAB2 – use this name, without an extension or any other modification). This file should include the following: · Your name

1) Total amount of time (effort) it took for you to complete the lab

2) Short description of any concerns, interesting problems or discoveries encountered, or comments in general about the contents of the lab

3) Describe how you used gdb to find a bug in your program while debugging it. Include how you set breakpoints, variables you printed out, what values they had, what you found that enabled you to fix the bug.

➢ You should aim to always hand an assignment in on time or early. If you are late (even by a minute – or heaven forbid, less than a minute late), you will receive 75% of your earned points for the designated grade as long as the assignment is submitted by 11:30 pm the following day, based on the due date given above. If you are more than 24 hours late, you will receive a zero for the assignment and your assignment will not be graded at all.

➢ Any lab submitted that does not compile – without errors - and run WILL RECEIVE AN AUTOMATIC GRADE OF ZERO. If your code generates warnings, you will lose credit for each warning generated, depending on the warning. No exceptions will be made for this rule - to achieve even a single point on a lab, your code must minimally build (compile to an executable without errors) on stdlinux and execute on stdlinux without crashing, using the following command:

$gcc -ansi -pedantic -g -o lab2 lab2.c


➢ You should not add features to your program that are not required; this makes it more difficult for the grader to test whether the program meets the requirements, and that is what your score depends on.

LAB DESCRIPTION

DATA SET CALCULATOR (100%) Mandatory filename: lab2.c

PROBLEM:

The user of your program will use it to do some elementary floating point calculations for an

unknown number of simple data sets. Each data set consists of a list of floating point values, but the number of values in each data set will be specified by the user, so you do not know in advance how many values there will be in each data set. Additionally, you do not know in advance how many data sets the user will enter. Therefore, you cannot use a static array to store any of these values.

➢ First, you should prompt the user to enter the number of data sets. The user will enter an integer greater than or equal to 1 to indicate the number of data sets. (NOTE: Make a point to test your program to ensure entering just 1 data set works.) You program should have a separate function to get the number of data sets (that is, you cannot put code to do this in main).

➢ After calling the function to get the number of data sets, your program should call a separate function to dynamically allocate an array to hold pointers to the data sets. You should then call a separate function to dynamically allocate space to hold the sizes of the data sets [although it is possible to hold the size of a floating point data set as the first element of the array, this is not a preferred method of doing it, and you should not do it that way for this lab. You should allocate space for a separate integer array to hold the data set sizes.]

➢ You should then call a function to print a single prompt for all the data sets (do not output a separate prompt for each data set) to the user to enter the number of floating point values in each data set (which the user will enter as an integer greater than 0, so no data set will be empty), followed by the floating point values themselves on the same line (newline/enter will follow the last floating point value on the line). You can assume that the user will enter the input in this format, so you do not need to check to make sure that the format of the input meets this description, and you do not need to reject input which is not properly formatted. You can also assume that the user’s input is correct. That is, that the number of data sets entered is actually the number of data sets in the input and that the integer entered first on each line to indicate the number of values for the data set given on the rest of the line is actually the number of floating point values that follows on the same input line. Your program needs to read the user input and store the float values in each data set in a dynamically allocated array of the appropriate size. If you do not completely understand this description jump to the bottom of this file and check out the example data. (NOTE: Make a point to test your program to ensure that entering just 1 floating point value on a line works. e.g. there is only one float value in the dataset and all of the functions described below calculate correctly.)

 

NOTE CAREFULLY: Until the user tells you the size of the float data set, you cannot allocate space to store the data set, because you do not know how big the data set is. Once the user tells you the size, then you can call malloc to allocate space for the data set. After storing the size of the data set in the data set size array, and after allocating space for the data set, then you can write a loop to read the floating point values from the remainder of the line, and put them into the space allocated for the array.

NOTE ALSO: This is the most difficult function in this program. My advice is to start testing your program with a small number of data sets (say, two), with 2 or 3 float values in each of the sets. Make a test file with this data, and redirect the input of your program to this input file when you test it. DO NOT TYPE INPUT FROM THE KEYBOARD!!!! Experienced C programmers never do this, and if you do it this way, you will make your life (and the lab!) much harder. Take the time to make a test input file, and use redirection!

➢ After getting the values in each data set, your program should repeatedly do the following two things (put this in a loop in a function):

1. Print the following prompt:

Enter the number of the data set on which you wish to do calculations:

The user will enter an integer value, followed by newline, in response to this prompt (the user will enter 0 for the first data set, 1 for the second, etc.), then based on the value entered, your program must be able to access the values in the appropriate data set in the dynamically allocated storage which you have created. Then do what is described immediately below.

2. Your program should then prompt the user to choose one of the following options for a calculation based on the data set chosen by the user (ask the user to enter one of the seven numbers, followed by enter):

1) Find the minimum value.

2) Find the maximum value.

3) Calculate the sum of all the values.

4) Calculate the average of all the values.

5) Print the values in the data set.

6) Exit the program.

Your code should print a blank line at the end of the menu (to separate it from the following output). 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 should not also be used to print the result; the result should be printed out by a call to printf not in the calculation function (you can do this as part of the switch-case code). The program should output the result with an appropriate message which specifies the data set the calculation was performed on, for example:

The maximum value in data set 4 is: 567.37


The results for options 1, 2, 3, and 4 should be printed out as floating point values with 3 digits of precision, and the result for option 5 should be to output the values in the data set in the order in which they were input, with three digits of precision for each value, and with any two values separated by a tab (\t). NOTE: The code for the functions which implements calculations numbered 1 to 5 can only be written using pointer arithmetic; you cannot use array indexes (array subscripts) in these functions.

➢ After your program outputs the result of the operation, it should prompt the user again to select one of the data sets, and then one of the seven options 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, a function should be called to free (deallocate) the memory which was allocated for all of the dynamically allocated arrays in the program.

CONSTRAINTS:

· Program structure should be:

-Any necessary #include directives for library header files (stdio.h and stdlib.h) -Declarations of all functions in the program (except 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:

float **dataSetsArrayPtr = NULL; /*pointer to array of float pointers to data sets */ int *dataSetSizesArrayPtr = NULL; /*pointer to array of int sizes of data sets */

int numSets = 0; /* number of data sets */

-Call other functions

-NOTE: Reading of input, dynamic allocation of memory, getting user choice of data set and

calculation to perform, calculations, and freeing of dynamically allocated memory should

NOT be done in main; all of these should be done in functions separate from main

· You cannot use statically declared arrays for this lab, as explained above, because the sizes of the arrays

are unknown when the code is written.

· Your code should work correctly for ANY NUMBER of input data sets (including just 1),

and for any number of values in each data set (including just 1 and up to the limits of

available memory, of course), and these numbers are not known in advance.

· The calculation functions for calculations 1 through 5 must use pointer arithmetic. No array

indexes or subscripts can be used in these functions.

· 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 4 might call the function for option 3).

· Use a function to get the input from the user about the number of data sets.

· Use a separate function to get the number of values in each data set, to allocate space for the data set,

and to read in the 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 6 calculation options, including a separate function to deallocate all dynamically allocated memory, as described above.

· Be sure to document what each function does.

· Even though you will have several functions, all code must be in a single file named lab2.c

· Your program should be able to work whether it receives input from the command line (e.g.keyboard)

or via redirected (stdin) input from a file. Note: Since input from a file is being accomplished via redirection of stdin from the command line, no changes to your code should be necessary. Testing it both ways would be a great way to verify this.

· Be sure to follow the Software Design Principles in C identified in the slide set posted on Carmen.

GUIDANCE ON DOING THE LAB

-Make sure you are comfortable with the concepts in the two slides set on pointers (9_C_Pointers_Part_1 and 10_C_Pointers_Part_2) before you start writing code for the lab!

LAB SUBMISSION

LAB SUBMISSION

You should submit the files for your lab assignment on Carmen, as follows (this is similar to what you did to submit Lab1):

- First, make sure that you are in the stdlinux directory where your lab2 files are.

- To zip your files into a folder, use: $zip lab2.zip lab2.c README_LAB2

- Run firefox from stdlinux: $firefox https://carmen.osu.edu/#

- Log in to Carmen using your name.number and password, and select this course in Carmen; - Choose the Lab2 assignment on Carmen, and submit your lab2.zip file.

Be sure to submit the following files (see above): lab2.zip

You should zip the folder with your files before submitting it.

Do not submit an executable version of your program, but only C source code!

See below for a sample data set input to the program. Your program should work for all valid data sets not just the one given below. Be creative with your test data!!!

See sample input data on next page.

 

Sample Data Set Input (This does not include user responses to the prompts to select a data set or to select an operation to perform)

6

8 3.45 2.37 85.32 34.5 569.45 335.2 193.4 74.39

6 23.45 32.37 185.32 364.5 179.4 144.39

7 35.45 121.47 42.32 44.5 249.75 385.9 113.4

4 44.45 567.37 311.32 131.5

9 3.25 332.37 851.32 314.4 249.47 385.5 173.65 154.32 244.47

11 22.45 2.37 85.32 34.5 569.45 335.2 193.4 74.39 122.45 413.2 89.32 1

1

2

2

3

3

4

4

5

5

6

6


京ICP备2025144562号-1
微信
程序代写,编程代写
使用微信扫一扫关注
在线客服
欢迎在线资讯
联系时间: 全天