Sie sind auf Seite 1von 7

ARRAY PROGRAM FILE

Q. 1O
#include<iostream.h>
#include<conio.h>
void main()
{
int ar[]={1,2,3,4,5,6,7,8,9};
int t=0;
t=ar[8];
ar[8]=ar[0];
ar[0]=t;
cout<<"Exchanged values are"<<ar[0]<<" "<<ar[8];
getch();
}
Q.11
#include<iostream.h>
#include<conio.h>
void main()
{
int
int p[]={33,52,546,33,24,11,423,44,32,42,42,34,534,53};
cout<<"Enter the no. to be searched";
int n,c=0;
cin>>n;
cout<<"No. is present at";
for(int i=0;i<9;i++)
{
if(n==ar[i])
{ cout<<i<<",";c++;}
}
(c==0)?cout<<"No. is not present in the array":cout<<"No. is present in the array "<<c<<"times";
getch();
}
Q.12
#include<iostream.h>
#include<conio.h>
void main()
{
int ar[]={1,2,3,234,56,67,434,243,123};
int l=ar[0],s=ar[0];
for(int i=0;i<9;i++)
{
if(ar[i]>l)
l=ar[i];
if(ar[i]<s)
s=ar[i];
}
cout<<"Largest and smallest elements of array are "<<l<<'\t'<<s<<"respectively";
getch();
}
Q.13
#include<iostream.h>
#include<conio.h>
void main()
{
int ar[]={1,2,3,4,5,6,7,8,9,10};
cout<<"Elements placed at even positions are";
for(int i=0;i<10;i++)
{
if(i%2==0)cout<<ar[i];
}
getch();
}

Q.14
#include<iostream.h>
#include<conio.h>
void main()
{
char ch[20];int v=0,w=0,sp=0;
cout<<"Enter a string";
cin>>ch;
for(int i=0;i<20;i++)
{
if(ch[i]=='a'||ch[i]=='e'||ch[i]=='i'||ch[i]=='o'||ch[i]=='u')v++;
if(ch==' '){
w++;sp++;}
}
cout<<"No. of vowels ="<<v<<"\t No. of words="<<w<<"\t No. of spaces="<<sp;
getch();
}

Q.21
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
clrscr( );
char str1[80],str2[80];
cout<<"Enter first string :";
gets(str1);
cout<<"Enter second string :";
gets(str2);
for(int l=0;str2[l]!='\0';l++);

//finding length of string 2

for(int i=0,j=0;str1[i]!='\0'&& str2[j]!='\0';i++)


if(str1[i]==str2[j])
j++;
else
j=0;
if(j==l)
cout<<"Substring found at position "<<i-j+1;
else
cout<<"Substring not found";
getch();
return 0;
}

Q.22
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n, a[10], b[10], count = 0, c, d;
printf("Enter number of elements:");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for(c=0;c<n;c++)
scanf("%d",&a[c]); //Getting the array elements
for(c=0;c<n;c++) //For removing duplicate elements
{
for(d=0;d<count;d++)
{
if(a[c]==b[d])
break;
}
if(d==count)
{
b[count] = a[c];
count++;
}
}
printf("Array after removing duplicate elements\n");
for(c=0;c<count;c++)
printf("%d\t",b[c]);
system("pause");
return 0;
}

Q. 23
#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
int a[100],i,pos,num,item,size;
cout<<"How many element: ";
cin>>size;
cout<<"Enter the elements of array: "<<endl;
for(i=0;i<size;i++)
cin>>a[i];
cout<<endl;
cout<<"Now Array is: "<<endl;
for(i=0;i<size;i++)
cout<<"Element at position "<<i+1<<" is "<<a[i]<<endl;
cout<<endl<<"Press any key to add another element"<<endl<<endl;
getch();
cout<<"Enter another element: ";
cin>>num;
cout<<"Enter position number: ";
cin>>pos;

cout<<endl;
--pos;
for(i=size;i>=pos;i--)
a[i+1]=a[i];
a[pos]=num;
size++;
cout<<"New Array: "<<endl;
for(i=0;i<size;i++)
cout<<"Element at position "<<i+1<<" is "<<a[i]<<endl;
cout<<endl<<"Press any key to delete any element"<<endl<<endl;
getch();
cout<<"Enter position number to delete: ";
cin>>pos;
--pos;
item=a[pos];
for(i=pos;i<size;i++)
a[i]=a[i+1];
size--;
cout<<"Deleted element: "<<item<<endl;
cout<<endl<<"New array: "<<endl;
for(i=0;i<size;i++)
cout<<"Element at position "<<i+1<<" is "<<a[i]<<endl;
getch();
}

Q.24
#include <iostream.h>
#include <stdio.h>
#include <ctype.h>
main()
{
char c;
int i = 0, lc = 0, uc = 0, dig = 0, ws = 0, pun = 0, oth = 0;
cout << "Please enter a character string and then press ENTER: ";
// Analyse text as it is input:
while ((c = getchar()) != '\n')
{
if (islower(c))
lc++;
else if (isupper(c))
uc++;
else if (isdigit(c))
dig++;
else if (isspace(c))
ws++;
else if (ispunct(c))
pun++;
else

oth++;
}
// display the counts of different types of characters
cout << "You typed:"<< endl;
cout<< "lower case letters = "<< lc<< endl;
cout << "upper case letters = " << uc <<endl;
cout<< "digits = " << dig << endl;
cout<< "white space = "<< ws << endl;
cout<< "punctuation = "<< pun<< endl;
cout<< "others = "<< oth;
}

Q.15
#include<iostream.h>
Void main()
{int list[30];
Int small,pos,temp,l,j,n;
cout<<enter list;
cin>>n;
cout<<enter the size of the list;
for(i=0;i<n;i++)
{ small=list[j];
Pos=I;
For(j=i+1;j<n;j++)
{if(small>list[j];
{small=list[j]
Pos=j;
}
}
Temp=list[i];
List[i]=list[pos];
List[pos]=temp;
}
Cout<<the sorted list is;
For(i=0;i<n;i++;)
Cout<<list[i];
}

Q.16
#include<iostream>
void main()
using namespace std;
{
int x[50], i, j, n, flag, tempo;
cin << n;
for (i = 0; i < n; i++)
cin << x[i];
flag = 0;
i = 0;
while ((i < n - 1) && (!flag))
{
flag = 1;
i++;
for (j = 0; j < n - i; j++)
{
if (list[j]>list[j + 1])
{
temp = list[j];
list = list[j + 1];
list[j + 1] = temp;
}
}
}
cout << "the sorted list is";
for (i = 0; i < n;i++)
cout << list[i]
}
Q.17

#include<iostream.h>
Void main()
{
int list[30];
Int i, k, j, n;
cout << enter list;
cin >> n;
cout << enter the size of the list;
cin >> n;
for (i = 1; i < n; i++)
cin >> list[i];
for (i = 1; i < n; i++)
{
temp = list[i];
j = i - 1;
while ((temp < list[j]) && (j >= 0))
{
list[j + 1] = list[j];
j = j - 1;
}
list[j + 1] = temp;
}
cout << "the sorted list is";
for (i = 0; i < n; i++)
cout << list[i];
}

Q.18
#include<iostream>
void main()
{
int a[20], b[20], c[40];
int m, n, ptr1, ptr2, ptr3, i = 0;
cout << "enter size of array a and b";
cin >> n >> m;
for (i = 0; i > n; i++)
cin >> a[i];
for (i = 0; i > m; i++)
cin >> b[i];
ptr1 = ptr2 = ptr3 = 0;
while ((ptr1 < n) && (ptr2 < m))
{
if (a[ptr1] < b[ptr2])
c[ptr3++] = a[ptr1++];
else
c[ptr3++] = b[ptr2++];
}
while (ptr1 < n)
c[ptr3++] = a[ptr++];
while (indexB < n)
c[ptr3++] = b[ptr2++];
cout << "the final merged list is";
for (i = 0; i < m + n; i++)
cout << c[i];
}
Q.19
#include<iostream.h>
#include<conio.h>
void main()
{
int i=0,j=0;
char str1[20];
char str2[20];
cout<<"Enter both the strings";
cin>>str1>>str2;
while(str1[i]!='\0') i++;
int l1=i+1;
while(str2[j]!='\0')j++;

int l2=j+1;
if(l1==l2)cout<<"string 1 is equal to string 2";
else if(l1>l2)cout<<"string 1 is bigger than string 2";
else cout<<"string 2 is biger than string 1";
getch();
}

Das könnte Ihnen auch gefallen