ENGINEERING H192 DAILY ASSIGNMENT E1

advertisement
ENGINEERING H192
DAILY ASSIGNMENT E1
Your boss has asked you to instruct a new employee in the use of drafting instruments. Your
task is to explain how to construct a polygon using drawing instruments. You have decided to
start with construction of the a regular dodecagon (12 sided polygon) using only a pencil,
straightedge, and compass. In the space below, write an algorithm (a step by step procedure) to
accomplish this task. Assume that the new employee has had no experience with the use of
drafting instruments. You must specify which instruments are to be used. It may be useful to
draw simple diagrams to help the new employee understand more easily.
Prepare your solution as a MS Word document, print it
out, and staple it behind this cover sheet. Note that MS
Word is installed on all REGION ONE PCs. You will
need a diskette in Drive A (or a zip disk in Drive D) in
order to save your document. It should be headed as
follows:
__________________________________________
|
| ENG H192 Winter Quarter 2003 01-07-03
| Daily Assignment E1
| Prepared by G. O. Bucks (Substitute your names)
|
| ASSUMPTIONS:
| 1.
|
| 2.
|
| 3.
|
| etc.
|
| ALGORITHM:
|
| 1.
|
| 2.
|
| etc.
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E2
For this assignment you are to draw a complete flowchart of the logic of for the algorithm given
below. The flowchart must be drawn neatly with a sharp soft pencil on suitable paper
(Engineering problem paper or grid paper) using a straight edge (optional), appropriate symbols,
and legible lettering.
Problem: There are a number of apples in a large box which must be sorted into baskets. An
apple is either a "large red" one, a "small red" one, or a "green" one. The total number of apples
placed into each basket should be displayed when the sorting is completed.
Assumptions:
1. There are 3 empty baskets present and labeled:
#1 – large red
#2 – small red
#3 – green
2. There are counters of some sort present.
3. The person sorting is not color blind and can tell the difference between large and
small.
4. Only these three kinds of apples are in the box.
Algorithm:
1. Set all counters to zero
2. Are there any apples in large box? (Hint: think "while")
a). If no, skip to Step 6
b). If yes, continue on with next step
3. Select an apple from the box
4. If apple is "green", then
a). Put apple in Basket #3
b). Add 1 to "green" counter
Else, if apple is "small red", then
a). Put apple in Basket #2
b). Add 1 to "small red" counter
Else
a). Put apple in Basket #1
b). Add 1 to "large red" counter
5. Go back to Step 2
6. Display number of "green", "small red", and "large red" apples
7. Stop
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E3
A. Login to a UNIX Workstation from a PC using Exceed X-windows software or a simple
telnet program. Perform the following tasks and print the UNIX commands for each exactly as
you typed it (correct case, blank spaces where needed, etc.). Use <cr> to indicate a carriage
return.
1. Create a second UNIX window (only if you are using Exceed).
>
2. Display your directory files in a UNIX window.
>
3. Copy file welcome.dat from the common area ( ~eg167hcl/welcome.dat ) to your directory.
>
4. Change the name of welcome.dat to welcome.txt.
>
5. Display the contents of welcome.txt in a UNIX window.
>
6. Print the contents of welcome.txt on one of the lab's printers. Submit the printout with this
sheet.
>
7. Erase welcome.txt from your directory.
>
B. Invoke the vi editor (vedit) in a UNIX window by typing vedit e3.cpp and then type in the
program shown on the following sheet exactly as shown (replace "Brutus Buckeye" with your
name). Save the file, then compile and link it by typing:
> CC -o e3.out e3.cpp
If an error occurs, return to the vi window, correct the program, and re-save the file. If it
compiles without an error message, run it by typing e3.out in the UNIX window where you
compiled it. When the program is running correctly, and producing correct results, print a copy
of e3.cpp by typing print e3.cpp in the UNIX window and submit it with this sheet.
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E3 (Continued)
/* ENG H192 Winter Quarter 2003 01-09-03 */
/* My name is Brutus Buckeye */
/* This is Daily Assignment E3 */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void main ( )
{
float pi = 3.14159265359, rad, volume ;
printf ("\n\n\nThis program asks you to enter the radius of\n") ;
printf ("a sphere and then calculates and prints the\n") ;
printf ("volume of the sphere.\n") ;
printf ("\nPlease enter the radius of the sphere now > ") ;
scanf ("%f", &rad) ;
printf ("rad= %f pi= %f\n ", rad, pi) ;
volume = 4.0 / 3.0 * pi * rad * rad * rad ;
printf ("\nThe volume of the sphere with a radius ") ;
printf ("\nof %6.2f inches is %10.2f cubic inches.\n", rad, volume) ;
}
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E4
Following is a C program. Evaluate by hand (or with calculator) the C expressions in the order
listed below, and write the value assigned to each variable when the program is run and the
statements are executed.
#include <stdio.h>
#include <math.h>
void main ( )
{
int a, b, c, d ;
float w, x, y = 4, z ;
int e = 3, f = 0.5, g = 2, h = 10.97 ;
float q = 5, r = .25, s = 7, t = 3/2 ;
w = g / e - 100 * f ;
/* w =
*/
w += h ;
/* w =
*/
a = t + pow (g, q) ;
/* a =
*/
b = --w + t ;
/* b =
*/
x = h % e++ ;
/* x =
*/
y = (e + w) * y ;
/* y =
*/
c = (++s) ;
/* c =
*/
z=r*s+h/e+q;
/* z =
*/
}
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E5
A. Write a complete C program to 1) prompt the user to input a length in feet from the
keyboard, 2) read the user's response, 3) compute the corresponding length in millimeters
(1 inch = 25.4 millimeters), and 4) print the results in the UNIX command window in the
form:
There are xxx millimeters in yyy feet
where xxx is the value read in and yyy is the computed value.
The following C statements may be used to input the length in feet:
printf ("\n\nEnter length in feet\n");
scanf ("%f", &ft);
and the following C statement may be used to print the computed length in millimeters:
printf ("There are %f millimeters in %f feet\n\n", mm, ft);
where mm and ft are real (i.e., float) variables.
Save your program as e5.cpp. Test your program by compiling, linking, and running it.
When it is working correctly, print the source code file e5.cpp on the printer and submit it
with this sheet.
B. Modify your program to also calculate and display the volume (in cubic millimeters) of a
sphere whose diameter is the distance (in feet) that was entered from the keyboard. Save your
modified program as e5mod.cpp. When it is working correctly print the file e5mod.cpp and
include it along with e5.cpp and this sheet.
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E6
File I/O
Write a complete C/C++ program to open an existing data file e6.dat (copy it from the common
area, ~eg167hcl, to your directory), read each line, and print it on the screen. When your
program is working correctly, modify it so that the program will also create and open an output
file named e6result.txt and print each line from the input file to this output file as well as to the
screen. Verify by inspection that the output file contents matches the input data contents.
Print a copy of your source code file, e6.cpp, and a copy of your e6result.txt file and submit
them with this sheet.
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E7
It can be shown that by using the principal of interval halving, a person can correctly guess a
number between 0 and 10 inclusive in not more than four tries with the correct answer being
known for certain after a maximum of three wrong guesses. Write a complete C/C++ program to
test whether a user can apply the principle correctly and, every time the program is run, can guess
the correct answer in not more than four tries. The algorithm below is one suggestion for the
organization of your program. Use one or more if - else if - else structures to implement the
decision making/selection process.
Algorithm:
1) Inform the user of the rules of the game, namely that he/she has four chances to guess a
number in the range 0<= x <= 10 that the computer has selected.
2) Select a random integer number in the range 0<= x <= 10
3) Prompt the user to guess what number was selected.
4) If the user guesses the correct number, print a congratulation message and terminate,
otherwise
5) If the user guesses too big a number, print a message stating so and go to step 7, otherwise
6) If the user guesses to small a number, print a message saying so.
7) If the user has made four incorrect guesses, print a condolence message and terminate,
otherwise continue with step 3.
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E8
A major use of the switch selection structure in C/C++ is to implement menu-like transfers. For
this assignment write a complete C/C++ program to calculate and display the area of 1) a square
given the length of one side, 2) a circle given the radius, and 3) an equilateral triangle given the
length of one side.
Your program should 1) prompt the user to select which figure is to be used by typing in the
name of the figure, 2) prompt the user to enter the required dimension for the figure selected, and
3) calculate and display the area of the selected figure. Your program should accept either upper
case or lower case letters for the figure name and either triangle or equilateral triangle for the
selection of the triangle. You may use just the first letter of each of the figure names to control
the choice of figure.
When the program is running correctly, submit a copy of the source code with this sheet.
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E9
A. Modify your program from assignment E7 so that it offers the user the option to play the
game again as many times as desired until he/she elects to quit. The game should execute the
first time without asking and then ask whether the user wishes to play again. The user could
enter his/her choice by typing a number or a letter. Your program MUST REQUIRE A VALID
ANSWER! If the user elects to continue, do not re-seed the random number function. Save your
modified program as e9a.cpp. Compile, link run, and if necessary, debug it. When it is running
correctly, print the source code file e9a.cpp and submit it with this sheet.
B. Modify your program from assignment E8 so that it offers the user the option to calculate and
display the area of one of the three figures as many times as desired until he/she elects to quit.
The program should keep offering the user the option to continue with another of the figures until
he/she types in "q" or "Q" to quit. When it is running correctly, print the source code file
e9b.cpp and submit it with this sheet and e9a.cpp.
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E10
An existing data file e10.dat in the common area contains measured diameters of 20 wristpins.
Write a complete C/C++ program to read all of the data, compute the average and standard
deviation, display the results, properly identified, in the UNIX Window, and also write them to
an output data file named e10result.txt. The formula for computing standard deviation is given
below:
n
s
2


x

x
 i
i 1
n  1
Save your program in file e10.cpp. Compile, link, and run and, if necessary, debug it, naming
your executable file e10.out. When it is running without error and producing correct results,
print the source code file, e10.cpp, and the output file, e10result.txt, and submit them both with
this sheet.
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E11
Many times obtaining a rigorous mathematical solution to a problem can be very difficult, but an
approximate result of sufficient accuracy may be obtained by replacing the complicated
expression with a series expansion called the Taylor Series. Retaining just enough terms to give
the desired accuracy reduces the computation time. The sine of an angle may be computed by the
series expansion,
x3
x5
x7
x9
x2n-1
sine(x) = x - -- + -- - -- + -- + ... +(-1)n+1 -------- + ...
3!
5!
7!
9!
(2n-1)!
where the angle x is expressed in radians, 3! is 3 factorial, and n is the term number., 1, 2, 3,
etc. You might note that each new term, after the first, may be computed from the preceding
term by multiplying the preceding term by - x2 and dividing by the product ((2n-1) . (2n-2)).
Complete the following C language program that has been started for you on the following page.
The main program contains a loop in which the angle varies from 0 to 90 degrees in 5 degree
increments. As it executes, it invokes a function to compute the single precision sine of the
specified angle. Within the function, each computation will be considered complete when 20
terms have been used or the absolute value of the nth term of the series becomes less than 0.01
percent of the sum of the preceding terms. Results are displayed by the main program in the
UNIX xterm window. As you complete the logic of the program, add blank lines for legibility
and include additional comments. Name the C program file e11.cpp and the executable file
e11.out. When the program is running and producing correct results, redirect the output to a text
file with the command e11.out >! e11out.txt. Submit printouts of the program file and the
output file stapled to this sheet.
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E11
/*
ENG H192
Winter Quarter 2003
Daily Assignment E11
Prepared by G.O. Bucks (Replace G.O. Bucks with your name.)
Calculate sines of angles from 0 to 90 degrees. */
#include <stdio.h>
#include <math.h>
#define pi 3.141593
/* pi is a symbolic constant. */
float mysine(float x);
/* sine function prototype. */
void main (void)
/* Start of main */
{
int angle ;
float x ;
printf ("\nENG H192 Daily Assignment E11 G.O. Bucks\n");
printf (" This program computes the sines of angles\n");
printf (" from 0 to 90 degrees at 5 degree intervals.\n");
for (angle = 0; angle <= 90; angle = angle + 5)
{
x = pi * angle / 180.;
printf("\n Angle =%6d sine =%10.6f sin =%10.6f",
angle, mysine(x), sin(x));
}
printf ("\n");
}
/* End of main. */
float mysine(float x)
/* sine function definition */
{
float xsq, term, sum ; int n ;
n = 1;
term = x ;
xsq = x * x ;
sum = . . . .
while (n < 20 && fabs (term) >= . . . .)
{
n = . . . .
term = - term * . . . .
. . . .
}
return (sum);
}
/* End of sine function. */
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E12
File e10.dat that you copied from the common area for assignment E10 contains measured
diameters of 20 wristpins. Today's assignment is to write a program consisting of a function
main ( ) and a function swap ( ) that will input the data from that same file, sort them into
ascending order, and display the sorted data in a UNIX window.
Your function main ( ) is to open the file, read all of the values into an array, and then sort them
into ascending order by:
1. Comparing successive pairs of the wristpin diameters (for example, compare
diameter[k] with diameter[k+1] and, if diameter[k+1] is smaller than diameter[k] ),
calling swap ( ) to swap or interchange them as necessary. This is to be done for the
index, k, going from 0 to "n-1", where "n" is the index of the last wristpin.
2. If any swaps were made in step 1, repeat step 1, otherwise the sort is complete and you
are ready to display the results.
3. When the sort is complete, display the results in a UNIX window.
The function prototype for function swap ( ) is:
void swap (float *, float *) ;
or
void swap (double *, double *) ;
/* If you used "float" for the array in E10 */
/* If you used "double" for the array in E10 */
Recall that two variables a and b can be swapped or interchanged by:
1. Declaring a third variable temp.
2. Assigning the value of a to temp.
3. Assigning the value of b to a.
4. Assigning the value of temp to b.
Compile, link, test, and if necessary, debug your program. Name your source code file e12.cpp.
When your program is working correctly, print e12.cpp, execute your program one more time
while redirecting your program's output to a file such as e12.txt, print e12.txt, and submit both
printed outputs with this sheet.
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E13
A data file named e13.dat exists in the class "common area" on the UNIX system. (The file is
thus named "/n5/engraph/eg167hcl/e13.dat" or something similar, but it is more easily accessed
by using a file name of ~eg167hcl/e13.dat.) The file contains actual data from one test of an
instrumented bicycle from an engineering hands-on lab experiment. You should look at the file
on the screen (with a "more" command), but DO NOT print it out, as it contains several thousand
(but less than 12,000) lines of data. You will note that at the beginning of the file there are
several lines of "header" information followed by many lines of data in four columns. Count by
hand the number of lines from the beginning of the file until you get to a line that has the actual
data in the four columns. (You will need this number in Step 2 below.) The fourth column is the
raw strain data (voltage) values from the lab experiment. Note also that the "header" information
includes the sampling rate.
Write a complete C/C++ program, e13.cpp, which does the following:
1. Opens the data file for input.
2. Input the correct number of header lines one by one, display each one on the screen
and print each one to a result file (say, e13result.dat), and then discard the
information.
3. Input each of the lines of data arranged in the four columns, discarding the data values
from each of the first three columns and storing only the data from the fourth column
in a one-dimensional array. For skipping over the columns with unwanted data, you
will need to use the assignment suppression operator, *, in the scanf format. Your
program will need to detect the end-of-file (EOF) to know when to stop inputting
data. Close the input file when you reach the EOF.
4. Find the largest value in the array and the smallest value in the array. Also determine
the elapsed time in seconds between the largest and smallest values.
5. Display the results on the screen, and also write the results to the output file,
e13result.dat. The results to be displayed and printed are:
 The total number of data points in the file
 The maximum voltage and time at which it occurred
 The minimum voltage and time at which it occurred
 The elapsed time between the maximum and minimum values
