So the problem this time around deals with charge account validation. Basically, we have to design a program in pseudocode where a user enters an account number, and the program validates it by comparing it to a list we are to put in an array. The numbers should be stored in the array, and we are required to use the sequential search algorithm to locate the number entered by the user. If the number is in the array, the program displays a message indicating it is valid. If not, it displays, invalid. There is a requirement of the main module, and the function isTicketValid. I was wondering if my pseudocode was proper. Thanks!
Constant Integer SIZE = 18
Declare String account[SIZE] = "5658845", "4520125", "7895122", 8777541", "8451277", "1302850", "8080152", "4562555", "5552012", "5050552", "7825877", "1250255", "1005231", "6545231", "3852085", "7576651", "7881200", "4581002"
Module main()
Display "Please enter your charge account number"
Input account
isValidAccount(account)
Main()
Function Integer isValidAccount(account)
Declare Boolean found
Declare Integer index
Set found = False
Set index = 0
While found == False AND index <= SIZE -1
If account[index] == account Then
Set found = True
Else
Set index = index + 1
End if
End While
If found Then
Display "Your account is valid ", index + 1
Else
Display "Your account is not valid."
End If
End Function
Related
I am trying to add the user inputs using a while loop and If statements. I am having trouble figuring out how to add all the userNumbers to each other. Any help would be appreciated.
//variables
Declare Integer userIn = 0
Declare Integer total = 0
//read numbers and calculate
While decision == decY
Display “Please enter your numbers: ”
Input decision
If UserIn > 0
Display userNumbers
Set total = userIn + 1
Display “Would you like to enter another number Y/N?”
Input decision
If decision == decN
Display “Done reading numbers, your total is ”, total
End If
End If
End While
Decide on a separator for the input, unless they're only allowed to enter a single number at a time in which case you can skip to 3.
Use string splitting to cut the input up and then loop through that list with for, while, do, until, or etc.
Create a sum total variable and add each input value to it, e.g. sum = sum + split_input[index], or if it will only allow a single input at a time sum = sum + input.
Some Notes:
Adding a value to a variable can be shortened to variable += value to add a value to an existing variable and assign the result to the variable, but not all languages support this syntax.
Not all programming languages start at 0 for list indices, so be sure to change the starting index accordingly.
In the program below r is initialized as a rounded random number and the user is prompted to guess. If the user guesses correctly there is one prompt Else there is another prompt. The code looks right but when ran the Else case is chosen whether the user got the number right or not. Please help I have read over it so many times that it hurts.
Set WshShell = WScript.CreateObject("WScript.Shell")
'This script is designed to create a random number, and have a user guess the number
'If the user guesses the number incorrectly, negatively impact the users system
'if the user guess the number correctly, do nothing
dim InputValue
dim UserInput
'create a random number
dim r
randomize
r = int(rnd * 10)+1
'Allow the user to guess what number is correct
Input = inputbox ("Choose a number from one to ten, and choose wisely ;)", "Type a number.")
InputValue = Input
'Provide the user with feedback as to what his/her choice was and what the number actually was.
UserInput = "You typed " & InputValue & "." & " The number was " & r & "."
'echo the random generated number for test purposes
WScript.Echo r
Select Case InputValue
Case r
WScript.Echo "No action"
Case Else
WScript.Echo "Negative Action"
End Select
The issue is that InputValue is a String type because that's what's returned by the InputBox function and r is an Integer, so when you compare InputValue to r, you're comparing a string to an integer, i.e. apples to oranges.
Proof of concept:
Dim x
x = "5"
Dim y
y = 5
Select Case x
Case y
MsgBox "Yes"
Case Else
MsgBox "No" 'this is what happens.
End select
VBScript is not a strongly-typed language, i.e. it does not know about types really at all -- well, it kind of knows. It treats every variable as one type called a "variant" which itself can then have an affinity to a more specific type but in a very confusing and error-prone way. I'd say you should read this to learn more about it.
You can get into a lot of trouble with this unless you always make sure you know what "types" you're working with.
Try changing it to this:
InputValue = CInt(Input)
Now you're converting the "string" into an "integer" (the variables are both still actually "variants" but they have the same underlying affinity to integer). However, you want to be careful because someone might input a value that is not able to be converted to an integer, e.g. hello, and thus you'll get an error on this line. You will need to do some error checking first before converting it.
Beginners Visual Basic Help: I am to create a simple "game" which prompts a user to enter a number 1-10. That number must be stored. Then the real game begins, Using a loop have the user try to guess the number stored. Notify if the guess is too high, too low, or correct. Continue loop until correct.
I am very stuck right now; all I have is-
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim mynumber, input As Integer
mynumber = Val(TextBox1.Text)
input = TextBox1.Text
Please Help I Know this is very simple but this class is very complicated for me.
So, to start off I'm going to use your variables and we're going to declare these so the whole of the form can access them.
Dim mynumber As Integer
Dim input As Integer
We're also going to declare a seperate variable which will count the amount of times that the user has tried to guess the number, and also the number that they are going to be guessing which in this case is declared as guessnumber
Dim counter As Integer
Dim guessnumber As Integer
You want to create a button and call it btnNewGame which will start the game playing and this is going to be the code that you'll add to that button:
Dim mynumber As Random
guessnumber = Number.Next(10) + 1
This will create the random number and have it change to a new value when the new game button is clicked, so no game is the same.
We want to initialize the counter now so that whenever a new game is started then the amount of guesses that the user has had is 0 and we also want to initialize the textbox so that it is empty when the new game button is clicked.
counter = 0
txtInput.Text = ""
So now, underneath a separate play button we want to add this code
Dim uni As Integer
uni = CInt(txtInput.Text)
counter = counter + 1
Now lets add in a 'Try' function which will run through until the user has either guessed the number or exceeded the amount of turns which is 3. You can easily alter the amount of turns.
Try
If uni = guessnumber Then
MessageBox.Show("You guessed my Number!")
ElseIf uni < guessnumber Then
MessageBox.Show("Your guess is too low")
ElseIf uni > guessnumber Then
MessageBox.Show("Your guess is too high.")
End If
Catch E1 As InvalidCastException
MessageBox.Show("Please enter a number and try again.")
End Try
Hope this helps you, please let me know how you get on!
I am trying my hand at Applescript and can't see anything wrong with this.
The Error I get is
error "Can’t get end of {button returned:\"OK\", text returned:\"3\"}." number -1728 from last insertion point of {button returned:"OK", text returned:"3"}
This is my code:
beep
set counter to 0
set tempX to 0
set temp to 0
set counting to 0
set stored to {0}
set input to "How many grades do you wish to enter?" as string
set str to display dialog input buttons {"NEXT"} default button "NEXT" default answer ""
repeat text returned of str times
counting = counting + 1
set grades to display dialog "GRADES: " default answer ""
set stored to grades
end repeat
set rep to the length of stored
repeat rep times
counter = counter + 1
set tempX to the ((end of stored) - counter) as number
set temp to temp + tempX
end repeat
set ln to the length of grades
set average to temp / ln
if text returned of str is 1 then
say "The Average of your grade is " & average using "Zarvox"
else
say "The Average of your grades is " & average using "Zarvox"
end if
get "AVERAGE: " & average
So, before I begin: I'd strongly recommend that you teach yourself how to use the Javascript interface to Apple Events, rather than the Applescript language itself. Applescript is a really weird language, and its quirks are largely unique; learning it is going to be frustrating, and won't help you learn other languages.
That being said, let's dive into your code:
set stored to {0}
This would start you out with one grade that's always present and set to zero. You probably want to just initialize this to an empty list:
set stored to {}
Next:
set grades to display dialog "GRADES: " default answer ""
This sets grades to a result object, not just the answer. What you probably want here is actually the text returned of the result:
set grades to text returned of (display dialog "GRADES: " default answer "")
(This is what's creating the really weird-looking object in your error message.)
Next, you overwrite stored with this result object:
set stored to grades
What you probably want here is to insert this element into the list. Because Applescript is a strange and obnoxious language, this is somewhat more cumbersome than you're thinking:
set stored to stored & {grades}
Finally, there's some logical issues with your averaging; you're adding the end of stored (that is, the last grade input) to the temp variable each time. A much simpler approach would be:
set temp to 0
repeat with n in stored
set temp to temp + n
end repeat
set average to sum / (count of stored)
With these changes all made, your script should work correctly.
Very new to Corona and trying to make a simple memory game. I want to flash about 5 random sprites from a spritesheet (containing about 20 sprites) and then ask the user to input what they thought flash up while showing the sheet. Hope I've explained that OK? I've done a little bit of JavaScript before but would not be confident in coding. Trying to do something challenging - may have to do something simpler. Was told Corona was a good place to start :(
Would math.random be the correct way to go about it?
Any ideas I would be grateful. At the moment I can only display the whole sheet not sure how to make the random sprites appear.
Here's some more in-depth example, that should help you out with building that system up (the previous answer has some a mistake)
local cards = {{name="one",count=0,flipped=false,matched=false},{name="two",count=0,flipped=false,matched=false},{name="three",count=0,flipped=false,matched=false},{name="four",count=0,flipped=false,matched=false},{name="five",count=0,flipped=false,matched=false},{name="six",count=0,flipped=false,matched=false}};
function getAvailableCards(Count)
local suitable_cards = (Count and {} or nil); -- create a table if you want to get keys of a valid cards in the cards table
local empty = true;
for key,data in pairs(cards) do
if (data.count < 2)then
if (Count) then -- add key if you want an output (table with valid keys)
table.insert(suitable_cards,key);
-- table.insert(suitable_cards,key); you can insert it again for higher range of numbers to select from, making math.random a bit more "random".
empty = false;
else
return true;
end
end
end
if (empty) then
return false;
end
return suitable_cards;
end
function getRandomCard()
local valid_cards = getAvailableCards(true); -- get a table with all valid keys of the cards table, to pick a random key
if (not valid_cards) then
return valid_cards; -- return false
end
local index = valid_cards[math.random(1,#valid_cards)]; -- select random key
cards[index].count = cards[index].count + 1; -- increase the count of key generation
return cards[index].name,index -- return card id and index from the cards table
end
function setupCards()
while getAvailableCards() do
local card_id,index = getRandomCard(); -- gets a random card id and it's index
-- setup the card in GUI and such,
-- like create a board and then create an image and load the image (based on card id)
-- As an example (no such functions, but you should create them or find functions similiar to them)
-- card = getBoard().NewImage();
-- card.id = index;
-- card.loadImage("hidden_card.png");
end
end
function TryToMatch()
local child1,child2 -- child tables that will be copied from the cards table, for later comparing
-- get all flipped but not matched cards
for key,child in pairs(cards) do
if (child.flipped and not child.matched) then
if (child1) then
child2 = key; -- get key
else
child1 = key; -- get key
end
end
end
if ( child1 and child2) then -- two revealed cards can be checked
if (child1.name == child2.name) then -- reveal
cards[child1].matched,cards[child2].matched = true,true;
TryToEnd();
else
--un-reveal it
end
end
end
function TryToEnd()
local matched = 0;
for key,child in pairs(cards) do
if (child.flipped and child.matched) then
matched = matched + 1;
end
end
if (#cards == matched) then
-- display that the game is finished
end
end
function Click(object)
-- When an object is clicked make it call this function with the object
if (object and object.id and cards[object.id] and not (cards[object.id].flipped and cards[object.id].matched)) then
cards[object.id].flipped = true;
-- You should load here the card image (actual card image, like you revealed the card)
-- Example
-- object.loadImage(cards[object.id].name.. ".png");
TryToMatch();
return true
end
return false
end
EDIT:
I did not test this script nor use Corona, it may have some bugs, in theory it should work as it is.
A common technique used in games to get a random object from a collection is to use a random generated number like math.random and then use the modulo operator % to get a number from 0 to number of objects.
For example lets say you have a list of cards:
local cards = {"one","two","three","four","five"};
local numCards = #cards;
If you want to randomly select a card you can do the following:
local function getRandomCard()
#This will give you a random number from 0 to 100
math.randomseed(os.time())
local randomNumber = math.random(100);
#This will give a random number from 0 to 4.
local randomIndex = randomNumber % numCards;
# Lua indices start at 1, so we add this to our index
randomIndex = randomIndex + 1; #This will get us a number from 1 to 5; =)
return cards[randomIndex];
end
I hope this points you in the right direction.