CS310 Team Lab #9
Conditional Execution and File Input
Unit 3: Programming in MATLAB

PROBLEMS

Problem 1: Conditional expressions and if statements

Conditional expressions are expressions that evaluate to true or false. We use conditional expressions in if (and while) statements to allow our code to decide which piece of code to execute next.

Note: the code you create in this problem will be re-used in later problems in this team lab.

  1. Create a new script. Then add the following code to your script:
    clear
    N = input('Enter a number: ');
    
    1. Write an if statement that displays 'ok-scalar' if the value N entered by the user is a scalar value (i.e., a single value).
    2. Write a second if statement that displays 'ok-positive' if the value N entered by the user is positive.
    3. Write a third if statement that displays 'ok-integer' if the value N entered by the user is an integer.(Hint: there are several possible ways to do this; one way is to use the floor function.)
    4. Finally, write a fourth if statement that displays 'ok-all 3' if the value N entered by the user is a single positive integer value. You should just write one if statement whose condition checks all three conditions from parts 1 - 3. (Hint: use boolean operators to combine conditions.)

    For each of the four if statements you write, run the script several times. Does it work correctly? Make sure you test each statement with values for N that result in it not displaying 'ok' as well as those that do.

  2. Add code to your script that implements the following algorithm:

    Set a variable N to 100
    Get a random value between 1 and N
    Ask the user to guess the value
    If the user's guess is correct
        display 'correct'
    Else
        display 'incorrect'

    Save your script and run it several times to see if it works as expected. You may want to display the random value you generate so you can more easily test a correct guess as well as an incorrect guess.

    You may wish to temporarily disable the code you wrote for part a. There's an easy way to do this in MATLAB's editor: select the code you want to disable, right-click on the highlighted code, and then choose Comment from the menu that pops up. When you want to enable this code again, select it, right-click, and choose Uncomment.

  3. Add the following code to your script: (you may wish to disable your code from part b)
    a = [ 4 2 8 1 ];
    b = [ 3 2 8 9 ];
    c = (a == b)

    What do you think the value of c will be? Run your program and find out. Do you understand why c has the value it does? Does the == operator work in an element-wise fashion on vectors or not? Now add the following if statement:

    if c
        disp('inside if')
    end

    What happens when you run your script? Why? What value does b have to have in order for 'inside if' to be displayed? Experiment with different values of b until you figure it out.

Problem 2: A guessing game

  1. Copy your code from problem 1b to a new script. Then add the following after your code:

    % code from problem 1b that defines N and gets a random value goes here
    
    goAgain = true;
    while (goAgain)
    
        % code from problem 1b that asks the user for input 
        % and displays a response goes here
    
    end

    and move your code from problem 1 part b to the places indicated.

  2. Add the statement goAgain = false; (at the appropriate place) so that this statement will get executed when the user has guessed correctly.

  3. As you add and modify code, it is common for the indentation to become messed up. The indentation makes it easy to see at a glance which statements are part of the body of an if, which are part of the body of an else, etc. You can have MATLAB fix the indentation by selecting all your code, right-clicking on the highlighted code, and choosing Smart Indent from the menu that pops up. (Note: you should always make sure that code you turn in for an assignment is indented appropriately.)

  4. Run your script. Make sure you understand how it is working. We use a while loop because we don't know how many times the user will need to guess until they are correct. A loop like the one we have is called a sentinel-controlled loop because it ends when a sentinel value is or is not detected.

  5. Just telling the user whether or not they are correct can make for a very long game. Let's give the user some help. Modify your if statement so that it displays 'correct' if the user's guess is correct, 'too high' if the user's guess is larger than the correct answer, and 'too low' if the user's guess is smaller than the correct answer. This will involve using elseif. Don't forget to test your code to see if is working correctly!

  6. Add code to count the number of guesses it takes before the user guesses correctly. The total number of guesses required should be displayed at the end of the game.

  7. Add code to let the user enter the value of N. Your code should also check that for valid input (i.e., N should be a single positive integer). It should keep asking the user for value of N until the user enters a valid value.

Show your guessing game program to a lab leader and demonstrate that it works correctly.

Problem 3: Using data that is stored in a file.

Often it will be easier to get the data we need from a file instead of asking the user to enter the data. For this problem, you will read in a file of scantron exam results and a file containing the answer key and calculate the scores based on the number of questions answered correctly and the point values of the questions.

Copy these two files into your current MATLAB directory to get started. Both of the files are text files; the .csv extension indicates that the file contains comma separated values.

Note: You can also create your own data (e.g., .txt) files using the MATLAB text editor and the same data file format as our examples demonstrate. When you save txt files, type the .txt extension (or MATLAB will save it with a .m extension and you may be tempted to run it as a program).

Each row of the scantron results corresponds to one student. The exam was a multiple choice exam (where the A - E answers have been converted to numeric values 1 - 5).

  1. Create a new script. Add code to load the scantron results into a matrix named answers and the key into a vector named key. How many students are there? How many questions were there on the exam?

  2. Write a for loop that goes through the first 5 students and displays the number of correct answers for each student. Note: you do not need to use any ifs or additional loops to do this. (Hints: what happens if you check if the entire vector of student answers is equal to the key? Also, don't forget MATLAB has the built-in sum command.) You should get 50, 42, 36, 50, 44.

  3. For this exam, questions 1 - 24 were worth 3 points each and questions 25 - 55 were worth 1 point each. Modify the body of your for loop so it calculates the score for each student (instead of just the number of correct answers). You should get 94, 78, 70, 90, 80.

  4. Using the weighting indicated in part c, if a student gets all the questions correct, the score will be 103. However, for this exam, the maximum score possible is 100. Add code to the body of your for loop to cap the score at 100.

  5. Modify your program so it computes the scores for all the students (instead of just the first 5). After computing the scores, it should print the mean, minimum, and maximum score for the exam.

ADDITIONAL PROBLEMS or FURTHER READING

Problem A1

Modify your script from problem 3 so it can be used with results from exams with different numbers of questions, different maximum scores, and that may or may not include a score from a written section. Do this by defining variables at the top of your script for values that may be different for different exams such as number of 3-point multiple choice questions, number of 1-point multiple choice questions, maximum score, whether there is a written question, etc. Then modify your code from problem 3 so that it uses those variables. You can assume that exam results always have the format: 3-point questions, then 1-points questions, then the score from the written section (if there is a written section).

Here are two more exams you can use to test your script:

Problem A2

Modify your script from Problem 3 so it computes and displays a histogram of the exam scores. The histogram should have one bar for scores of 50 and lower, one bar for scores of 51-55, one bar for scores of 56-60, etc. You can create a histogram as follows:

Problem A3

Modify your script from Problem 2 so that it: