联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2020-04-03 11:06

Homework

Instructions:

Follow instructions carefully, failure to do so may result in points being deducted. Hand in all

your source code files through webhandin and make sure your programs compile and run by using

the webgrader interface. If your programs do not run on web grader you will get zero score

(no partial credit). You can grade yourself and re-handin as many times as you wish up until

the due date. Name your source files as described in the individual problem prompts below.

Assumptions you may make in this assignment:

In regards to error checking you can assume for this assignment that the data entered will be

of the proper data type and be within the bounds supported for that data type. You may

not assume that the data will be valid for the math operation to be performed or that in

menus a valid choice will always be entered.

Partner policy:

You are not allowed to work with a partner for this assignment.

Webgrader policy:

If webgrader doesn’t show any output, you will receive a zero score. Carefully note that

webgrader WILL NOT run if even one program has a problem. Therefore, you need to test

each program individually on the webgrader.

? Don’t upload all the source codes at once in webhandin for testing via webgrader.

? Upload the source codes one by one.

? Test each code separately via webgrader.

? If the first code runs successfully, only then upload the second code, etc.

? If any of your source code does not run on webgrader, then you should remove that from

webhandin; otherwise it might prevent webgrader from running for the other codes.

? Note that you will get zero if your webgrader does not run (even though some programs

are correct).

1

? Therefore, only submit those codes in webhandin that runs on webgrader.

A note about menus:

? Some programs will ask you to create menus. The exact formatting of menus is left

up to the programmer for each homework assignment with the expectation of it being

clear to a user.

? You may not however change an input method. For example, if we ask you to have the

user enter 1, 2, 3, or 4 you MAY NOT have the user enter a, b, c, or d.

? Use of another input method will result in a lower grade down to a possible 0 for that

problem.

? Starting with assignment 3 and on every subsequent assignment the program should

“fail” gracefully this means that on a menu selection if the user fails to input a valid

selection they should be prompted again until they input a correct response. It is

assumed you have read this and it will not necessarily be mentioned in each problem

statement, it is just a general feature you must implement. Not implementing this

menu feature will frequently cause your program to fail to run in the webgrader and

could result in a 0 for the entire assignment if your program locks up the webgrader!

Functions:

Henceforth in this class and for the rest of the semester whenever an assignment calls for

a function the function prototype in your solution must match EXACTLY the prototype

described in the assignment description. This includes function name, variable names, and

variable data types. Any deviation even if it is just a variable name will result in no points

for that problem and if your deviation causes the webgradder to crash on your solution you

could loose all points for the assignment!

2

Programs

1. Volume Converter (volEnum.c)

There are several different methods for measuring volume. For instance, you can

measure volume in cm3

, liters, quarts, and gallons. You could write several functions

to compute back and forth between these four scales. However, for this exercise, you

will write a single function to convert a single value from any of the four scales to

any of the other four scales. To support this, you will need to define an enumerated

type called Volume that enumerates the following four scales: CM, LITER, QUART,

GALLON. You will then use this enumerated type and implement the following function:

Error convertVolume(double *cm, double *liter, double *quart, double *gallon, Volume

scale)

? The return value of the convertVolume function will be an enumerated type

named Error indicating an error-level: 0 for no error, 1 for an error (negative

values), and 2 for a NULL pointer error.

? Create a main function in volEnum.c that prompts the user for one of the scales

and a value to convert and outputs the result in all four scales.

? Look online for a volume conversion table.

Enter a volume: 257.50

Enter scale (G,C,L,Q): L

Conversion:

257.50 Liters

68.02 Gallons

257500 Cubic Centimeters

272.10 Quarts

You need only concern yourself with accuracy up to 0.1 decimal places so if for example

the correct answer was 99.23 and you put 99.24, you would receive full credit

Data Type Requirement: Input for volume is of type double for volume and char

for volume choice.

Input Validation: Input should be a valid volume greater than 0, menu choices should

be a valid selection (G, C, L, Q).

You have to submit the following file containing the solution of this program

via handin: volEnum.c

3

2. Grade Book (grades.c)

Write a menu driven program that simulates a virtual grade book. The program will

print a menu of several options. These options are: enter numeric grade, find average

of the numeric grade, find number of certain letter grade, and print the number of total

numeric grades entered. Each function runs and then returns to the main menu except

for exit which ends the program.

For this problem you will use a static fixed sized array of size 100. We are assuming

that you on’t enter more than 100 numeric grades. You should initialize the array with

a sentinel value ‘-1’ for all 100 positions. When a new numeric grade is entered, it will

replace the sentinel value.

You need to implement the following functions:

void addNumericGrade ( i n t ? a r ray , i n t s i z e ) ;

double averageNumericGrade ( i n t ? a r ray , i n t s i z e ) ;

i n t coun tLe t te rG rade ( i n t ? a r ray , i n t s i z e ) ;

i n t countEnteredNumericGrades ( i n t ? a r ray , i n t s i z e ) ;

addNumericGrade: This function gets one new numerical grade from the user and

adds it to the grade book. You need to prompt the user to get a numeric grade. A valid

grade is anything between 0 and 100 inclusive. It adds the grade to the next available

free spot in the array. For example, if the array already contains three numeric grades,

it adds the new grade to the 4th block of the array.

averageNumericGrade: Average calculates the numerical arithmetic mean of all

grades entered and prints the average value to screen.

countLetterGrade: This function prompts the user for a letter grade and then

calculates how many entered numerical grades fall into that particular letter grade

