Home | Lab 1 | Lab 2 | Lab 3 | Lab 4 | Lab 5 | Lab 6 | Lab 7 | Lab 8 | Lab 9 | Lab 10 | Lab 11 | Lab 12

Linked at EntranceNepal.com, i-geek blog & Ekendra's Share World

Object I. To write a program to display a set of data into ascending and descending order.

Object II. To write a program to add two matrices.

Download this full program | Lab 6 | in pdf format

Background Theory:

An array is a group of related data items that share a common name. A particular value is indicated by writing a number called index number or subscript in brackets after the array name, as salary[10]. The complete set of values is referred as an array and the individual values elements.

 One-Dimensional Array: A list of items can be given one variable name using only one subscript; such a variable is called a single-subscripted variable or a one-dimensional array. 

Declaration of Arrays: Like any other variable, arrays must be declared before they are used. The general form of array declaration is type variable-name[size];. The type specifies the type of element that will be contained in the array, such as int, float or char and the size indicates the maximum number of elements that can be stored inside the array.  For an instance: float height[50]; declares the height to be an array containing 50 real elements. Any subscripts 0 to 49 or 1 to 50 are valid.

 Two-Dimensional Array: Whenever a table of values has to be stored such array type is used to represent specified numbers of rows and columns. Two-dimensional arrays are declared as: type array-name[row-size][column-size];.

 Single operations which involve entire arrays are not permitted in C. Thus, if a and b are similar arrays ie same data type, same dimensionality and same size, assignment operations, comparison operations, etc must be carried out on an element-by-element basis. This is usually accomplished within a loop, where each pass thru the loop is used to process one array element. The number of passes thru the loop will therefore equal the number of array elements to be processed. Each array element represents a single numerical quantity. 

Using loop read the set of data and store the data element in array index like a [0], a[1] etc is the initial step during the performance of the program. The array elements are compared and swapped if necessary, using loop.

 

Code:

//to sort a set of data into order//

#include<stdio.h>

#include<conio.h>

 

void main()

{

            int i, j, a[20], n, temp;

            clrscr();

            printf("\nHow many numbers to be sorted?\t");

            scanf("%d", &n);

            printf("\n\nEnter these %d numbers\n\t", n);

            for(i=1; i<=n; i++)

            scanf("%d", &a[i]);

            for(i=1; i<=n; i++)

            {

                for(j=i+1; j<=n;j ++)

                {

                        if (a[i]>a[j])

                        {

                             temp=a[i];

                             a[i]=a[j];

                             a[j]=temp;

                        }

                }

 

            }

            printf("\nAscending order:\n\n");

            for(i=1; i<=n; i++)

            printf("%d\t", a[i]);

 

            printf("\n\nDescending order:\n\n");

            for (i=n; i>=1 ;--i)

            printf("%d\t", a[i]);

            getch();

}

//program to add two matrices//

 

#include<stdio.h>

#include<conio.h>

void main()

{

            int i, j, k, a[2][3],b[2][3];

            clrscr();

            printf("Enter the elements of the first matrix\n\t");

            for(i=0; i<2; i++)

            {

               for(j=0; j<3; j++)

               scanf("%d",&a[i][j]);

            }

            printf("Enter the elements of the second matrix\n\t");

            for(i=0; i<2; i++)

            {

               for(j=0; j<3; j++)

               scanf("%d", &b[i][j]);

            }

            printf("\nRequired matrix is\n\t");

            for(i=0; i<2; i++)

            {

               for(j=0; j<3; j++)

               {

                  printf("%d ",(a[i][j]+b[i][j]));

               }

               printf("\n\t");

            }

            getch();

}

To Download this full program | Lab 6 | in pdf format you must have PDF reader as Adobe Acrobat Reader 5 and above in your computer,

click here to download now.

Search yourself for more such codes

Google

Home | Lab 1 | Lab 2 | Lab 3 | Lab 4 | Lab 5 | Lab 6 | Lab 7 | Lab 8 | Lab 9 | Lab 10 | Lab 11 | Lab 12

Protected under Creative Commons License.
Written by Ekendra Lamsal, www.ekendralamsal.com
for comments email at gmail address:
gearShifts@gmail.com