Sr. No. | Instructions | Description |
#include<stdio.h> | Header file included | |
2. | #include<conio.h> | Header file included |
3. | void main() | Execution of program begins |
4. | { | Memory is allocated for variable i,n and array a |
5. | inti,n,a[10]; | |
6. | clrscr(); | Clear the output of previous screen |
7. | printf("enter a number"); | Print “enter a number” |
8. | scanf("%d",&n); | Input value is stored at the addres of variable n |
9. | for(i=0;i<=10;i++) | For loop started from value of i=0 to i=10 |
10. | { | Compound statement(scope of for loop starts) |
11. | a[i]=n*i; | Result of multiplication of n and I is stored at the ith location of array ieasi=0 so it is stores at first location. |
12. | printf("\n %d",a[i]); | Value of ith location of the array is printed |
13. | } | Compound statement(scope of for loop ends) |
14. | printf("\n first element in array is %d",a[0]); | Value of first element of the array is printed |
15. | printf("\n fifth element in array is %d",a[4]); | Value of fifth element of the array is printed |
16. | printf("\n tenth element in array is %d",a[9]); | Value of tenth element of the array is printed |
17. | getch(); | Used to hold the output screen |
18. | } | Indicates end of scope of main function |
#include<conio.h>
void main()
{
int a[20];
intn,i;
printf("\n Enter the size of array :");
scanf("%d",&n);
printf("size of array is %d",n);
printf("\n Enter Values to store in array one by one by pressing ENTER :");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n The Values in array are..");
for(i=0;i<n;i++)
{
printf("\n %d",a[i]);
}
getch();
} P2: Program to copy 1-D array to another 1-D array#include <stdio.h>#include<conio.h> void main(){int n, i, j, a[100], b[100];// a is original array and b is copied array printf("Enter the number of elements in array a\n");scanf("%d", &n); printf("Enter the array elements in a\n"); for (i= 0; i< n ; i++)scanf("%d", &a[i]); /* Copying elements from array a into array b */ for (i =0;i<n;i++)b[i] = a[i]; /* Printing copied array b. */ printf("Elements of array b are\n"); for (i = 0; i< n; i++)printf("%d\n", b[i]); getch();} Output: P3:Program to find reverse of 1-D array#include <stdio.h>#include<conio.h> void main(){int n, i, j, a[100], b[100];// a is original array and b is reversed array printf("Enter the number of elements in array\n");scanf("%d", &n); printf("Enter the array elements\n"); for (i= 0; i< n ; i++)scanf("%d", &a[i]); /* * Copying elements into array b starting from end of array a */ for (i = n - 1, j = 0; i>= 0; i--, j++)b[j] = a[i]; /* Printing reversed array b. */ printf("Reverse array is\n"); for (i = 0; i< n; i++)printf("%d\n", a[i]); getch();}Output:
#include<conio.h>
void main()
{
int a[20];
intn,i;
printf("\n Enter the size of array :");
scanf("%d",&n);
printf("size of array is %d",n);
printf("\n Enter Values to store in array one by one by pressing ENTER :");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n The Values in array are..");for(i=0;i<n;i++)
{
printf("\n %d",a[i]);
}
getch();
} Q4. Explain multidimensional arrays?A4. 1-D array has 1 row and multiple columns but multidimensional array has of multiple rows and multiple columns.Two Dimensional array:It is the simplest multidimensional array. They are also called as matrix.Declaration: data-typearray_name[no.of rows][no. of columns];Total no of elements= No.of rows * No. of Columns Example 3: inta[2][3]This example illustrates two dimensional array a of 2 rows and 3 columns.
0 1 2 3 4 |
|