range; and prints the count to screen. We will use the standard letter grade bounds for

deciding what letter a given grade corresponds to. The valid choices for letter grades

are A, B, C, D, or F.

countEnteredNumericGrades: This function returns an integer that represents a

count of the number of entered numeric grades. In other words, the returned value

should represent the effective size of the array. To get the count of the entered grades,

this function should loop through the array and check to see which values are not the

sentinel value (recommended to use -1, but any invalid input should work that also fits

in an int data type). If a value is not a sentinel value then increase a counter variable

until the end of the array. This counter variable will represent a count of the number

of entered grades.

Sample Output:

4

Main menu

1: Add a Numeric Grade

2: Average Numeric Grade

3: Number of a Certain Letter Grade

4: Number of Numeric Grades Entered

5: Exit

1

Entered grade: 91

...Print main menu again...

3

Enter a letter grade to find its total count: D

Number of the letter grades entered: 3

...Print main menu again...

4

Number of total numeric grades entered: 4

...Print main menu again...

5

Note: the line ...print main menu again... does not mean to print that literal phrase, it

means to print the menu again and is used here to save space.

Data Type Requirement: Input data is int for the menu, int for the numeric grades

in the array, and char for letter grade choice.

Input Validation: Valid menu choices are 1 to 5, valid numeric grades are between 0

and 100 inclusive. The valid choices for letter grades are A, B, C, D, or F.

REMEMBER: This program uses STATIC fixed sized arrays.

You have to submit the following file containing the solution of this program

via handin: grades.c

3. Array Utilities (array utils.c)

In this exercise you will write several functions that operate on arrays. The individual

functions are detailed below.

(a) The dot product of two arrays are a = [a1, a2, a3, .., an] and b = [b1, b2, b3, .., bn] is

a · b =

Xn

i=1

aibi = a1b1 + a2b2 + · · · + anbn

Write a function that computes the dot product of the two arrays. It returns the

dot product value.

int dotProduct(const int *a, int size, const int *b, int size2);

5

(b) Write a function that takes an integer array and returns the number of integers

that are prime in the array.

int numPrime(const int *a, int size);

(c) Write the following function.

void arrayDifference(const int *a, const int *b, int sizeOfA, int sizeOfB);

This function dynamically creates an array and fills it with all integers that can be

found in a, but not in b. The values in this dynamic array should be unique; that

is, there should not be any duplicates. Furthermore, the size of the array should

not waste memory. Display the values of this difference array.

(d) Write the following function.

void arrayIntersect(const int *a, const int *b, int sizeOfA, int sizeOfB);

This function dynamically creates an array and fills it with all integers that can

be found in a and b. The values in the dynamic array should be unique; that is,

there should not be any duplicates. Furthermore, the size of the array should not

waste memory. Display the values of this intersection array.

Program flow pseudocode:

Start

Ask user for size of int arrays A & B (assume that both A and B has the

same size)

Dynamically create arrays A & B with size provided by the user

Prompt the user for entering values into A and B

Print menu

Prompt for menu choice

Run the chosen utility function or on invalid input prompt again

Print result of the chosen utility function and return to menu

Loop until exit

end program

Sample Output:

6

Enter size of array:

3

Enter values for array A:

2 2 4

Enter values for array B:

4 4 4

Main Menu

A: Find Dot Product

B: Find Number of Prime Integers In Array

C: Array A Difference With B

D: Array Intersection

E: Exit

B

Array A or B?

A

2 prime numbers in array A

...Print main menu again...

D

intersection array is: [4]

...Print main menu again...

C

difference array is: [2]

...Print main menu again...

E

Note: the line ...print main menu again... does not mean to print that literal

phrase, it means to print the menu again and is used here to save space.

Data Type Requirement: The array contents should be any valid integer and the

size should be any valid integer greater than 0.

Input Validation: Input can be any positive integer plus 0. You should account for

special nature of 0 and 1 when checking prime. These two numbers are not prime.

REMEMBER: This program uses DYNAMIC arrays.

You have to submit the following file containing the solution of this program

via handin: array utils.c

7

? Bonus Problem: Array Backed Polynomial Functions(pollyArray.c)

A ‘C’ program can represent a real polynomial p(x) of degree n as an array of the real

number coefficients a0, a1, ..., an(an 6= 0)

P(x) = a0 + a1X + a2X

2 + ... + anX

n

Write a program that inputs a polynomial of maximum degree 8 and then evaluates

the polynomial at various values of x. Include a function get poly that fills the array

of coefficients and sets the degree of the polynomial and a function eval poly that

evaluates a polynomial at a given value of x.

Implement these functions:

void g e t p o l y ( double c o e f f [ ] , i n t ? d eg r e ePol y ) ;

double e v a l p o l y ( co n s t double c o e f f [ ] , i n t deg ree , double x ) ;

We will do this program as a run once program. Your program flow should be as follows:

program starts in the main function and does any setup you deem necessary, then calls

the get poly function and setup the polynomial. Then it returns to the main function

and get a value (via scanf) to evaluate the polynomial. It, then, calls the eval poly

function and prints resulting evaluation of the polynomial with the given value for x

plugged in and exit.

Data Type Requirement: For both polynomial coefficients and the value of X input

is of type double, the degree of the polynomial is an int.

Input Validation: For polynomial coefficients the value can be any valid double value

including negative numbers, 0, and positive numbers. The same holds true for the value

of X, any real number that will fit inside a double is a valid input.

You have to submit the following file containing the solution of this program

via handin: pollyArray.c

8


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp