Psuedocode

Writing

engineering matlab

Question Description

Psuedocode is the word representation of how you want to proceed with
the program.

Please refer to MODULE 5 (Selection Statements) Presentation to
understand what a pseudo code is.


I will attach both the presentation (MODULE 5) so you can see what
Psuedocode is, and i will upload the word document that describes what

Unformatted Attachment Preview

Branches, Flowcharts, and Pseudocode • ENGR 102 • West Virginia University • Statler College of Engineering & Mineral Resources Flowcharts: • Are visual representations of a sequence of steps and decisions needed to perform a process. • Can be used to map out the logic of a computer program. • Can be used to plan out your thoughts prior to coding or to share the logic of a program with someone who is unfamiliar with the syntax of the language. • Standard symbols are used in certain situations. Figure 1: Basic symbols and their functions Figure 2: Example of a flowchart for a simple thermostat on a furnace Pseudocode: • Pseudocode is an informal way to express the design of a computer program or an • • • • algorithm. Its goal is to communicate the basic idea of the computer code without including exact syntax. There is no “industry standard” or accepted method of writing pseudocode. Can think of pseudocode as writing the program out in words using shorthand, and not complete sentences. Sometimes it is a good way to show someone what you intend for a code to do or to plan out your own code before you actually start writing the code in MATLAB®. Pseudocode Example: • If you were ask to write a code in MATLAB which allowed a to input temperature and display if heat is turned on or off. Ask for user input of the room temperature Check to see if the temp is greater than or equal to 70 If greater than or equal to 70 Display “Turn off heat” If temp less than 70 Display “Turn on heat” End While the actual Code would look like. . .. Temp=('Please enter the temperature') if Temp>=70 disp(‘Turn off heat') else disp(Turn on heat’) end Pseudocode Example: • If you were ask to write a code in MATLAB which allowed a student to enter their grade as a percentage and the program would return their letter grade (A-C) the pseudocode could look like this. . . Ask for user input of their percentage grade Check to see if the grade is greater than or equal to 90 If greater than or equal to 90 Display “A” If grade is not greater than or equal to 90 check to see if it is greater than or equal to 80 If greater than or equal to 80 Display “B” If grade is not greater than or equal to 80 check to see if it is greater than or equal to 70 If greater than or equal to 70 Display “C” If none of the above are true Display “You Must Repeat The Class” End While the actual Code would look like. . .. Grade=input('Please enter the temperature') if Grade>=90 disp('A') elseif Grade >= 80 disp('B’) elseif Grade >= 70 disp('C’) else disp('You Must Repeat the Class’) end Conditional Statements A conditional statement is a command that allows MATLAB to decide whether or not to execute some code that follows the statement • Conditional statements almost always part of scripts or functions • They have three general forms • if-end • if-else-end • if-elseif-else-end 11 Conditional Statements: • Conditional statements allow MATLAB to make decisions • Typically that decisions is whether or not to execute a group of commands • A conditional expression is stated in the conditional statement, if this expression is true, a group if commands that follow the statement are executed, if the expression is false, the group of commands is skipped • The basic conditional statement is an if statement MATLAB Syntax if conditional expression consisting of relational and/or logic operators end Note: Every if statement must end with an end statement if Statements: • Examples: if a< b if a>=c if a==d if (a5) Example of an if-end Statement: • Example: Write a MATLAB script to calculate the shipping cost of an order, if the order weighs less than 5 lbs the shipping cost is $5.00, for every pound over 5 the shipping cost increases $0.10. MATLAB Syntax w = input('Please input the weight of the shipment in lbs '); Cost = 5.00; if w>5.00 Cost = Cost + (w-5)*(0.10); end fprintf('The shipment cost is $%f', Cost) Command Window Please input the weight of the shipment in lbs 10 The shipment cost is $5.500000 ICA • Write a matlab script to prompt the user for number and print its square root if the number is positive. If Else Statements: • The if-else statement allows the user to provide a means for choosing one group of commands, out of two possible groups of commands to be executed • Must also conclude with an end statement • The logic check occurs, if the check is false, command group 2 is executed, if the statement is true, then command group 1 is executed. Example of an if-else-end Statement: • Example: Write a MATLAB script to calculate the shipping cost of an order, if the order weighs less than 10 lbs the shipping cost is $0.00, If the weight is greater than or equal to 10 lbs the order costs $5.00 plus $0.10 for every pound over 10. MATLAB Syntax w = input('Please input the weight of the shipment in lbs '); if w<10.00 Cost = 0.00; else Cost = 5.00 + (w-10)*(0.10); end fprintf('The shipment cost is $%f', Cost) Command Window Please input the weight of the shipment in lbs 2 The shipment cost is $0.000000 Please input the weight of the shipment in lbs 15 The shipment cost is $5.500000 ICA • Write a matlab script to prompt the user for number and print its square root if the number is positive, print absolute value otherwise. • Write a matlab script to calculate the area of circle. Prompt the user to enter radius and display the output “The area of circle is XX for radius XX” and display “Sorry XX is not a valid radius” if the radius is not positive value If-elseif-else-end: • If statements may be used in side of other if statements, this is useful when there are three desired possible codes as outcomes • Several else and elseif statements may nested Example of an if-elseif-else-end Statement: • Write a MATLAB script file that determines the real roots of a quadratic equation ax 2 + bx + c = 0. If the discriminate (D) > 0 the program needs to display “the equation has two real roots”, if D=0, display “the equation has one real root’” if D<0 display, “The equation has no real roots”, have the values of the roots display after the message. MATLAB Syntax %Quadratic Equation %Obtain User Input a=input('Enter the constant a '); b=input('Enter the constant b '); c=input('Enter the constant c '); %Calculate the discriminate D=b^2-4*a*c; %Apply if-elseif-else logic if D<0 disp('The equation has no real roots.') elseif D==0 x=-b/(2*a); fprintf('The equation has one root: x = %f\n',x) else x1=(-b+sqrt(D))/(2*a); x2=(-b-sqrt(D))/(2*a); fprintf('The equation has two roots:\n') fprintf('x1 = %f, x2 = %f\n',x1, x2) end Example of an if-elseif-else-end Statement • Apply to script file generated previously to the following quadratic equations 2x2 + 8x + 8 = 0 -5x2 + 3x-4 = 0 -2x2 + 7x + 4 = 0 Nested if-else statements • • • Nesting means one statement inside another. Consider the following expressions • Y = 1 if x<-1 • Y = x^2 if -1<=x<=2 • Y = 4 if x>2 if x<-1 • • Y = 1; else • % If we are here, x must be >=-1 • % Use if-else statement to choose between remaining two conditions • if x<=2 • • • • end Y = x^2; else • % No need to check because if we are here x >2 • Y = 4; end Flowchart – Nested IF- Else Statements Example of an Nested ifelse Statement: • • Consider the following expressions • Y = 1 if x<-1 • Y = x^2 if -1<=x<=2 • Y = 4 if x>2 if x<-1 • • else • • • • Y = 1; end if x<=2 • % If we are here, x must be >=-1 • Y = x^2; • % No need to check because if we are here x >2 • Y = 4; else end The switch-case Statement: • Another statement that can be used to direct the flow of a program • The switch statement is used in place of if-else clause when an expression is tested to see whether it is equal to one of several possible values. • Provides a means for choosing one group of commands for execution out of several possible groups • The first line of the statement is the switch command • MATLAB Syntax: switch switch expression • The switch expression is either a scalar, a string, or a math expression with previously assigned variables • After the switch command are one or several case commands. Each has a value next to it, that value can be a scalar or a string, and an associated group of commands below it. • After the case command there can be an otherwise command followed by a group of commands • The statement must end with an end statement The switch-case Statement: • If there is more than one match, only the first matching case is executed • If no cases match then either the end or otherwise command is executed • A case may have more than one values, use the format {value1, value2, value3,….} The case is executed if at least one of the values matches the values of the switch expression • The switch expression is compared in sequence to the values of case expressions (value1, value2, value3) Example for Switch Statement • Choice = input(‘Enter a 1, 3 or 5’); • switch Choice • case 1 • disp(‘It’’s a one!!’); • case 3 • disp(‘It’’s a three!!’); • case 5 • disp(‘It’’s a five!!’); • otherwise • disp(‘Other than the given number’); • end Example of the switch-case Statement: • • • • • Write a script to change the grade to letter grade. switchlettergrade.m % switchlettergrade displays the letter grade corresponding to score of quiz. Marks = input(‘Enter value between 0 and 10: ’); switch Marks • case 10 • • case 9 • • Grade = ‘D’; otherwise • • • Grade = ‘C’; case 7 • • Grade = ‘B’; case 8 • • Grade = ‘A’; end disp(Grade); Grade = ‘F’; Example of the switch-case Statement: • • • Write a script to change the grade to letter grade. • • Marks = input(‘Enter value between 0 and 10: ’); switchlettergrade.m % switchlettergrade displays the letter grade corresponding to score of quiz. Display A for marks 9-10, B for marks 7-8, C for 6, D % for 5 and F otherwise. switch Marks • case {10, 9} • • case {8,7} • • Grade = ‘D’; otherwise • • • Grade = ‘C’; case 5 • • Grade = ‘B’; case 6 • • Grade = ‘A’; end disp(Grade); Grade = ‘F’; Example of the switch-case Statement: • Write a MATLAB script file to calculate the training heart rate (THR) for men and women based on the following formulas, based on AGE, resting heart rate (RHR), fitness level (INTEN). The program needs to ask users if their gender, age, resting heart rate, and fitness level (low, medium, or high). The program then displays the THR. Note: the INTEN values are 0.55 for low, 0.65 for medium, and 0.8 for high Men: THR =[(220-AGE)-RHR]*INTEN+RHR Women: THR = [(206-0.88*AGE)-RHR]*INTEN+RHR • Once the program is complete determine your training heart rate MATLAB Syntax gender=input('Please enter your gender (male, or female)','s'); AGE=input('Please enter your age (a number) '); RHR=input('Please enter your rest heart rate (a number) '); FitLevel=input('Please enter your fitness level (low, medium, or high) ','s'); switch gender case 'male’ factor=(220-AGE)-RHR; case 'female’ factor=(206-0.88*AGE)-RHR; end switch FitLevel case 'low’ INTEN=0.55; case 'medium’ INTEN=0.65; case 'high’ INTEN=0.8; otherwise disp('ERROR: Fitness level was entered incorrectly.') end THR=factor*INTEN+RHR; fprintf('Your training heart rate is %3.0f\n',THR) Menu Command • Generates a graphical menu of choices for user inputs. • K = menu(‘title’, ‘option1’, ‘option2’, ‘option3’); • If the user’s terminal provides graphics capability, menu displays the menu items as push buttons. • As of version R2015b, an alternative to ‘menu’ function ‘listdlg’ is being used. Menu Command • If the user terminal doesn’t provide graphical capability, they will be given as an numbered list in the command window. Example for Menu Command • choice = menu('Choose a value','1','3','5'); • switch choice • case 1 • disp('It''s a one!!'); • case 3 • disp('It''s a three!!'); • case 5 • disp('It''s a five!!'); • end The IS function in MATLAB • There are lot of functions built into matlab to test whether or not true. • These functions have name that begin with “is”. • isequal – True if arrays are numerically equal • isletter - True for letters of alphabets • isempty – True for empty array • Other functions: ischar, islogical, isfloat, isinteger etc. Inclass Activity • Write a script file that generates a random number between 1 and 100. Using conditional statement display the number is even if it is divisible by 2 or display the number is odd. • Write a script that calculates the area of circle. Prompt for user to input radius in inches. Generate a menu with options inches, feet, yards, meters, centimeters. Use switch statement to display the result in unites selected. b Engineering 102 Project two – Encryption with photos Secure communication is millennia old, and new methods and cyphers are always being developed. As increasing communication is done over the internet, 21st century engineers will need to confront secure communication like never before. There are many methods of encrypting and decrypting messages, and this project will explore a few of them, including encrypting messages and hiding them in plain sight in an image. You will be assigned teams of 4. Each team member will be responsible for analyzing some data, with comprehensive analysis by the whole group. Team members who do not contribute substantially in their groups can be replaced, and team members who are in poorly performing groups may move to other groups. Four cyphers will be used, one for each member. The team must determine which team member will do which one. For teams with fewer than four members, they need only to do one per team member. So teams of three need only do three of the cyphers. The cyphers are all lower case and contain only letters, numbers and a space. For each, the letters correspond to numbers starting a 1 and going to 217 in increments of 6 (same length of vector as the letters). Four image files are provided, one for each team member. Each member should use a different image, but there is not a requirement to use any particular image with any specific cypher. Any image can be used with any cypher, but each member of the team should have his or her own cypher and image unique from the other members on the team (DO NOT CHANGE THE NAME OF IMAGE FILE). Cypher A (Alphabet broken in half, count down M-A, then Z-N, “space”, then numbers 0 through 9) ‘M, L, K, J, I, H, G, F, E, D, C, B, A, Z, Y, X, W, V, U, T, S, R, Q, P, O, N, “space”, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9’ Cypher B (Vowels at the beginning, consonants (all alphabets should be Uppercase), “space”, numbers 0 through 9) ‘A,E,I,O,U,B,C,D,F,………..Y,Z, “space” ,0,1,2, … 9 ’ Cypher C (Numbers 0 through 9, “space”, vowels in reverse, then all consonants in reverse (all alphabets should be Uppercase)) ‘0,1,2, … 9 ,“space”,U,O,I,E,A,Z,Y,X,…….D,C,B’ Cypher D (Vowels at the end after Z (all alphabets should be in Uppercase), “space”, numbers 0 through 9) ‘B,C,D,F,……….X,Y,Z,A,E,I,O,U, “space”,0,1,2, … 9 ’ Important programming information: • • • • • • You must use the techniques taught in the course to solve this project. Perform the following analysis in a single MATLAB script file (one per team member) named “Project_2_Firstname_Lastname.m “. Each member must write his or her own code, and team member’s codes may not be identical. The team must work together to divide the work and to complete the final analysis. In all cases except where noted MATLAB must do the computations and analysis and generate the output. Computed values for outputs must be used, values may not be hard coded into the code unless noted. Use comment statements to separate parts of the code and suppress all output to the command window except the required output. • The code must be capable of handling any message of any length, not just the one you use as an example. Individual portion: 1. Create a message to be encrypted. The minimum length of the message is 2500 characters, and the program should be capable of handling a message of any length up to that number. It must check the length of the message and inform the user if it’s too long. Generate your selected encryption cypher and convert the message to the numerical equivalent. This must be done using loops. 2. Reshape the vector into a message matrix 50 x 50 using reshape command. 3. Display in the command window which of the cyphers you are using. Display the message to be encrypted and its numerical equivalent using display statements. It should read: Original Message Your Message Numerical Message Numerical message Encoded Matrix Encoded Numerical Matrix 4. Import your image using imread(‘ '). Display the image in a figure window using imshow(‘ ‘) and title it “Original Image” 5. Define the position of row and column to insert the encoded numerical matrix. (Remember that the position cannot be more than the value of sum of size of image and the size of Encoded Matrix) 6. Embed the encoded message starting from the positions defined i.e., using loops, replace the pixels (numbers) of image with the encoded numerical matrix. (For example, the encoded numerical message is of size 50 x 50 and the positions to embed the message are row = 100 and col = 100. So the program should be capable of replacing all values of images from position (101,101) to (100+50,100+50)) 7. Display the image in a new figure window and title it “Encoded Image” 8. Extract the encoded message from the image using the reverse of the process in step 5. 9. Change the numbers back to string values and display the string message Decoded Message Should be same as Your Numerical message Decrypted Message Should be same as Your Original Message Note: Output should consist of only the required lines in the command window and four figures. ...
Student has agreed that all tutoring, explanations, and answers provided by the tutor will be used to help in the learning process and in accordance with Studypool's honor code & terms of service.

This question has not been answered.

Create a free account to get help with this and any other question!

Similar Questions
Related Tags