0% found this document useful (0 votes)
4 views23 pages

COMP1103 Week 5 (1)

The document provides exercises for an introductory programming course, focusing on concepts such as pH measurement, Reynolds number, and the use of loops in MATLAB. It covers both 'for' and 'while' loops, including examples and exercises for calculating sums, products, and averages. Additionally, it includes tasks for generating random numbers and determining prime numbers, emphasizing practical programming skills.

Uploaded by

efeuygar2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views23 pages

COMP1103 Week 5 (1)

The document provides exercises for an introductory programming course, focusing on concepts such as pH measurement, Reynolds number, and the use of loops in MATLAB. It covers both 'for' and 'while' loops, including examples and exercises for calculating sums, products, and averages. Additionally, it includes tasks for generating random numbers and determining prime numbers, emphasizing practical programming skills.

Uploaded by

efeuygar2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

COMP1103

INTRODUCTION TO
PROGRAMMING
Spring 2025 – Week 5
Tuğba Erkoç
Exercise - 8
◦ In chemistry, the pH of an aqueous solution is a measure of its acidity.
The pH scale ranges from 0 to 14, inclusive. A solution with a pH of 7 is
said to be neutral, a solution with a pH greater than 7 is basic, and a
solution with a pH less than 7 is acidic.
◦ Write a script that will prompt the user for the pH of a solution, and
will print whether it is neutral, basic, or acidic. If the user enters an
invalid pH, an error message will be printed.
Exercise - 9
◦ In fluid dynamics, the Reynolds number Re is a dimensionless number used to
determine the nature of a fluid flow. For an internal flow (e.g., water flow
through a pipe), the flow can be categorized as follows:

◦ Write a script that will prompt the user for the Reynolds number of a flow and
will print the region the flow is in. Would it be a good idea to write the
selection statements using switch? Why or why not?
Loops
◦ Suppose that we need to display the same message on the screen 100
times.
◦ Would it be easy to repeat the same code 1000000 times?
◦ Example:
◦ disp(“There is an error!”);
◦ disp(“There is an error!”);
◦ disp(“There is an error!”);
◦…
◦ disp(“There is an error!”);
Loops
◦ We cannot repeat typing the same code for 100, 1000 or million times. It is
not feasible/practical.
◦ There is a solution to avoid re-typing the same codes to execute them
repeatedly.
◦ We call the structure that allows us to repeat the same code multiple times
as loop.
Loop Types
◦ There are two loop structures:
◦ for loop → counted loops
◦ Repeats statements a specified number of times.
◦ We know how many times the statements are to be repeated.
◦ Example: Repeat the same calculation 100 times

◦ while loop → conditional loops


◦ Repeats statements until a condition becomes false.
◦ We do not know how many times the statements are to be repeated.
For Loops
◦ The general form of the for loop is:
for loopVariable = range
action
end
◦ loopVariable: The variable that is used to iterate through values is called a loop variable,
or an iterator variable
◦ range: Range of values through which the loop variable is to iterate. We use colon operator
to specify the range. Range is written in the form start:steps:end or start:end
◦ Example:
◦ for count = 1:5 → increments count by 1
◦ Example:
◦ for count = 1:2:10 → increments count by 2
For Loops - Simple Example
◦ Write a MATLAB program that calculates and prints the area of a
circle for radius values from 1 to 3 cm.
For Loops - Simple Example with Steps
◦ Write a MATLAB program that calculates and prints the area of a
circle for radius values from 1 to 3 cm in steps of 0.5.
For Loops - Example with Steps 2
◦ Write a MATLAB program to print this column of integers by using loop:
0
50
100
150
200
For Loops – Running sum
◦ Write a program that takes 5 numbers from the user and calculates the
sum of these numbers. Display the sum.
For Loops – Running product
◦ Write a program that takes 5 numbers from the user and calculates the
product of these numbers. Display the product.
While Loops
◦ The while loop is the conditional loop in MATLAB.
◦ While loop is used to repeat an action when we do not know how many times the
action will be repeated.
◦ The general form of the while statement is:
while condition
action
end

◦ The action is executed as long as the condition is true.


While Loops – How it works?
◦ The condition is evaluated. If the condition is logically
true, the action is executed.
◦ Then, the condition is evaluated again. If it is still true,
the action is executed again.
◦ We will repeat the same condition check and action
execution until the condition becomes logically false.
◦ The condition must eventually become false to avoid an
infinite loop.
◦ If this happens, pressing Ctrl+C will exit the loop.
While Loops –Factorial Example
◦ Write a MATLAB program that takes a number from the user. Find the first factorial
that has a greater value than the number entered by the user.
◦ Factorial number is shown like n!
◦ Example: 5! = 1 * 2 * 3 * 4* 5 = 120
Condition
While Loops – Running Sum Example
◦ Write a MATLAB program that calculates the running sum of positive numbers
entered by the user. The program must continue taking a new number from the user
until the user enters a negative value. Display the sum after user enters a negative
value.
While Loops – Average Example
◦ Write a MATLAB program that calculates the average of positive numbers entered by
the user. The program must continue taking a new number from the user until the
user enters a negative value. Display the average after user enters a negative value.
Break
◦ break terminates the execution of a for or while loop.
◦ Statements in the loop after the break statement do not execute.
Break – Prime Number Example
◦ An integer greater than 1 is prime if its only positive divisor is 1 or itself. For example,
2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not.
◦ Write a MATLAB program that takes a positive number from the user and checks this
number to determine whether it is a prime number or not. Display the decision.
Continue
◦ continue passes control to the next iteration of a for or while loop.
◦ It skips any remaining statements in the body of the loop for the current iteration.
The program continues execution from the next iteration.
Continue – Multiple of 7
◦ Write a MATLAB program that calculates the sum of only the multiples of 7 in the
range 1 to 50. However, you can only use while loop.
Exercise - 10
◦ Write a MATLAB program that randomly generates a number in
[0,100] interval. The user will try to guess this number. The
program prompts the user to enter a number continuously until the
number matches the randomly generated number. For each user
input, the program tells the user whether the input is too low or too
high, so the user can choose the next input intelligently.
Exercise - 11
◦ Write a MATLAB program that asks the user to enter two positive
numbers. Then calculate the least common multiple of these two
numbers. The least common multiple is defined as the smallest
multiple that two or more numbers have in common.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy