Monday, September 30, 2013

P2C3: Arrays

Arrays

An array stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
Arrays in C#

//Source code for C Sharp Arrays

using System;
namespace Application_Array
{
    class Program_Array
    {
        static void Main(string[] args)
        {
            int[] n = new int[10]; /* n is an array of 10 integers */
            int i, j;


            /* initialize elements of array n */
            for (i = 0; i < 10; i++)
            {
                n[i] = i + 10;
            }

            /* output each array element's value */
            Console.WriteLine("=====OUTPUT=====");
            for (j = 0; j < 10; j++)
            {
                Console.WriteLine("Element[{0}] = {1}", j, n[j]);
            }
            Console.ReadKey();
        }
    }
}

OUTPUT

=====OUTPUT=====
Element[0] = 10
Element[1] = 11
Element[2] = 12
Element[3] = 13
Element[4] = 14
Element[5] = 15
Element[6] = 16
Element[7] = 17
Element[8] = 18
Element[9] = 19

P2C2: Write a program in C Sharp for fibonacci numbers using iteration and recursion method.

Fibonacci Series:

Fibonacci series is the sequence of the numbers which has following properties:
  1. First number is 1.
  2. Second number is 1
  3. Remaining other numbers are sum of their preceding numbers. Eg: third number = 1+1=2
  4. Fibonacci series: 1, 1, 2, 3, 5, 8, 13, ....
------------------------------------------------------------------

//Source code using iteration method
using System;

public class Program_Fibonacci_Iteration
{
   public static void Main()
    {
        int num;
        long ans;

        Console.Write("Enter any number: ");
        num = Convert.ToInt32(Console.ReadLine());

        ans = Fibo(num);
        Console.WriteLine("\n :::::::: OUTPUT :::::::::\n");
        Console.WriteLine("The " + num + " th number in fibonacci series is: \n" + ans);
       
        Console.WriteLine("\n\nPress any key to exit...");
        Console.ReadLine();
    }

   static int Fibo(int n)
   {
       int num1 = 1, num2 = 1;
       int num3 = 0;

       if (n <= 2)
           return 1;
       else
       {
           for (int i = 3; i <= n; i++)
           {
               num3 = num1 + num2;
               num1 = num2;
               num2 = num3;
           }
           return num3;
       }
   }
}

OUTPUT

Enter any number: 8

 :::::::: OUTPUT :::::::::

The 8 th number in fibonacci series is:
21


Press any key to exit...

-------------------------------------------------------------------

//Source code using recursion method
using System;

public class Program_Fibonacci_Recursion
{
   public static void Main()
    {
        int num;
        long ans;

        Console.Write("Enter any number: ");
        num = Convert.ToInt32(Console.ReadLine());

        ans = Fibo(num);
        Console.WriteLine("\n :::::::: OUTPUT :::::::::\n");
        Console.WriteLine("The " + num + " th number in fibonacci series is: \n" + ans);
     
        Console.WriteLine("\n\nPress any key to exit...");
        Console.ReadLine();
    }

   static int Fibo(int n)
   {
       if (n <= 2)
           return 1;
       else
           return (Fibo(n-1) + Fibo(n-2));
   }

}

OUTPUT

Enter any number: 8

 :::::::: OUTPUT :::::::::

The 8 th number in fibonacci series is:
21


Press any key to exit...

---------------------------------------------------------------
//Printing Fibonacci Series
using System;

public class Program_Fibonacci_Recursion
{
   public static void Main()
    {
        int n, num1, num2, num3;
        num1 = 1; //First fibonacci number
        num2 = 1; //Second fibonacci number
     
        Console.Write("Enter any number: ");
        n = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("\n\n:::::::: OUTPUT :::::::::");
     
        //To print first and second fibonacci numbers
        Console.WriteLine("Position: " + 1 + "\tValue: " + num1);
        Console.WriteLine("Position: " + 2 + "\tValue: " + num2);

       //Counting for fibonacci number starts from third, so i = 3 is assigned
        for (int i = 3; i <= n; i++)
        {
            num3 = num1 + num2;
            num1 = num2;
            num2 = num3;
            Console.WriteLine("Position: " + i + "\tValue: " + num3);
        }
           
        Console.WriteLine("\n\nPress any key to exit...");
        Console.ReadLine();
    }
}

OUTPUT

Enter any number: 12


:::::::: OUTPUT :::::::::
Position: 1     Value: 1
Position: 2     Value: 1
Position: 3     Value: 2
Position: 4     Value: 3
Position: 5     Value: 5
Position: 6     Value: 8
Position: 7     Value: 13
Position: 8     Value: 21
Position: 9     Value: 34
Position: 10    Value: 55
Position: 11    Value: 89
Position: 12    Value: 144


Press any key to exit...

P2C2: Write a program in C Sharp to calculate 2's power using recursion.

//Write a program in C Sharp to calculate 2's power using recursion
using System;

public class Program_Power_Recursion
{
   public static void Main()
    {
        int num;
        long ans;

        Console.Write("Enter any number: ");
        num = Convert.ToInt32(Console.ReadLine());

        ans = Power2(num);
        Console.WriteLine("\n :::::::: OUTPUT :::::::::\n");
        Console.WriteLine(" 2^" + num + " = " + ans);
     
        Console.WriteLine("\n\nPress any key to exit...");
        Console.ReadLine();
    }

   static long Power2(int n)
   {
       if (n == 0)
           return 1;
       else
           return (2 * Power2(n - 1));
   }
}

OUTPUT:

Enter any number: 10

 :::::::: OUTPUT :::::::::

 2^10 = 1024


Press any key to exit...

Sunday, September 29, 2013

P2C2: Write a program in C Sharp to check whether a number entered by a user is even or odd.

//Using if else statement

using System;

public class Program_Even_Odd
{
   public static void Main()
    {
        int num;

        Console.WriteLine("Enter any number: ");
        num = Convert.ToInt32(Console.ReadLine());

        if (num % 2 == 0)
            Console.WriteLine("\n{0} is an Even number", num);
        else
            Console.WriteLine("\n{0} is an Odd number", num);
                 
        Console.WriteLine("\nPress any key to exit...");
        Console.ReadLine();

    }
}

OUTPUT:

Enter any number:
100

100 is an Even number

Press any key to exit...
---------------------------------------------------------------------------------------------

//Using case statement

using System;

public class Program_Even_Odd
{
   public static void Main()
    {
        int num, ans;

        Console.WriteLine("Enter any number: ");
        num = Convert.ToInt32(Console.ReadLine());

       ans = (num % 2);

       switch (ans)
       { 
           case 0:
               Console.WriteLine("\n{0} is an Even number", num);
               break;

           case 1:
               Console.WriteLine("\n{0} is an Odd number", num);
               break;

           default:
               Console.WriteLine("Invalid Entry");
               break;
       }
                   
        Console.WriteLine("\nPress any key to exit...");
        Console.ReadLine();

    }
}

OUTPUT:

Enter any number:
55

55 is an Odd number


Press any key to exit...

P2C2: C Sharp Program - Sum of 10 consecutive numbers using while, do ... while and for loop

// Using while loop

using System;

public class Program_Sum
{
   public static void Main()
    {
        int n=0, sum=0;
     
        while (n < 10)
        {
            n = n + 1;
            sum = sum + n;
            Console.WriteLine("Number = {0} Sum = {1}", n, sum);
        }


        Console.WriteLine("\n\nSum of {0} numbers = {1}", n, sum);
     
        Console.WriteLine("\nPress any key to exit...");
        Console.ReadLine();

    }
}

OUTPUT:

Number = 1 Sum = 1
Number = 2 Sum = 3
Number = 3 Sum = 6
Number = 4 Sum = 10
Number = 5 Sum = 15
Number = 6 Sum = 21
Number = 7 Sum = 28
Number = 8 Sum = 36
Number = 9 Sum = 45
Number = 10 Sum = 55


Sum of 10 numbers = 55

Press any key to exit...


//Using do ... while loop


using System;

public class Program_Sum
{
   public static void Main()
    {
        int n=0, sum=0;

        do
        {
            n = n + 1;
            sum = sum + n;
            Console.WriteLine("Number = {0} Sum = {1}", n, sum);
        } while (n < 10);


        Console.WriteLine("\n\nSum of {0} numbers = {1}", n, sum);
     
        Console.WriteLine("\nPress any key to exit...");
        Console.ReadLine();

    }
}

OUTPUT:

Number = 1 Sum = 1
Number = 2 Sum = 3
Number = 3 Sum = 6
Number = 4 Sum = 10
Number = 5 Sum = 15
Number = 6 Sum = 21
Number = 7 Sum = 28
Number = 8 Sum = 36
Number = 9 Sum = 45
Number = 10 Sum = 55


Sum of 10 numbers = 55

Press any key to exit...


//Using for loop

using System;

public class Program_Sum
{
   public static void Main()
    {
        int sum=0;

        for(int n=1; n<=10; n++)
        {
            sum = sum + n;
            Console.WriteLine("N = {0} \t Sum = {1}", n, sum);
        }

                 
        Console.WriteLine("\nPress any key to exit...");
        Console.ReadLine();

    }
}

OUTPUT:

N = 1    Sum = 1
N = 2    Sum = 3
N = 3    Sum = 6
N = 4    Sum = 10
N = 5    Sum = 15
N = 6    Sum = 21
N = 7    Sum = 28
N = 8    Sum = 36
N = 9    Sum = 45
N = 10   Sum = 55

Press any key to exit...

P2C2: C Sharp Program - Multiplication of two numbers using function

//Multiplication of two numbers using function
using System;

public class MultiplicationProgram
{
   static long Multiply (int n1, int n2)
    {
        return (n1 * n2);
    }

    public static void Main()
    {
        int num1, num2;
        long mul=1;

        Console.WriteLine("Enter First Number:");
        num1=Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Enter Second Number:");
        num2 = Convert.ToInt32(Console.ReadLine());

        mul = Multiply(num1, num2);
     
        Console.WriteLine("\nMultiplication = {0} * {1} = {2}",num1, num2, mul);
        Console.WriteLine("\nPress any key to exit...");
        Console.ReadLine();

    }
}

OUTPUT:

Enter First Number:
10
Enter Second Number:
20

Multiplication = 10 * 20 = 200

Press any key to exit...

P2C2: C Sharp Program - Factorial using Recursion and Iteration

//Source Code using Recursion
using System;

public class Recursion_Factorial
{
   static long Factorial(int num)
    {
        if (num <= 1)
            return 1;
        return num * Factorial(num - 1);
    }

    public static void Main()
    {
        int num;
        long fact=1;

        Console.WriteLine("Enter a Number");
        num=Convert.ToInt32(Console.ReadLine());

        fact = Factorial(num);
     
        Console.WriteLine("Number = {0} and Factorial = {1}",num,fact);
        Console.WriteLine("\nPress any key to exit...");
        Console.ReadLine();

    }
}

OUTPUT:

Enter a Number
6
Number = 6 and Factorial = 720

Press any key to exit...

-------------------------------------------------------------------------------------

//Using Iteration Method
using System;

public class Program_Factorial_Iteration
{
   public static void Main()
    {
        int num;
        long ans;

        Console.WriteLine("Enter any number: ");
        num = Convert.ToInt32(Console.ReadLine());

        ans = factorial(num);
        Console.WriteLine("Factorial of " + num + " = " + ans);
       
        Console.WriteLine("\n\nPress any key to exit...");
        Console.ReadLine();
    }

   static long factorial(int n)
   {
       long fact;
       if (n <= 1)
           return 1;
       else
       {
           fact = 1;
           
           for (int i = n; i > 0; i--)
           {
               fact = fact * i;
           }
           return fact;
       }      
   }
}

OUTPUT:

Enter any number:
5
Factorial of 5 = 120


Press any key to exit...

Thursday, September 26, 2013

P2C5: Writing maintainable programs

Outlines

  1. Programming terminology:
    • A variable is a data item associated with a memory location of which the value can change.
    • A constant is a data item associated with a memory location of which the value can't change. If the value needs to change, it is changed once and the program is recompiled.
    • An identifier is a name that represents an element in a program such as a variable, a constant, a function, a procedure, a user-defined data type or an object.
    • A reserved word, or keyword, is one that is defined by the programming language, such as int or switch in C#.Net.
  2. Variables must usually be declared so that the computer knows how much space to allocate and the type of data to which they refer.
  3. The identifier name for a variable or constant must be unique and not a reserved word. It should be meaningful.
  4. Variables should be initialized appropriately before using them
  5. Variables have a scope within the program code:
    • a variable which is recognised everywhere in the code is said to be a "global" variable
    • a variable may be "local" to a procedure or function.
  6. Uses followings for good readability of source codes:
    • comments
    • indentation and formatting to show the control structures (i.e. if statements and loops)
    • white spaces



Sunday, September 8, 2013

P3C6: Databases

Databases - Outlines

  1. A flat file consists of a large number of records each of which comprises a number of fields.
  2. Each field has its own data type and holds a single item of data.
  3. Limitations of flat-file systems:
    • separation and isolation of data
    • duplication of data
    • data dependence
    • data inconsistencies
    • difficulty in changing applications programs
  4. Relational databases store data in tables.
  5. Normalization is a set of formal rules that must be followed after a set of table is designed.
  6. Normal forms (NF):
    • 1 NF: It doesn't have a repeated group of attributes
    • 2 NF: There are no non-key fields attributes which are dependent on only part of the primary key
    • 3NF: There are no dependencies between non-key attributes
  7. An entity - relationship (E-R) diagram shows the entities in the database and the relationships between them.
  8. A relational database gives the following advantages over flat files:
    • Data are contained in a single software application.
    • Duplication of data is minimized.
    • Data inconsistency is reduced.
    • The volume of data is reduced leading to faster searching and sorting of data.
    • Data structures remain the same even when tables are altered.
    • Existing programs don't need to be altered when a table design is changed or new tables created.
    • Queries and reports can be set up quickly.
  9. A primary key is used to uniquely identify a record or row in a table.
  10. A foreign key attribute links to a primary key in a second table.
  11. A secondary key is used to get fast access when searching on this attribute.
  12. Different categories of user can see different views of the data - only what is needed for their job.
  13. Terminals in public areas may be refused access to sensitive data, even if the user has access.
  14. A DBMS (Database Management System) is a piece of software that provides the following facilities:
    • a data definition language (DDL) that the database designer uses to define the tables of the database
    • a data dictionary that contains all design information
    • a data manipulation language (DML) for inserting, selecting, updating and deleting data records
    • backup of the database data
    • control of multi-user access to the data.
  15. The DML and DDL used by all modern database software is Structured Query Language (SQL).

P1C4: System software

System software - Outlines

  1. The purpose of operating system
    • controls the hardware
    • provides a platform for application software to run onto it
    • provides an HCI (Human - Computer Interaction)
    • manages the resources of the computer system
  2. A batch operating system controls the passage of jobs through the computer without user interaction.
  3. A real-time operating system reacts quickly enough to affect the next input or process that needs to be carried out. It operates a continuous cycle of input-processing-output.
  4. A single-user operating system enables only one user at a time to access the system.
  5. A multi-user operating system enables more than one user to access the system at the same time.
  6. A multi-tasking operating system gives the user the impression that they can carry out more than one task at the same time.
  7. A network operating system links a number of computers together and enables them to share peripherals.
  8. Applications that require batch processing include:
    • payroll
    • bank statements
    • utility bills
  9. Applications that require a real-time response include:
    • industrial control systems
    • robots
    • ticket-booking systems
  10. A form-based user interface provides boxes into which the user can type data. It provides the user with help (on-screen prompts and validation) in completing the data.
  11. A menu-based user interface provides the user with a set of options that restrict the available information. An information system for tourists and on-screen menus for digital television are easy for users to operate.
  12. A graphical user interface (GUI) provides windows, icons, menus and a pointer to enable the user to interact with the computer in complex ways.
  13. A natural language user interface enables the user to use a natural language such as English to interact with the computer. It may be spoken or typed input.
  14. A command line user interface requires the user to type commands to give specific instructions to the computer. It enables a technician to get close to the workings of the computer.
  15. Disk formatting software prepares a disk for use by the operating system.
  16. File handling software enables the user to move, copy and delete files.
  17. Hardware drivers enable successful communication between devices and the operating system.
  18. File compression software allows data to be stored in a smaller amount of storage space.
  19. Virus-checking software monitors input and stored data to ensure that it does not contain malicious software.

Wednesday, September 4, 2013

A Level Computing 9691

The blog concerns on A Level Computing 9691 paper.

The Computing Course consists 4 papers:-

  1. Computer systems, communications and software: 75 marks
  2. Practical programming techniques: 75 marks
  3. Systems software mechanisms, machine architecture, database theory, programming paradigms and integrated information systems: 90 marks
  4. Computing project: 60 marks