Jump to content

I hate asking for help on school work but I'm in a Cognitive Science class as a gened and have no background in programming or anything of that nature (I'm premed). This is very basic though. 

 

The question is: 

Use the Rudimentary Programming Language defined in class to write an algorithm that takes a string of digits as input and determines how many of the digits are even.   For example, if the input is “461653”, then the output should be “3”.

 

This is what I have written down, but I don't have any faith in my answer 

 

                LetterCount = 0

                While Input-Remains

                                Read NextLetter

                                If <even> then <count +1>

                                If <odd> then <count+0>

                End

                Print Number

 

 

 

Link to comment
https://linustechtips.com/topic/312368-very-basic-programming-help/
Share on other sites

Link to post
Share on other sites

Use the Rudimentary Programming Language defined in class = Something you and fellow class mates would only have info on....

 

 

But from what I've read it looks like make up programming with how to example programming to someone whom doesnt program so I would adjust and write:

EvenCount = 0;OddCount = 0;Input = "442233";While (Input.length > 0){	// Get letter to check	NumberToCheck = GetCharacterFrom(Input);		// Chop off number from Input which is now stored in LetterToCheck to prevent endless loop	Input.splice(); 		if ( isNumberEven(NumberToCheck) ){		EvenCount++;	} else {		OddCount++;	}}Print "There are " + EvenCount + " even numbers and " + OddCount + " odd numbers in your input.";
Link to post
Share on other sites

There are a couple issues with your solution but I can't really comment on the important lines without knowing what the language allows.

LetterCount = 0 // Not used. Remove unless used elsewhere. If used elsewhere, then relocate closer to where it's used.While Input-Remains     Read NextLetter    If <even> then <count +1>    If <odd> then <count+0> // This whole line isn't needed and can be removed. It's not doing anything useful.EndPrint Number // What is "Number"? Did you mean to print "count"?

But I guess I'll try

// This might be okWhile Input-Remains // Or maybe you should use something likeFor digit In number    // ...Next

If you can use the above recommended change, then you'll no longer need the following line

Read NextLetter

If you can't use the for loop, the above line may need some form of assignment

digit = Read NextLetterdigit = ReadNextLetter(Number)digit = ReadNextLetter From Number// etc

As for the comparison, you may need something more specific than

If <even>

Like the following options

// More verbose but may be valid, I can't sayIf <digit Is Even>// Common even test for programming languages where you test that the remainder when divided by 2 is 0If <digit % 2 == 0>

And usually when incrementing a counter by 1, it's done the following ways with most languages

count++count += 1count = count + 1

So one of the above options may be more correct that count + 1 which doesn't show any assignment back to count.

 

But again, we don't know what your language allows.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×