Wednesday, December 9, 2015

Array Worksheet ( Visual Basic )



1. Write a program to input price of 10 items in a one-dimentional array and display them in a row on the screen. Also, display the average price of all the items.

Programming Language: Visual Basic

Module Module1
Sub Main()
Dim price(10), counter, average, total As Decimal
Console.WriteLine("Enter the price of 10 items")
For counter = 1 To 10
price(counter) = Console.ReadLine()
Next
Console.WriteLine()
For counter = 1 To 10
Console.Write(price(counter) & " ")
total = total + price(counter)
Next
average = total / 10
Console.WriteLine()
Console.WriteLine()
Console.WriteLine("The average price of all the items is=" & average)
Console.ReadLine()
End Sub

End Module


Programming Language: C++ (Turbo)
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int price[10],counter;
int total=0;
int average=0;
cout<<"Enter the price of10 items: \n";
for (counter=1;counter<=10;counter=counter+1)
{
cin>>price[counter];
}
for (counter=1;counter<=10;counter=counter+1){
cout<<" "<<price[counter];
total=total+price[counter];
}
average=total/10;
cout<<" ";
cout<<"\n The average price of 10 items is="<<average;
getch();
return 0;
}






2. Make changes in program (1) such that it also displays number of items that cost more than average, equal to average and less than average.

Programming Language: Visual Basic
Module Module1

Sub Main()
Dim price(10), counter, average, total As Decimal
Dim greater, lesser, equal As Decimal
Console.WriteLine("Enter the price of 10 items")
For counter = 1 To 10
price(counter) = Console.ReadLine()
Next
Console.WriteLine()
For counter = 1 To 10
Console.Write(price(counter) & " ")
total = total + price(counter)
Next
average = total / 10
For counter = 1 To 10
If price(counter) > average Then
greater = greater + 1
ElseIf price(counter) < average Then
lesser = lesser + 1
Else
equal = equal + 1
End If
Next
Console.WriteLine()
Console.WriteLine("The average price of the given items is=" & average)
Console.WriteLine("The number of price greater than average=" & greater)
Console.WriteLine("The number of price lesser than average=" & lesser)
Console.WriteLine("The number of price equal than average=" & equal)
Console.ReadLine()
End Sub

End Module







3.       Write a program to input age of 10 people in a one-dimensional array. Display the age the oldest and the youngest person with appropriate message.


 Module Module1

    Sub Main()
        Dim age(10) As Integer
        Dim counter, old, young, store As Integer
        Console.WriteLine("Enter the age of 10 people:")
        For counter = 1 To 10
            age(counter) = Console.ReadLine
             Next
        store = age(1)
        For counter = 1 To 10
            If store > age(counter) Then
                store = age(counter)
            End If
        Next

        young = store
        For counter = 1 To 10
            If store < age(counter) Then
                store = age(counter)
            End If
        Next
        old = store
        Console.WriteLine()
        Console.WriteLine("The youngest age is=" & young)
        Console.WriteLine("The oldest age is=" & old)
        Console.ReadLine()
    End Sub

End Module




4.       Write a program to store eight integer values such that every next value is greater than the previous values. Display the difference between the subsequent values. For examples, if the array store.
  5         9       12      20       30   41     50    58

The output should be

            4       3         8      10       11       9     8


Module Module1

    Sub Main()

        Dim store1(8), Difference(7), temp As Integer
        Dim counter As Integer
        Console.WriteLine("Enter 8 values:")
        For counter = 1 To 8
            store1(counter) = Console.ReadLine()
        Next
        Dim just As Boolean


        just = True

        Do While just

            just = False

            For counter = 1 To 7
                If store1(counter) > store1(counter + 1) Then
                    temp = store1(counter)
                    store1(counter) = store1(counter + 1)
                    store1(counter + 1) = temp
                    just = True
                End If
            Next
        Loop

        For counter = 1 To 7

            Difference(counter) = store1(counter + 1) - store1(counter)
        Next
        Console.WriteLine("The difference between the subsequent values:")
        For counter = 1 To 7
            Console.WriteLine(Difference(counter))
        Next

        Console.ReadLine()

    End Sub
End Module





5.       Refer to array in program (4). Write a program to print the product of the consecutive values. The output should be
  45              240             1230         2900


Module Module1

    Sub Main()
        Dim store(8), product(4) As Integer
        Dim counter, num1, num2 As Integer
        num1 = 1
        num2 = 2
        Console.WriteLine("Enter 8 values such that every next value is greater than the previous values:")
        For counter = 1 To 8
            store(counter) = Console.ReadLine()
        Next
        For counter = 1 To 4
            product(counter) = store(num1) * store(num2)
            num1 = num1 + 2
            num2 = num2 + 2
        Next
        Console.WriteLine("The product of  the consecutive values:")
        For counter = 1 To 4
            Console.WriteLine(product(counter))
        Next
        Console.ReadLine()
    End Sub

