Oop PR4

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Practical No.

4: Program to implement One Dimension Array

Practical Related Questions


1. State output of the following code :
Code:
#include<iostream.h>
int main()
{
int n,i;
float num[100],sum=0.0,average;
cout<<"Enter the numbers of data:";
cin>>n;
while(n>100 || n<=0)
{
cout<<"Error! number should be in range of (1 to 100)."<<endl;
cout<<"Enter the number again:";
cin>>n;
}
for(i=0;i<n;++i)
{
cout<<i+1<<".Enter number: ";
cin>>num[i];
sum+=num[i];
}
average = sum/n;
cout<<"Average = "<<average;
return 0;
}
Output:
2. Which of the following correctly declares an array?
Ans) a) int array[10];
3. Can we change the size of an array at run time ?
Ans) No
4. What is the default value of Array ?
Ans) the default value for an integer array is 0.
5. Can we declare array size as a negative number?
Ans) No (array size should be declared in positive number or else it gives error)
6. What is the meaning of anonymous array? Explain with an example?
Ans) An array without any name is known as an anonymous array. Using an anonymous
array, we can pass an array of user inputs without the referenced variable.
7. Is there any difference between int[]a and int a[]?
Ans) Yes, int a[size] is the correct declaration for an array, whereas int[]a is an incorrect
method of array declaration.
8. How to copy an array into another array?
code:
#include<iostream.h>
#include<conio.h>
void main()
{
int A[100],B[100],i,size;
clrscr();
cout<<"Input the size of the array : ";
cin>>size;
cout<<"Enter the elements of first array: ";
for(i=0;i<size;i++)
{
cin>>A[i];
}
for(i=0;i<size;i++)
{
B[i]=A[i];
}
cout<<"1st array: ";
for(i=0;i<size;i++)
{ cout<<A[i]<<"\t";
}
cout<<endl<<"2nd array: ";
for(i=0;i<size;i++)
{ cout<<A[i]<<"\t";
}
getch();
}

Output:
9. Can you declare an array without assigning the size of an array?
Ans) No, we cannot declare an array without assigning the size of an array.

Exercise
1. Write a C++ program to find median of two sorted arrays of same size.
Code:

#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a1[100],a2[100],n1,n2,sum1=0,sum2=0,median1,median2,medianmax,sum,i;
cout<<endl<<"Output prepared by Aswhath Bhekare (CO3IB) 22203B0015";
cout<<endl<<"Enter the size of the 1st array: ";
cin>>n1;
cout<<endl<<"Enter elemnts of 1st array: ";
for(i=0;i<n1;i++)
{
cin>>a1[i];
sum1=sum1+a1[i];
}
cout<<"Enter the size of the 2nd array: ";
cin>>n2;
cout<<endl<<"Enter elemnts of 2nd array: ";
for(i=0;i<n2;i++)
{
cin>>a2[i];
sum2=sum2+a2[i];

}
cout<<endl<<"Elements of 1st array are: ";
for(i=0;i<n1;i++)
{
cout<<a1[i]<<"\t";
}
cout<<endl<<"Elements of 2nd array are: ";
for(i=0;i<n2;i++)
{
cout<<a2[i]<<"\t";
}

median1=sum1/n1;
median2=sum2/n2;
medianmax=(median1+median2)/2;
cout<<endl<<"Median of two sorted array is: "<<medianmax;
getch();
}

Output:
2. Write a C++ program to find the two repeating elements in a given array.
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a1[50], no,i,j;
cout<<"Output prepared by Aswhath Bhekare (CO3IB) 22203B0015";
cout<<endl<<"Enter size of the array:";
cin>>no;
cout<<endl<<"Enter array elements:";
for(i=0;i<no;i++)
{
cin>>a1[i];
}
cout<<endl<<"Repeated elements in the array are:";
for(i=0;i<no;i++)
{
for(j=i+1;j<no;j++)
{
if (a1[i]==a1[j])
{
cout<<a1[j]<<" \t";
}
}
}
getch();
}
Output:
3. Find the smallest and second smallest elements in an array.
Code:

#include<iostream.h>
#include<conio.h>
void main()
{
int a[50],no,small,sm,i,j;
clrscr();
cout<<endl<<"Output prepared by Ashwath Bhekare (CO3IB) 22203B0015";
cout<<endl<<"Enter size of the array: ";
cin>>no;
cout<<endl<<"Enter array elements: ";
for(i=0;i<no;i++)
{
cin>>a[i];
}
cout<<endl<<"Array elements: ";
for(i=0;i<no;i++)
{
cout<<a[i]<<"\t";
}
small=a[0];
sm=small;
for(i=0;i<no;i++)
{
for(j=0;j<no;j++)
{
if(a[j]<a[i]&&a[j]!=small)
{
sm=a[j];
}
if(a[j]<small)
{
small=a[j];
}
}

}
cout<<endl<<"Smallest element: "<<small<<endl<<"Second Smallest
element:"<<sm;
getch();
}
Output:

4. Complete the given table:


5.

Program Code Write & justify Output


a) #include<iostream.h> 80
int
array1[]={1200,200,2300,1230,1543};
int array2[]={12,14,16,18,20};
int temp,result=0;
void main()
{
for (temp=0;temp<5;temp++)
{
result+=array2[temp];
}
cout<<result;
}
b) #include<iostream.h> 27
void main()
{
int array[]={0,2,4,6,7,5,3};
int n,result=0;
for(n=0;n<8;n++)
{
result+=array[n];
}
cout<<result;
}
c) #include<iostream.h>
void main()
{
char str[5]= "ABC";
cout<<str[3];
cout<<str;
}

You might also like