C lecture

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

Arrays

Single-Dimensional Arrays
C programming Arrays is the collection of
elements of the same data types.
Declaration:
typename variablename[size]
 typename is any type

 variablename is any legal variable name

 size is a number the compiler can figure out

For example
int a[10]; ----Defines an array of int with subscripts
ranging from 0 to 9
There are 10*sizeof(int) bytes of memory
reserved for this array.

You can use a[0]=10; x=a[2]; a[3]=a[2]; etc.


You can use scanf("%d",&a[3]);
Using Constants to Define Arrays
It is useful to define arrays using constants:
#define MONTHS 12
int array [MONTHS];

However, in ANSI C, you cannot


int n;
scanf("%d", &n);
int array[n];
Initializing Arrays (Compile Time)
Initialization of arrays can be done by a
comma separated list following its definition.
For example:
int array [4] = { 100, 200, 300, 400 };
This is equivalent to:
int array [4];
array[0] = 100;
array[1] = 200;
array[2] = 300;
array[3] = 400;
Another way:
int array[] = { 100, 200, 300, 400};
Example of one Dimensional Array
#include <stdio.h>
int main(){
int marks[10],i,n,sum=0;
printf("Enter number of students: ");
scanf("%d",&n);
for(i=1; i<=n; i++){
printf("Enter marks of student%d: ",i);
scanf("%d",&marks[i]);
sum+=marks[i];
}
printf("Sum= %d",sum);
return 0;
}
Find the smallest number
#include<stdio.h>
int main(){
int arr[50],n,i,small;
printf("\nEnter the number of the array: ");
scanf("%d",&n);
printf("\nEnter %d elements in to the array: ", n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
small=arr[0];
for(i=1;i<n;i++){
if(small>arr[i])
small=arr[i];
}
printf("\nThe smallest number is: %d",small);
return 0;
}
Sorting program
#include<stdio.h>
void main(){
int i, j, t, a[20], n;
printf("Enter the number input:\n");
scanf("%d", &n);
printf("Enter the number input:\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]){
t=a[i];
a[i]=a[j];
a[j]=t;
}}}
printf("Ascending Order is:");
for(j=1; j<=n; j++)
printf("\n%d",a[j]);
}
Initializing Multidimensional Arrays
The following initializes a[4][3]:
int a[4] [3] = { {1, 2, 3} , { 4, 5, 6} , {7, 8, 9} , {10,
11, 12} };
Also can be done by:
int a[4] [3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12 };
is equivalent to
a[0][0] = 1;
a[0][1] = 2;
a[0][2] = 3;
a[1][0] = 4;
...
a[3][2] = 12;
#include<stdio.h>
int main()
{
int a[4][4], i , j, x=1;
printf("The Two dimensional array\n\n");
for (i = 0; i < 4; i++)
{
for ( j = 0; j < 4; j++)
{
a[i][j] = x++;
printf("a[%d][%d] = %d \n", i, j, a[i][j]);
}
}
}
#include<stdio.h>
int main()
{
int a[6][6],i,j;
printf("The Two dimensional array\n\n");
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(i==j)
a[i][j]=1;
else
a[i][j]=0;
printf("%d ",a[i][j]);
}
printf("\n");
}
}
Pascal’s Triangle:
#include<stdio.h>
void main()
{
int A[15][15]={0},i,j,n;
printf("Enter the number of rows:");
scanf("%d",&n);
for(i=0;i<n;i++)
A[i][0]=1;
for(i=1;i<n;i++)
for(j=1;j<i+1;j++)
A[i][j]=A[i-1][j-1]+A[i-1][j];

for(i=0;i<n;i++)
{ for(j=0;j<=i;j++)
printf("%d ",A[i][j]);
printf("\n\n");
}
#include<stdio.h> printf("\nThe Second matrix
int main(){ is\n");
int a[3][3],b[3][3],c[3][3],i,j; for(i=0;i<3;i++){
printf("Enter the First matrix"); printf("\n");
for(i=0;i<3;i++) for(j=0;j<3;j++)
for(j=0;j<3;j++) printf("%d\t",b[i][j]);
scanf("%d",&a[i][j]); }
printf("\nEnter the Second for(i=0;i<3;i++)
matrix"); for(j=0;j<3;j++)
for(i=0;i<3;i++) c[i][j]=a[i][j]+b[i][j];
for(j=0;j<3;j++) printf("\nThe Addition of
scanf("%d",&b[i][j]); two matrix is\n");
printf("\nThe First matrix is\n"); for(i=0;i<3;i++){
for(i=0;i<3;i++){ printf("\n");
printf("\n"); for(j=0;j<3;j++)
for(j=0;j<3;j++) printf("%d\t",c[i][j]);
printf("%d\t",a[i][j]); }
} return 0;
#include<stdio.h> printf("The Second Matrix is : \n");
void main() { for (i = 0; i < 3; i++) {
int a[10][10], b[10][10], c[10][10], for (j = 0; j < 3; j++) {
i, j, k; printf(" %d ", b[i][j]); }
int sum = 0; printf("\n"); }
printf("\nEnter First Matrix : \n"); for (i = 0; i < 3; i++) {
for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) {
for (j = 0; j < 3; j++) { sum = 0;
scanf("%d", &a[i][j]); for (k = 0; k <3; k++) {
} } sum = sum + a[i][k] * b[k][j];
printf("\nEnter Second Matrix:\n"); }
for (i = 0; i < 3; i++) { c[i][j] = sum;
for (j = 0; j < 3; j++) { } }
scanf("%d", &b[i][j]); printf("\nMultiplication Of Two
} } Matrices : \n");
printf("The First Matrix is: \n"); for (i = 0; i < 3; i++) {
for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) {
for (j = 0; j < 3; j++) { printf(" %d ", c[i][j]);
printf(" %d ", a[i][j]); }
} printf("\n");
printf("\n"); } } }

You might also like