End Module





6.       Refer to array in program (3). Write a program to swap the contents of the alternate elements and then displays the values. The output should be
12         20         5        9          50         58        30             41



Module Module1

    Sub Main()
        Dim store(8), num(8) As Integer
        Dim temp As Integer = 3
        Dim counter As Integer
        Console.WriteLine("Enter 8 values such that every next value is greater than the previous values:")
        For counter = 1 To 8
            store(counter) = Console.ReadLine()
        Next
        For counter = 1 To 4
            If counter = 3 Then
                temp = 1
                num(counter) = store(temp)
            Else
                num(counter) = store(temp)
            End If
            temp = temp + 1
        Next
        temp = 7
        For counter = 5 To 8
            If counter = 7 Then
                temp = 5
                num(counter) = store(temp)
            Else
                num(counter) = store(temp)
            End If
            temp = temp + 1
        Next
        Console.WriteLine("The values after swaping:")
        For counter = 1 To 8
            Console.WriteLine(num(counter))
        Next
        Console.ReadLine()
    End Sub

End Module





7.       Write a program to input five integer values in a one-dimensional array A1 and another five integer values in another array called   A2. Mix the values in a third array called A3 by taking values alternating from each one of them. For example, if
 A1         9         3        29        63        7
 A2        70        5        96        33        5
The resultant array should be    
A3          9       70         3          5          29          96         63        33        7          5



Module Module1

    Sub Main()
        Dim A1(10), A2(10) As Integer
        Dim A3(10), counter As Integer
        Console.WriteLine("Enter five values for A1:")
        For counter = 1 To 10
            If counter Mod 2 <> 0 Then A1(counter) = Console.ReadLine()
        Next
        Console.WriteLine("Enter five values for A2:")
        For counter = 2 To 10 Step 2
            If counter Mod 2 = 0 Then A2(counter) = Console.ReadLine()
        Next
        For counter = 1 To 10
            If counter Mod 2 = 0 Then
                A3(counter) = A2(counter)
            Else
                A3(counter) = A1(counter)
            End If
        Next
        Console.WriteLine("The resultant array i.e, A3:")
        For counter = 1 To 10
            Console.WriteLine(A3(counter))
        Next
        Console.ReadLine()
    End Sub

End Module





8.       Write a program to input total income of 10 people in array. Display the values stored in an array and also print how many of them are taxpayers. If the total income is greater than or equal to Rs 200000 then the person has to pay the tax.



Module Module1

    Sub Main()
        Dim income(10), taxP As Integer
        Dim counter As Integer
        Console.WriteLine("Enter total income of ten peoples:")
        For counter = 1 To 10
            income(counter) = Console.ReadLine()
        Next
        For counter = 1 To 10
            If income(counter) >= 200000 Then taxP = taxP + 1
            Console.WriteLine("income(" & counter & ")=" & income(counter))
        Next
        Console.WriteLine("The number of taxpayers=" & taxP)
        Console.ReadLine()
    End Sub

End Module








9.       Write a program that stores 20 characters in a one-dimensional array and displays the location of all the vowels in it. For example, if the array stores

  S       P        R        I           N           G            M            E          A             D       O     W            S


The output should be
                      3                     8                    9                    11



Module Module1

    Sub Main()
        Dim str1(20) As String
        Dim counter As Integer
        Console.WriteLine("Enter 20 characters:")
        For counter = 1 To 20
            str1(counter) = Console.ReadLine()
        Next
        Console.WriteLine("Location of vowels:")
        For counter = 1 To 10
            If UCase(str1(counter)) = "A" Or UCase(str1(counter)) = "E" Or UCase(str1(counter)) = "I" Or UCase(str1(counter)) = "O" Or UCase(str1(counter)) = "U" Then
                Console.WriteLine(counter)
            End If
        Next
        Console.ReadLine()
    End Sub

End Module




10.   Write a program to copy the values stored in two-dimensional array to a one-dimensional array.  


  Module Module1

    Sub Main()
        Dim array1D(9), array2D(3, 3) As Integer
        Dim length, counter2, counter1 As Integer
        length = 1
        For counter1 = 1 To 3
            For counter2 = 1 To 3
                array2D(counter1, counter2) = Console.ReadLine()
                array1D(length) = array2D(counter1, counter2)
                length = length + 1
            Next
        Next
        For counter2 = 1 To 9
            Console.Write(" ")
            Console.WriteLine(array1D(counter2))
        Next
        Console.ReadLine()
    End Sub
End Module
                     



Thursday, September 17, 2015

Programming ( AS Level Computer Science)

Programming question ( Examination type):


The cost of delivery is calculated as follows:

     ● There is a basic delivery charge of $5 for all orders.
     ●   If the total weight of an order is more than 1 kg, there is an additional charge of $0.50 or every extra 0.1 kg.
         ●   If the total volume of an order is more than 1000 cubic.cm, there is an additional charge of $0.50 for every extra 200 cubic.cm
Write a program to ask user total weight and total volume of the items , and then show the total cost of delivery.




Module Module1

    Sub Main()
       Dim Totalweight, Totalvolume, Extravolume As Decimal
        Dim costweight, costvolume, totalcost As Decimal
        Console.Write("Enter total weight in kg:")
        Totalweight = Console.ReadLine()
        Console.Write("Enter it volume in  cubic.cm :")
        Totalvolume = Console.ReadLine()
        If Totalweight > 1 Then
            costweight = ((Totalweight - 1) / 0.1) * 0.5 + 5
        ElseIf Totalweight > 0 And Totalweight <= 1 Then
            costweight = 5
        Else
            costweight = 0
        End If
        If Totalvolume > 1200 Then
            Extravolume = (Totalvolume - 1000)
            If (Extravolume / 200) > 0 Then
                If (Extravolume / 200) Mod 200 = 1 Then
                    costvolume = ((Extravolume / 200) * 0.5) + 5
                Else
                    costvolume = (((Extravolume \ 200) + 1) * 0.5) + 5
                End If
            End If
        ElseIf Totalvolume > 0 And Totalvolume < 1200 Then
            costvolume = 5
        Else
            costvolume = 0
        End If
        If costweight > 1 And costvolume > 1 Then
            totalcost = costweight + costvolume - 5
        Else
            totalcost = 0
        End If
        Console.WriteLine("The cost of delivery=" & totalcost & "$")Console.ReadLine()
    End Sub

End Module







 Instagram


Sunday, September 6, 2015

AS-Level Related Computer Science Programming .( Visual Basic )

Programming Questions with solutions:



1.       Gina is developing her programming skills in string handling.

                 She is going to input one string. The string contains letters and zero or more " * " characters in any positions. Gina wants to remove all the " * "s and output the letters in their original order. For example:
                       ●    Input  "com*put**er*" , the output is "computer"
                       ●   Input "hardware", the output is "hardware"
(a)        Using a high-level programming language, write the code to perform this task. 
    ( Ensure that you use meaningful variable names and lay the code out clearly )


Module module1

    Sub main()
        Dim counter As Integer
        Dim inputString, temp, newString As String
        newString = ""

        Console.WriteLine("Enter any word:")
        inputString = Console.ReadLine()

        For counter = 1 To Len(inputString)
            temp = Mid(inputString, counter, 1)
            If temp <> "*" Then newString = newString + temp
        Next

        Console.WriteLine(newString)
        Console.ReadLine()
    End Sub

End Module





2.       John is developing his programming skills in string handling.

                 He is going to input two string.
           Each string is made up of three parts:
                       ●    Letters following by
                       ●    a single " * " character, followed by
                       ●     Letters

The groups of letters after the " * " characters are joined together to form a new string which is then output.

 For example, with " DFG*COM" and "B*PUTER" as inputs, the new string output will be
"COMPUTER".
(a) Using a high-level programming language, write the code to perform this task. ( Ensure that you use meaningful variable names and lay the code out clearly.)




Module module1
    Sub main()
        Dim counter As Integer
        Dim inputString1, inputString2, temp, newString, finalWord As String
        Dim newString1, newString2 As String
        Console.WriteLine("Enter any word:")
        inputString1 = Console.ReadLine()

        Console.WriteLine("Again enter any word:")
        inputString2 = Console.ReadLine()

        For counter = 1 To Len(inputString1)
            temp = Mid(inputString1, counter, 1)
            If temp = "*" Then newString1 = Mid(inputString1, counter + 1, Len(inputString1) - counter)
        Next

        For counter = 1 To Len(inputString2)
            temp = Mid(inputString2, counter, 1)
            If temp = "*" Then newString2 = Mid(inputString2, counter + 1, Len(inputString2) - counter)
        Next

        newString = newString1 + newString2
        For counter = 1 To Len(newString)
            temp = Mid(newString, counter, 1)
            If temp <> "*" Then finalWord = finalWord + temp
        Next
        Console.WriteLine(finalWord)
        Console.ReadLine()
    End Sub


End Module


Instagram