Vous êtes sur la page 1sur 8

Manual # 8

Topic:
Arrays (Single, 2-D) + Matrix Systems

Subject:
Introduction to Computing

Author:
Engr. Ali Faisal Murtaza

1
------------------------------------------------------------------------------------------------------------
#include<iostream.h>

int main()
{
int a[1];

cin>>a[1];

cout<<a[1];

cout<<"\n\n";

return 0;
}

Note the output of the above program, you`ll get the error.

#include<iostream.h>

int main()
{
int a[1];

cin>>a[0];

cout<<a[0];

cout<<"\n\n";

return 0;
}
------------------------------------------------------------------------------------------------------------
You have to give the fix size of an array otherwise you`ll get error.

#include<iostream.h>

int main()
{
int a[];

cout<<"Hello";

cout<<"\n\n";

2
return 0;
}

#include<iostream.h>

int main()
{
int a[10];

cout<<"Hello";

cout<<"\n\n";

return 0;
}
------------------------------------------------------------------------------------------------------------
Q1: Write a program which will take ten values from the user and display the average of
these ten values.
------------------------------------------------------------------------------------------------------------
Q2: Fifteen Elements are entered from the keyboard into an array. Write a program to
find out how many of them are positive, how many are negative, how many are even, and
how many are odd.
------------------------------------------------------------------------------------------------------------
Write a program to enter and display the matrix.

#include<iostream.h>

int main()
{
int i[2][2],k,l;

for(k=0;k<3;k++)
{
for(l=0;l<3;l++)
{
cin>>i[k][l];
}
}

for(k=0;k<3;k++)
{
for(l=0;l<3;l++)
{
cout<<i[k][l];
cout<<" ";
}

3
cout<<"\n";
}

cout<<"\n\n";

return 0;
}

Note: Error in this program because we declare an array of 2 elements and we are trying
to save three elements in it through loops.

#include<iostream.h>

int main()
{
int i[3][3],k,l;

for(k=0;k<3;k++)
{
for(l=0;l<3;l++)
{
cin>>i[k][l];
}
}

cout<<"\n\n";

for(k=0;k<3;k++)
{
for(l=0;l<3;l++)
{
cout<<i[k][l];
cout<<" ";
}
cout<<"\n";
}

cout<<"\n\n";

return 0;
}

Input can also be done as:

#include<iostream.h>

4
int main()
{
int i[3][3],k,l,j;

for(k=0;k<3;k++)
{
for(l=0;l<3;l++)
{
cin>>j;
i[k][l]=j;
}
}

cout<<"\n\n";

for(k=0;k<3;k++)
{
for(l=0;l<3;l++)
{
cout<<i[k][l];
cout<<" ";
}
cout<<"\n";
}

cout<<"\n\n";

return 0;
}
------------------------------------------------------------------------------------------------------------
Addition of any two 3 x 3 matrices.

#include<iostream.h>

int main()
{
int i[3][3],j[3][3],x[3][3],k,l;

for(k=0;k<3;k++)
{
for(l=0;l<3;l++)
{
cin>>i[k][l];
}
}

5
cout<<"\n\n";

for(k=0;k<3;k++)
{
for(l=0;l<3;l++)
{
cin>>j[k][l];
}
}

cout<<"\n\n";

for(k=0;k<3;k++)
{
for(l=0;l<3;l++)
{
cout<<i[k][l];
cout<<" ";
}
cout<<"\n";
}

cout<<"\n\n";

for(k=0;k<3;k++)
{
for(l=0;l<3;l++)
{

cout<<j[k][l];
cout<<" ";
}
cout<<"\n";
}

cout<<"\n\n";

for(k=0;k<3;k++)
{
for(l=0;l<3;l++)
{
x[k][l]=i[k][l]+j[k][l];
cout<<" ";
}
cout<<"\n";
}

6
for(k=0;k<3;k++)
{
for(l=0;l<3;l++)
{
cout<<x[k][l];
cout<<" ";
}
cout<<"\n";
}

cout<<"\n\n";

return 0;
}
------------------------------------------------------------------------------------------------------------
Q3: Do Multiplication, Subtraction & Division. (In Division, float variables may be
used).
------------------------------------------------------------------------------------------------------------
Q4: Write a program to find cofactor of any 3 x 3 matrix. And then find the adjoint of
matrix by taking transpose of the matrix.

7
------------------------------------------------------------------------------------------------------------
Q5: Write a program, to pick up the largest number from 5 x 5 matrix.
------------------------------------------------------------------------------------------------------------
Q6: Write a program to obtain transpose of a 4 x 4 matrix. The transpose of a matrix is
obtained by exchanging the elements of each row with the elements of the corresponding
columns.
------------------------------------------------------------------------------------------------------------
Q7: Write a program to add three 6 x 6 matrices.
------------------------------------------------------------------------------------------------------------
Q8: Write a program to multiply any two 3 x 3 matrices.
------------------------------------------------------------------------------------------------------------
Q9: Write a program to sort all elements of any matrix.
------------------------------------------------------------------------------------------------------------
Q10: Write a program to find out the determinant value of 5 x 5 matrix.
------------------------------------------------------------------------------------------------------------

Vous aimerez peut-être aussi