6. Calculate the strain, , for the maximum and minimum voltage values, using the
appropriate expressions from the hands-on Lab Experience. Display these values on
the screen with proper engineering units, and print them to the result file as well.
Compile & link and test your program. Debug it as may be needed. When it is running correctly,
print your source code, e13.cpp, and the output file your program generated, e13result.dat, and
submit them both with this assignment sheet.
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E14
(Part A.) Using MATLAB, create a set of numbers going from 0 to 10 in increments of 0.1 and
store it in a vector named A. Using the equation B = 4*A –2.5, define B. Plot B versus A (B on
the vertical axis, A on the horizontal axis). Label the axes with their variable names (A and B)
and title the plot "Assignment E14, your name, seat no. xx". Print out the plot and include it with
this sheet.
Write the commands you used in the space below.
(Part B.) Using MATLAB, load the data file e14.dat and display it on the screen. Plot the first
column versus the second column (second column on the vertical axis). Print out the plot and
include it with this sheet.
Write the commands you used in the space below.
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E15
File e15.dat in the common area contains several names, one per line. Copy the file to your
directory and look at it. Write a complete C/C++ program to:
1) open the file,
2) read in the names to an array of strings (an array of arrays of type char),
3) print out the list of names in the original order,
4) using a pointer array sort the names into alphabetical order, and
5) print out the list of names in alphabetical order.
Save your program in a file named e15.cpp. Compile, link, test, and debug it. When it is
running without error and producing correct results, print your source code file e15.cpp. Also,
run your working program one more time while using UNIX redirection to cause the output from
the screen to go into a file, such as e15.txt. Print this text file and include both it and the source
code printout with this sheet.
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E16
During this quarter you have been using library functions that have had their function prototypes
declared in header files such as stdio.h, math.h and stdlib.h. It is often convenient to create and
use your own library functions and declarations.
Today's assignment has three parts:
A. Create your own header file mylib.h that contains definitions for the constants:
PI
(the value of pi to at least 7 places)
E
(the value of e, the base of natural logarithms)
and the function prototypes for the function factorial ( ) that you wrote for assignment E11
and for the following two functions:
double square (double x)
{
return (x*x) ;
}
double cube (double x)
{
return (x*x*x);
}
B. Create and compile (only) a file mylib.cpp that contains the two functions shown above and
also the function factorial ( ) that you wrote for assignment E11. Your compiled file will be
named mylib.o.
Note:
The "compile only" command is:
> CC -c mylib.cpp
C. Test your library by writing and testing a short program e16.cpp that contains the preprocessor directive #include "mylib.h" and does the following:
1. Prompts the user to select whether to calculate the area of a circle a = PI*square(r), or
the volume of a sphere a = 4./3.*PI*cube(r).
2. Gets the user's response, and then prompts for and gets the radius of the selected shape.
3. Performs the required calculation using your library definitions and functions, and
displays the result in a UNIX window.
Note:
The compile/link command will be:
> CC -o e16.out e16.cpp mylib.o
When your library and test program are working correctly, print mylib.h, mylib.cpp and e16.cpp
and submit them with this sheet.
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E17
The program listed below contains a typedef for struct student, a main program that creates a
struct variable st1 of type student and a function named fillstruct ( ) that obtains data from the
user and initializes the struct variable. main ( ) invokes fillstruct ( ) to initialize st1 and then
accesses st1 and displays its contents using printf ( ).
Using vedit or other editor, create a file e17.cpp and type in the program. Save the file, compile,
link, test, and if necessary, debug it. When it is working correctly, modify it as follows:
1.) Add a member of type char to struct student to hold a string of length 4, for
example, a variable named college to hold the abbreviation for a student's college
(ENG, ASC, etc.).
2.) Add the necessary statements to fillstruct ( ) to input a value into this new member.
3.) Add a second variable st2 of type student to main ( ).
4.) Add a function call to main ( ) to initialize st2.
5.) Add printf() statements to main ( ) to display the contents of st2.
Compile, link, test, and if necessary, debug your modified program. When it is working
correctly, print the source code file e17.cpp and submit it with this sheet.
#include <stdio.h>
typedef struct student_record
{
char name[30];
long int id;
float gpa;
} student;
void fillstruct (student *num);
void main ( )
{
student st1;
fillstruct (&st1);
printf ("Student %s, id# %9ld,", st1.name, st1.id);
printf (" has a GPA of %4.2f\n", st1.gpa);
}
void fillstruct (student *num)
{
printf ("enter student name ");
fflush (stdin);
gets ((*num).name);
fflush (stdin);
printf ("enter id# (no spaces or dashes) ");
scanf ("%ld", &(*num).id);
printf ("enter grade point average ");
scanf ("%f", &(*num).gpa);
}
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E18
The program listed below contains a typedef for struct student, a main program that creates an
array eg167[5] of type student and a function fillaray ( ) that obtains data from the user and
initializes elements of the array. main ( ) invokes fillaray ( ) repeatedly to initialize the array
and then displays its contents using by repeated calls to printf ( ).
Using vedit, create a file e18.cpp and type in the program. Save the file, compile, link, test, and
if necessary, debug it. when it is working correctly, modify it as follows:
1.) Declare a new struct personal containing members gpa and a string coll[4] of type char.
2.) Replace member gpa of struct student with struct personal.
3.) Modify fillaray ( ) as necessary to correctly initialize struct student.
4.) Change the name of array eg167[ ] to eg167h[ ].
5.) Add the printf ( ) statements in main ( ) to display the contents of array eg167h[ ].
Compile, link, test, and if necessary, debug your modified program. When it is working
correctly, print the source code file e18.cpp and submit it with this sheet.
#include <stdio.h>
typedef struct student_record
{
char name[30];
long int id;
float gpa;
} student;
void fillaray (student eg167[ ], int k);
void fillaray (student eg167[ ], int k)
{
printf ("student name ");
fflush (stdin);
gets (eg167[k].name);
fflush (stdin);
printf ("id# (no spaces or dashes) ");
scanf ("%ld", &eg167[k].id);
printf ("grade point average ");
scanf ("%f", &eg167[k].gpa);
}
void main ( )
{
int k, n;
student eg167[5];
for (n = 1; n <= 5; n++)
{
printf ("Enter data for student # %d\n", n);
fillaray (eg167, n-1);
}
for (k = 0; k < 5; k++)
{
printf ("Student %s, id# %9ld,", eg167[k].name, eg167[k].id);
printf (" has a GPA of %4.2f\n", eg167[k].gpa);
}
}
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E19
Given the C structure:
struct Complex
{
float real;
float imag;
};
you are to write a C++ class definition of Complex. This class should:
1). make the variables real and imag private, and
2). contain the function prototypes for:
a). a member function input that will input two numbers and assign them to real
and imag data members,
b). a member function show which will output real, and imag, and
c). a constructor member function which will initialize the data members of an
object in this class to appropriate values.
Note that the member functions should be public.
You are to write a complete C++ program which implements the class as described above,
including the class functions. You are to provide a small main ( ) program which declares an
object in this class and tests its functions. Your main program might look something like:
void main ( )
{
Complex imag_num1, imag_num2 ;
imag_num1.input ( ) ;
imag_num1.show ( ) ;
imag_num2.show ( ) ;
}
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E20
The program listed below contains a complete program that includes a header file, defines a class
and its two member functions, and has a main program that declares an object that is a member
of the class and then invokes (calls) the two member functions to initialize the object and then
display its contents.
Using vedit or other editor, create a file e20.cpp and type in the program. Replace the header file
with iostream.h Replace each scanf ( ) with a cin. Replace each printf ( ) with a cout. Replace
the gets ( ) with a cin.get ( ) or a cin.getline ( ). Save the file.
Compile, link, test, and if necessary, debug your modified program. When it is working
correctly, print the source code file e20.cpp and submit it with this sheet.
#include <stdio.h>
class Student
{
private:
char name[30];
long int id;
float gpa;
public:
void fillaray ( );
void showdata ( );
};
void main ( )
{
Student eg;
printf ("Enter the following data for a student\n");
eg.fillaray ( );
eg.showdata ( );
}
void Student::fillaray ( )
{
printf ("name ");
gets (name);
fflush (stdin);
printf ("id# (no dashes or spaces) ");
scanf ("%ld", &id);
printf ("grade point average ");
scanf ("%f", &gpa);
}
void Student::showdata ( )
{
printf ("Student %s, id# %9ld,", name, id);
printf (" has a GPA of %4.2f\n", gpa);
}
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E21
The program listed on assignment E20 contains a complete program that includes a header file,
defines a class and its two member functions, and has a main program that declares an object that
is a member of the class and then invokes (calls) the two member functions to initialize the
object and then display its contents.
For assignment E20 you modified the program to replace all of the stdio.h functions with
iostream.h objects. For assignment E21, copy e20.cpp to e21.cpp and then, using vedit or other
editor, modify e21.cpp as follows:
Add a constructor for the class which takes “calling” parameters, and
Add a second object in main initialized with your name, an id# (not necessarily your real
one), and a gpa (again not necessarily your real one) using by parameters when the object
is created.
Save the file. Compile, link, test, and if necessary, debug your modified program. When it is
working correctly, print the source code file e21.cpp and submit it with this sheet.
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E22
Using the editor built into MATLAB (redcommended) or a text editor (an alternative) such as
Notepad, create a MATLAB script M-file containing the commands you used for Part A of
assignment E14 along with comments to explain in detail what the script file does. Save the file
as e22.m. Then, using MATLAB, run the script file and verify that it performs the same
functions as your code for Part A of E14 did. Print out e22.m and submit a copy with this sheet.
For easy reference, Part A of E14 was:
(Part A.) Using MATLAB, create a set of numbers going from 0 to 10 in increments of 0.1 and
store it in a vector named A. Using the equation B = 4*A –2.5, define B. Plot B versus A (B on
the vertical axis, A on the horizontal axis). Label the axes with their variable names (A and B)
and title the plot "Assignment E14 – Your Name, Seat No. xx". Print out the plot and include it
with this sheet.
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E23
Large timber management companies are concerned with reforestation of harvested timberlands.
A formula has been developed to compute the acreage reforested in a specified number of years
based on the number of acres left uncut and the reforestation rate, which differs for different
climates and species. If 1000 acres are left uncut and the reforestation rate is 5% (.05) then at the
end on one year 1050 acres (1000+.05*1000) will be forested, and at the end of the second year
1102.5 acres (1050+.05*1050) will be forested.
A. Write a MATLAB function that, when sent the number of forested acres at the start, the
reforestation rate and (optionally) the number of years, will calculate and return the number of
acres that will be forested. Save your function in an M-file.
B. Write a MATLAB script (save it in a script M-file, e23.m) to test the function by using it to
calculate the number of forested acres at the end of each year for a user-specified number of
years. The user should be prompted to enter the number of acres left after harvesting, the
reforestation rate, and the number of years to use in the calculation. Your script should then call
the function repeatedly to get the number of forested acres at the end of each year and should
display a table showing the initial number of forested acres and the number at the end of each
year for the specified number of years.
The MATLAB command rate = input('Enter the reforestation rate >')
can be used to get the reforestation rate from the keyboard and similar commands can be use for
initial forested acreage and number of years to use.
You have the option of calling your function once and having it calculate and return a matrix
containing data for each of the selected number of years or calling the function for each year and
having it return the data for that year. The sample fprintf statements below assume doing the
latter.
The MATLAB command fprintf('Year
column headings for the table.
forested acres\n') can be used for
The MATLAB command fprintf('start
display the initial conditions.
%10.1f\n',acres) can be used to
The MATLAB command fprintf('%3d
to display the data for each year.
%10.1f\n',year,acres) can be used
Print your script file, e23.m, your function file, and the results that were displayed in the
MATLAB command window and submit them with this sheet.
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E24
An existing data file e10.dat in the common area contains measured diameters of 20 wristpins.
You will need to transfer this file from the UNIX workstation to your PC. Write a complete
MATLAB program (as a MATLAB script M-file) to read all of the data, compute the average
and standard deviation, determine the number of parts whose diameter varies by more than one
standard deviation from the average, and display the results in the MATLAB Window. The
formula for computing standard deviation is given below:
n
s
2


x

x
 i
i 1
n  1
Include your name, table number, and section as a MATLAB comment statement in your script
file. Save your file as e24.m. Print out your script file and a copy of your results and submit
those printouts with this sheet.
Name__________________________Instr._________Room_______Seat______Hour______
ENGINEERING H192
DAILY ASSIGNMENT E25
Elastic cables AC and BC are attached to a solid rod AB of length a. Depending on the position
of end A, the angle θ between the two cables will change. The angle between these two cables
can be defined using the vector scalar product (also known as the dot product). A relatively
simple calculation shows that the angle can be defined as:
x 2  ax  720
cos 
( x 2  720)[( x  a) 2  720]
y
C
24"
12"

A
z
x
B
a
x
Write a MATLAB script that will determine the angle θ as a function of the x coordinate and the
length of rigid rod AB. Save your script as e25.m
Plot θ versus x with  20  x  20 , for rod lengths of a = 15”, 20” and 25”.
When your script is working and producing correct results, print the script and the plot and
submit them with this sheet.
Name__________________________Instr._________Room_______Seat______Hour______
Download