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