解析ECE114、C/C++ Programming讲解、辅导C/C++编程、C/C++设计留学生讲解、解析C/C++

- 首页 >> C/C++编程

ECE114 Intro to C/C++ Programming

Fall 2018

1

Assignment #2

(due on Thursday, 9/27, 11:59pm)

Upload your SOURCE FILES (.c) to Blackboard with the file name hw2_1_LastName_FirstName.c

and hw2_2_ LastName_FirstName.c (e.g. hw2_1_Montana_Joe.c). The first line of each

program should comment with the file name and your full name (e.g. //

hw2_1_Montana_Joe.c by Joe Montana). You SHOULD NOT submit executable files (The

Blackboard does accept .exe files); however, try to run your program successfully before you

submit your source files. No late assignment!! Never show or talk about your code to your

classmates. If you meet a problem submitting your files to Blackboard, you can email me your

assignment by the due time.

Grading: (10 points for each question, 20 points in total)

Correctness No errors, input/output correct, output presented nicely 6 points

Solution Code is efficient but easy to follow 2 points

Style Variable names, comments, indentation 2 points

Q1. We want to create a program where the user can enter a list of names of students for

the computer to print out. The program will end when the user inputs the letter ‘q’. We

also want to know how many students will be left over if we separate students into

groups of N, where your program gets N from the user. In order to do this we must find

a way to keep track of the total number of students. We will assume that there is at

least 1 student in the class.

Sample Output:

Please enter the number of people in each group (> 0): 6

Please start inputting the names of the students in the class: Emre

Student name: Emre

Enter the next name: Paul

Student name: Paul

Enter the next name: Ryan

Student name: Ryan

Enter the next name: Steven

Student name: Steven

Enter the next name: Alfredo

Student name: Alfredo

Enter the next name: Carolina

Student name: Carolina

Enter the next name: Cassidy

Student name: Cassidy

Enter the next name: Abrar

Student name: Abrar

Enter the next name: q

If you separate 8 students into groups of 6 you will have 2 students remaining.

ECE114 Intro to C/C++ Programming

Fall 2018

2

Hint:

Include <string.h> in order to use strcmp. strcmp(string1, string2) compares

two character strings. We will learn about it later but for now all you need to

know is that it returns 0 if and only if the strings are the same. To check if a

string is equal to "q" you would say strcmp(string, "q") == 0.

Q2. In this assignment, you will write a simple Linear Regression program, one of

the most widely used statistical tools ever to exist. The idea is to explain the

relationship between two (or more) variables using a straight line. For example,

consider a chemical process where the yield of the process is measured under

different reaction temperatures, and the obtained data is plotted as below.

Clearly, there seems to be a linear

relationship between the dependent

variable ‘Yield’ with independent variable

‘Temperature’. Linear Regression would

take all the observed data pairs (x1, y1),

(x2, y2) etc. as input, and fit them to a line.

To motivate another example, Disneyland

runs regression analysis on a daily basis to

estimate the number of visitors expected

that day (y) based on a large number of

independent variables (x), such as

whether it is school holiday or what the weather is. Based on this estimation,

they optimize operation decisions, such as the number of puppeteers to hire.

For this assignment, let us stick to the basic yield (y)-temperature (x) example.

To make our lives easy, we will work with just three (x, y) pairs of data. Your

program will ask the user for the data as shown in the sample below, and scan

them as floating points. You will then find intercept a and slope b for the fitted

line y = a+bx using the formula below:

?? =

(∑ ??)(∑ ??

2

) ? (∑ ??)(∑ ????)

??(∑ ??

2) ? (∑ ??)

2

?? =

??(∑ ????) ? (∑ ??)(∑ ??)

??(∑ ??

2) ? (∑ ??)

2

As a hint, say the three input temperatures are saved in variables x1, x2 and x3.

You can then compute ∑ ??

2

as,

float sum_x2;

sum_x2 = x1*x1 + x2*x2 + x3*x3;

ECE114 Intro to C/C++ Programming

Fall 2018

3

which you can in turn use to compute a and b. We will have n = 3 for this

problem. Your program should print the fitted equation as shown in the sample

below. Please print the coefficients up to 2 decimal points. Finally, you need to

ask the user for yet another temperature value, and print the estimated yield

based on the computed relationship. Assume that the user is cooperative and

only provides valid inputs. The output of your program should match the

following:

Sample Output:

Welcome to the Linear Regression Tool!

Please enter temperature x1: 50

Please enter yield y1: 122

Please enter temperature x2: 53

Please enter yield y2: 118

Please enter temperature x3: 54

Please enter yield y3: 128

The fitted equation is:

yield = 78.38 + 0.85 * temperature

Please enter a new temperature: 52

The estimated yield is 122.38


站长地图