Lec 3 Flashcards
Arrays with Loops
Write a program that will:
o Use a loop to prompt the user to enter 3 numbers
o Each number will be stored as an element of an array variable (you are building a list using a loop)
o Use a second, separate loop to find the largest value in the list stored in your array AND determine the
total of all the numbers stored in the array
o Display the results to the user
Declare Real list[3],max, total
Declare Integer Index
For Index 0 to 2
Display “Enter #”
Input list [Index]
End For
Set Index=0
Set totoal=0
Set max=list[0]
While Index<3
Set total=total+list[index]
If list [Index]>max
Set max= list [Index]
End If
Set= index=index+1 End While Display the max is", max Display"The total is", total
Two type of data types
Characters-any single letter of the alphebet, digit or special characters entered by a key word like “Z”“A””@”
String-sequences of characters treated as a unit
like “whole email” or “abc123” “Zippy Roo”
-Be careful not all programming languages use both data types some us only use string data type for all text info
-distinguisg inn code by quotation marks
-some use double quotes and other use single quotes(matlab)
What wrong:
Declare Real num1, num2
Declare Real total
Set num1 = 10
Set num2 = “15”
Set total = num1+num2
Display “The total is”, total
LIne 4 is set as a string instead of a number so it can be calculauted
String function:length( string)
The length()Function will return the number of character in a string
Pseudocode
Declare String str1, Integer len
Set str1 = “Test”
Set len = length(str1)
Display str1, “has” len “characters”
Output
Test has 4 Characters
append(string1, string2
The append()function append/ add str2 to the ned of str1(order matter)
Pseudocode
Declare String str1, str2, new
Set str1 = “Go”
Set str2 = “Zips!”
Set new = append(str1,str2)
Display new
Ouput
GoZips!
Ouput
contains(string1, string2)
The contains() function will a 1 if str2 contains is found contains(string1, string2) inside of strl1;0 if not
Pseudocode
Declare String str1, str2, str3
Set str1 = “Antarctica”
Set str2 = “tar”
Set str3 = “ant”
Set result = contains(str1, str2)
Display result (1)
Set result = contains(str1, str3)
Display result (0)
Output
-order matter
-case sensitive
Character-by-character Text Processing
Some common programming tasks require the programmer to access and/or manipulate indivudals characters within a string
We can achieve this by treating strings as an array characters, where the first letter in the string is at element 0, the
second letter in the string is at element 1, and so on
Program Pseudocode Program Output
Declare name
Set name = “Jacob”
Display name[0]
Display name[1]
Display name[2]
Display name[3]
Display name[4
Example (using a loop!) with name Jacab
Code
Declare String Name
Set Integer Index
Set Name=Jacob
For index=0 to length(name)-1
Display “char”,index+1, “is”, name[index]
End For
Program Output
Char 1 is J
Char 2 is A
Char 3 is C
Char 4 is O
Char 5 is B
Manipulating Text Example:
Write a program that:
* Prompts the user to enter a short phrase
* Use a loop to go through all the characters within the string
* Use a decision structure to test whether the character is the letter “d” or “D”
* If it is a match, replace it with the letter “B”
Code
Declare
Display “Enter a phrase”
Input phrase
For index =0 to length(phrase) -1
If Phrase[index]==”a” or Phrase[index]== “D”
set Phrase[index]==”B”
End If End For Display "I think you meant" Display "phrase"
Output
Enter a short phrase
Go Ducks! [Enter]
I think you meant
Go Bucks!
isLetter(character)
isDigit(charcter)
The isLetter() function will return 1 if the chcarcter is upper case or lower case letter and othwise
The isDigit() function will return a 1 if one of the characters is the digit 0-9;0 otherwise
Pseudocode
Declare String str1
Set str1 = “Test123”
Display isLetter(str1[4]) (0)
Display isDigit(str1[4]) (1)
isUpper(character)
isLower(charcter)
The isUpper() function will return on if character is uppercase otherise 0
The isLower() function will return 1 if characters is lowercase otherwise 0
Pseudocode Output(first letter)
Declare String str1
Set str1 = “Test123”
Display isUpper(str1[0]) (1)
Display isLower(str1[0]) (0)