Monday, 22 September 2014

Stacks & Queues

First I will start of with basic skills needed to go into stacks and queues:

The first thing I did in this is made a list called ages, put random values in and then checked them by printing the list and seeing if everything was correct.

The next thing I did was changing an element of the list by referencing the index number. To do this all you have to do is write the name of the list "ages" and then in square brackets [] type the index number you want to change. For example I have done "ages[1]=18" this now means that index 1 in the list has now been changed to 18, even though previously it was 19.

After that I did another basic thing which was adding to a list using append. This is pretty simple as all you have to do is type the name of the list, .append it and then type whatever you want to add to the list in brackets (). For example "ages.append(12)"

Then we move on to inserting a value into the middle of the list. I did this using insert. This is also very simple as all you have to do is type the name of the list, .insert it and then in brackets the index where you want to input the value and then the value. So I did, "ages.insert(6,14)". This means that the value "14" will be inserted into the list called ages at the 6th position.

Moving on, you can also count the number of duplicates you have in your list. This can be done using the count function. For this all you need to do is type the name of the list, .count it and then in brackets type the value you want to check the duplicates for. You also have to print this to actually be able to see how many duplicate values you actually have. For example what I did was "print(ages.count(12))". This will print the amount of times "12" is in the list.

 Finally we have come on to Stacks & Queues. A Stack is a list with a first in, last out (FILO) mechanism for managing elements where the last item added is the first item removed.
Basically what the code I have written for stack does a basic thing, firstly it prints the stack which is in range 19, so all values from 0-18 will be printed. Then I append to the stack and add 19 into the stack. After that comes the fun part, we start to pop the stack. Popping in stacks means that you are getting rid of the last value in the stack, so after popping 2 values our stack will only have values from 0-17 as 18 and 19 have been popped off.

A queue is a list with a first in, first out (FIFO) mechanism for managing elements where the first item added is the first item removed. We start of doing exactly what did for stacks and make the range 19 so we will have values from 0-18, after this I decided to append 3 more values into the queue being 20,21 and 22. Then I made a variable "n" and made its value the first value in the queue and then I removed it. So in the queue the first value that has been inputted is the first one out. So after removing "n" we have values from 1-18 + 20,21,22.

Wednesday, 17 September 2014

Passing Parameters + 2D arrays

Passing Parameters + 2D Arrays

*Sorry for my varaible and function names they don't go with the program
**Also it will help if you read my previous post before this as it is actually building on from there















The first part of this is exactly the same as my last post, the function does exactly the same thing as it did before. However this time I will be using 2D Arrays with this function as well. For this to work one list (which the studentlist) is outside the main program.

Then there is another list which is inside of the main program. This list is called "listinlist". Then I have done exactly what I explained in my last post except I am appending to the list which is insde the main program which is "listinlist". After the user has inputted the required enteries then I ran the printMail_MergeItems functions. This time though the records that have been entered will be saved in the list which is inside the functions, and that list will be saved inside the "studentlist".

The Last bit of my program will then be able to search the entries that have been entered and have been saved in the lists. To do this I made a variable called "printstudents" this asks for the user to input an integer, so the user will input the integer of the record they are looking for. The same thing has been done with the other variable though this will search for the record inside the first record.

For e.g someone has a fullname apple username banana and password orange. The printstudents variable will find this record which will contain all 3 of those things, the searchstudents variable will then find either the fullname, the username or the password.

The last bit which says print(studentlist[printstudents][searchstudents]) is actually just printing the record the user has asked to find.

Sunday, 14 September 2014

Passing Parameters

A2 Computing Basic Coding
Passing Parameters
This is the basic passing parameters code. The function has "(name)" after it this is because that is the variable that needs to be substituted from the main part of the program where the user inputted it.
In the main part of the program the variable "(name)" is called "(enteredname)

Passing Parameters 2

This is when I have developed my program further. Firstly I have added 3 lists, the studentlist, usernamelist and passwordlist. Whatever the user enters for fullname, username and password will be saved in these lists.

We also have a loop in this, this improves the program as then the user can add as many records as they want until they want to stop. This is because we have named a variable called "restart" this is set to "y" as its starting value. Then I have added a while loop so while that condition is true then the loop will continue however if it is not true then the program will stop and no more records will be added.

However there is a flaw in this code as if you type "y" when it asks you "Do you want to make another record y or n?") if you type in anything other than "y" the program will stop working.