Sie sind auf Seite 1von 22

Computing Fundamentals

Dr. Muhammad Yousaf Hamza

2-D Arrays
Nesting in Loops

Dr. Yousaf, PIEAS

Multidimensional Arrays
Arrays in C can have virtually as many dimensions as
you want.
Definition is accomplished by adding additional
subscripts when it is defined.
For example:
int a [4] [3] ;
defines a two dimensional array

a[0][0]

a[0][1]

a[0][2]

Dr. Yousaf, PIEAS

2-D Arrays
How to store these values?
1
4
7
10

2
5
8
11

3
6
9
12

How to print these values?

Dr. Yousaf, PIEAS

How to Print 2-D Arrays?


#include<stdio.h>
int main()
{
int a[4] [3] = { {1, 2, 3} , { 4, 5, 6} , {7, 8, 9} , {10, 11, 12}
int row, col;
for (row = 0; row <=3; row++)
{ for (col = 0; col <=2; col++)
{
printf(%d", a[row][col]);
}
}
getchar(); return 0; }

Dr. Yousaf, PIEAS

};

Output of the Program


123456789101112

Dr. Yousaf, PIEAS

Output with Tabs


for (row = 0; row <=3; row++)
{ for (col = 0; col <=2; col++)
{
printf(%d\t",a[row][col]);
}
}
Output:
1
2
10
11

3
12

Dr. Yousaf, PIEAS

Output in Matrix Form


for (row = 0; row <=3; row++)
{ for (col = 0; col <=2; col++)
{
}

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

printf(\n);

Output:
1
2
4
5
7
8
11
10

3
6
9
12
Dr. Yousaf, PIEAS

How to scan 2-D Arrays?

#include<stdio.h>
int main()
{
int a[4] [3];
int row, col;
for (row = 0; row <=3; row++)
{
printf("Enter 3 elements of row %d\n", row + 1);
for (col = 0; col <=2; col++)
{
scanf("%d",&a[row][col]);
}
}
//Rest of the code goes here
Dr. Yousaf, PIEAS

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;
Dr. Yousaf, PIEAS

Multiple-Subscripted Arrays
Multiple subscripted arrays
Tables with rows and columns (m by n array)
Like matrices: specify row, then column
Column 0 Column 1 Column 2 Column 3
Row 0
Row 1
Row 2

a[ 0 ][ 0 ] a[ 0 ][ 1 ] a[ 0 ][ 2 ] a[ 0 ][ 3 ]
a[ 1 ][ 0 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ]
a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ]
Column subscript
Array name
Row subscript

Dr. Yousaf, PIEAS

Multiple-Subscripted Arrays
1
3

Initialization
int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
Initializers grouped by row in braces
If not enough, unspecified elements set to zero
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };

Referencing elements
Specify row, then column
printf( "%d", b[ 0 ][ 1 ] );

Dr. Yousaf, PIEAS

1
3

2
4

0
4

Multidimensional Arrays
Array declarations read right-to-left
int a[10][3][2];
an array of ten arrays of three arrays of two (type
ints). In memory
3

...
2

10

Dr. Yousaf, PIEAS

To Find the Maximum Marks


#include<stdio.h>
int main()
{
int marks[6] =
{36,78,7,99,43,29};
int i, sum;

Maximum = marks[0];
for (i = 1; i <6;i++)
{
if (Maximum <marks[i])
{
Maximum = marks[i];
}
}
printf("Maximum Marks are
%d\n",Maximum);
getchar();
return 0;
}
Dr. Yousaf, PIEAS

Addition of Two Matrices

Let us first two addition of the elements of two single


dimension arrays
#include<stdio.h>
printf("The resultant array by
the addition of these two
int main ()
arrays is:\n\n");
{
for(i=0;i<5;i++)
int a[5] = {3, 6, -7, 10, 4};
printf("\t%d",result[i]);
int b[5] = {10, 5, 8, 0, -7},
result[5], i;
for(i=0;i<5;i++)
result[i] = a[i] + b[i];

getch();
return 0;
}

Dr. Yousaf, PIEAS

Addition of Two Matrices


#include <stdio.h>
int main()
{
int X[2][2] = { {1,2},{3,4} }, Y[2][2] = {
{5,6},{7,8} };

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


{
printf("\n\n\t");
for (j = 0; j<2; j++)
{

add[i][j] = X[i][j] + Y[i][j];

int add[2][2];
int i, j;

printf("%d\t", add[i][j]);
}

printf("\n\tAddition of two matrices


is");

}
getchar();
return 0;
}

Dr. Yousaf, PIEAS

Multiplication of Two Matrices


#include <stdio.h>
int main()
{
int X[2][2] = { {1,2},{3,4} }, Y[2][2] = {
{5,6},{7,8} };
int add[2][2], mul[2][2];
int i, j, k, sum = 0;
printf("\n\n\tMultiplications of two
matrices is");

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


{
printf("\n\n\t");
for (j = 0; j<2; j++)
{
for (k=0; k<2; k++)
{
sum = sum + (X[i][k]*Y[k][j]);
}
mul[i][j] = sum;
printf("%d\t", mul[i][j]);
sum = 0;
}
}
getchar(); return 0; }

Dr. Yousaf, PIEAS

Some Examples

Dr. Yousaf, PIEAS

//Linear Search
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter the number of elements in
array\n");
scanf("%d",&n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the number to search\n");
scanf("%d", &search);

for (c = 0; c < n; c++)


{
if (array[c] == search)
{
printf("%d is present at location
%d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in
array.\n", search);

getchar();
return 0;

Dr. Yousaf, PIEAS

To Generate Random Numbers


#include <stdio.h>
#include <stdlib.h>
#include<math,h>
int main()
{
int c, n;
printf("Ten random numbers
in [1,100]\n");
for (c = 1; c <= 10; c++) {
n = rand()%100 + 1;
printf("%d\n", n);
}
getchar();
return 0;
}

If we use rand() only as


n = rand();
It can generate random number
of value in thousands. So when
we use
n = rand()%100 + 1;
Whatever be the value of
random number, its modulus
with hundred will always be in
the range 0 to 99. By adding 1, n
will always be in the range from
1 to 100.
Dr. Yousaf, PIEAS

Sorting an Array
// Sorting an array in ascending
//order
#include <stdio.h>
int main()
{
int s, i ,j, temp, a[20];
printf("Enter total elements: ");
scanf("%d",&s);
printf("Enter %d elements: ",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);

for(i=0;i<s;i++)
{
for(j=i+1; j<s; j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("After sorting: \n");
for(i=0; i<s; i++)
printf(" %d",a[i]);
getchar(); return 0;
}

Dr. Yousaf, PIEAS

To Generate Fibonacci Series


The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3,
5, 8, 13, 21, 34, ... The next number is found by adding up
the two numbers before it. The 2 is found by adding the
two numbers before it (1+1)
fib[0] = 0;
#include<conio.h>
fib[1] = 1;
#include<stdio.h>
for(j=2;j<nterms;j++)
int main () {
int fib[50], nterms,j;
fib[j] = fib[j-2] + fib[j-1];
printf("This program generates
printf("\n Feibinacci series
Fibonacci series upto 20
is:\n\n");
terms\n");
for(j=0;j<nterms;j++)
printf("Please enter the number
printf("%d\t", fib[j]);
of terms: ");
getch(); return 0;
scanf("%d", &nterms);
}
Dr. Yousaf, PIEAS

Das könnte Ihnen auch gefallen