Array Program
Single (One) Dimensional Array:
1. Read and display an array(set of numbers):
// Read and display an array
#include <iostream>
using namespace std;
int main() {
int n, a[20], i;
cout<<"Enter size of array";
cin>>n;
cout<<"Enter Array values"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Array values are"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<"\n";
}
return 0;
}
Input:
Enter size of array5
Enter Array values
10 30 50 40 20
Output:
Array values are
10
30
50
40
20
2.) Sum of given numbers:
#include <iostream>
using namespace std;
int main()
{ int n, a[20],s=0,i;
cout<<"Enter the size of array";
cin>>n;
cout<<"Enter the elements";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Display the elements";
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
s=s+a[i];
}
cout<<"Sum of given elements"<<s;
return 0;
}
Input:
Enter the size of array4
Enter the elements10
20
30
40
Output:
Display the elements10
20
30
40
Sum of given elements100
3) Maximum of given array
// Maximum of given array
#include <iostream>
using namespace std;
int main() {
int n, a[20], i, max=0;
cout<<"Enter size of array";
cin>>n;
cout<<"Enter Array values"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Array values are"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<"\n";
}
max=a[1];
for(i=0;i<n;i++)
{
if (a[i]>max)
{
max=a[i];
}
}
cout<<"Maximum of given array:"<<max;
return 0;
}
Input:
Enter size of array7
Enter Array values
20 40 80 70 100 10 60
Output:
Array values are
20
40
80
70
100
10
60
Maximum of given array:100
4) Linear Search:-
// Linear search
#include <iostream>
using namespace std;
int main() {
int n, a[20], i, x,t;
bool r=false;
cout<<"Enter size of array";
cin>>n;
cout<<"Enter Array values"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter search element";
cin>>x;
for(i=0;i<n;i++)
{
if (a[i]==x)
{ r=true;
t=i;
break;
}
}
if (r)
cout<<"Search element is found at "<<t+1<<" position";
else
cout<<"Search element is not found";
return 0;
}
Input:
Enter size of array5
Enter Array values
20 50 80 40 10
Enter search element40
Output:
Search element is found at 4 position
b)Two dimensional Array:
1)Read and display two dimensional array
// Read and display two dimensional array
#include <iostream>
using namespace std;
int main() {
int r, c, a[5][5], i, j;
cout<<"Enter row and column of matrix a";
cin>>r>>c;
cout<<"Enter the values for matrix a"<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>a[i][j];
}
}
cout<<"Display the values of matrix a:"<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<a[i][j]<<endl;
}
}
return 0;
}
Input
Enter row and column of matrix a3 3
Enter the values for matrix a
10
20
30
20
78
53
23
12
32
Output:
Display the values of matrix a:
10
20
30
20
78
53
23
12
32
2) Matrix Addition
// Matrix Addition
#include <iostream>
using namespace std;
int main() {
int r1, c1, r2, c2, a[3][3], b[3][3],c[3][3], i, j;
cout<<"Enter row and column of matrix a";
cin>>r1>>c1;
cout<<"Enter the values for matrix a"<<endl;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cin>>a[i][j];
}
}
cout<<"Display the values of matrix a:"<<endl;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<"Enter row and column of matrix b";
cin>>r2>>c2;
cout<<"Enter the values for matrix b"<<endl;
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
cin>>b[i][j];
}
}
cout<<"Display the values of matrix b:"<<endl;
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
cout<<b[i][j]<<" ";
}
cout<<endl;
}
cout<<"Matrix Addition is"<<endl;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{c[i][j]= a[i][j]+b[i][j];
cout<<c[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Input:
Enter row and column of matrix a3 2
Enter the values for matrix a
1
2
3
4
5
6
Display the values of matrix a:
12
34
56
Enter row and column of matrix b3 2
Enter the values for matrix b
5
3
2
4
1
7
Display the values of matrix b:
53
24
17
Output:
Matrix Addition is
65
58
6 13
3) Trace or Sum the diagonals of Matrix
// Sum the diagonal of Matrix
#include <iostream>
using namespace std;
int main() {
int r, c, a[3][3], i, j, d=0;
cout<<"Enter row and column of matrix a";
cin>>r>>c;
cout<<"Enter the values for matrix a"<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>a[i][j];
}
}
cout<<"Display the values of matrix a:"<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<a[i][j]<<" ";
if (i==j)
{
d=d+a[i][j];
}
}
cout<<endl;
}
cout<<"The sum of the diagonals of matrix i"<<d;
return 0;
}
Input:
Enter row and column of matrix a3 3
Enter the values for matrix a
1
2
3
4
5
6
7
8
9
Output:
Display the values of matrix a:
123
456
789
The sum of the diagonals of matrix i15
4) Matrix Multiplication
// Matrix Multiplication
#include <iostream>
using namespace std;
int main() {
int r1, c1, r2, c2, a[3][3], b[3][3], c[3][3], i, j, k;
cout<<"Enter row and column of matrix a";
cin>>r1>>c1;
cout<<"Enter the values for matrix a"<<endl;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cin>>a[i][j];
}
}
cout<<"Display the values of matrix a:"<<endl;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<"Enter row and column of matrix b";
cin>>r2>>c2;
cout<<"Enter the values for matrix b"<<endl;
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
cin>>b[i][j];
}
}
cout<<"Display the values of matrix b:"<<endl;
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
cout<<b[i][j]<<" ";
}
cout<<endl;
}
cout<<"Matrix Multiplication is"<<endl;
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<r2;k++)
{
c[i][j]= c[i][j]+a[i][k]*b[k][j];
}
cout<<c[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Input:
Enter row and column of matrix a2 2
Enter the values for matrix a
1
1
1
1
Display the values of matrix a:
11
11
Enter row and column of matrix b2
2
Enter the values for matrix b
1
1
1
1
Display the values of matrix b:
11
11
Output:
Matrix Multiplication is
22
22
5) Transpose of a Matrix
// Transpose of a Matrix
#include <iostream>
using namespace std;
int main() {
int r, c, a[3][3], i, j;
cout<<"Enter row and column of matrix a";
cin>>r>>c;
cout<<"Enter the values for matrix a"<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>a[i][j];
}
}
cout<<"Display the values of matrix a:"<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<"Transpose of a matrix"<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
a[i][j]=a[j][i];
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Input:
Enter row and column of matrix a3 3
Enter the values for matrix a
1
2
3
4
5
6
7
8
9
Output:
Display the values of matrix a:
123
456
789
Transpose of a matrix
147
458
789
Strings/Character array programs:
1) Counting number of vowels and consonants:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{int v=0, c=0,d=0,b=0,i;
char s[20];
cout<<"Enter the string\t";
[Link](s,20);
for(i=0; s[i]!='\0';i++)
{
if (s[i]=='a' || s[i]=='e' || s[i]=='i'||s[i]=='o'||s[i]=='u' || s[i]=='A' || s[i]=='E' ||
s[i]=='I'||s[i]=='O'||s[i]=='U')
v++;
else if((s[i]>='a'&& s[i]<='z') || (s[i]>='A'&& s[i]<='Z'))
c++;
else if (s[i]>='0' && s[i]<='9')
d++;
else
b++;
}
cout<<"Number of vowels:"<<v<<endl;
cout<<"Number of consonants:"<<c<<endl;
cout<<"Number of digits:"<<d<<endl;
cout<<"Number of spaces:"<<b<<endl;
return 0;
}
Input:
Enter the string welcome 123 all
Output:
Number of vowels:4
Number of consonants:6
Number of digits:3
Number of spaces:2
2) String Manipulation
#include <iostream>
#include <cstring>
using namespace std;
int main()
{int l, r, k, i;
char s1[20], s2[20],s3[20], *p, t;
cout<<"String Manipulation"<<endl;
cout<<"-------------------"<<endl;
cout<<"Enter the string:\t";
[Link](s1,20);
l=strlen(s1);
cout<<"Length of the string:\t"<<l<<endl;
strcpy(s3,s1);
cout << "The copied string is: " <<s3 << '\n';
cout<<"Enter second string:\t";
[Link](s2,20);
k=strcmp(s1,s2);
if (k==0)
{cout<<"strings are equal\n";}
else
{cout<<"strings are not equal\n";}
strcat(s2,s1);
cout<<"Concatenated string is\t "<<s2<<endl;
p=strstr(s2,s1);
if (p)
cout<<"string\t"<<s1<<"\tis present in\t"<<s2<<"\tat position\t"<<p-s2<<endl;
else
cout<<"string\t"<<s1<<"\tis not present in\t"<<s2<<endl;
cout<<"Enter the character to be searched\t";
cin>>t;
p=strchr(s2,t);
cout<<"The searched character is present at position\t"<< p-s2+1<<endl;
for(i=l;i<r;i++)
{
t=s1[i];
s1[i]=s1[r];
s1[r]=t;
r--;
}
cout<<"Reversed String\t"<<s1<<"\n";
if (strcmp(s1,s3)==0) //Compare two strings
cout<<"The string\t"<<s1<<" is palindrome";
else
cout<<"The string\t" << s1 <<"is not palindrome";
return 0;
}
String Manipulation
-------------------
Enter the string: madam
Length of the string: 5
The copied string is: madam
Enter second string: hello
strings are not equal
Concatenated string is hellomadam
string madam is present in hellomadam at position 5
Enter the character to be searched e
The searched character is present at position 2
Reversed String madam
The string madam is palindrome