Kindly try to find the the errors in the following programs:
Firstly the difficulties then the progrograms are given respectively:-
Difficulties::
1. Array of structure
No Error
Wrong out put
2. To find the area of figures of Rectangle,Circle,Triangle using function overloading
No Error
No output
3.Create a class student with register no;name;age and member function getdata(),displaydata().Get the information of 2 students.
No Error
wrong result
4.Constructor Overloading
To print period of investment with total amount using Constructor Overloading
No Error
Wrong output
5. Linear Search in Array
To perform Linear search in an Array
No Error
6. Binary Search in Array
perform Binary search in an Array
4 Errors,2 Warnings in line 12,16,26,26,37,37,
7.Insertion in Array
To insert an element in existing Array
1 Error in line 13
8.Deletion in Array
To delete an element in an Array
No Error
Program No.1:
#include<iostream.h>
#include<conio.h>
struct student
{
int rno;
char name[20];
float mark;
}s[5];
void main()
{
clrscr();
cout<<"Enter the r no\nEnter the name\nEnterthe mark";
for(int i=1;i<=5;i++)
{
cin>>s[i].rno>>s[i].name>>s[i].mark;
}
cout<<"The no is\nThe name is\nThe mark is";
for(i=1;i<=5;i++)
{
cout<<s[i].rno<<s[i].name<<s[i].mark;
}
getch();
}
Program No.2:
#include<iostream.h>
#include<conio.h>
#include<math.h>
float area(float a,float b,float c)
{
float s,ar;
ar=sqrt(s*(s-a)*(s-b)*(s-c));
return ar;
}
float area(float a,float b)
{
return a*b;
}
float area(float a)
{
3.14*a*a;
return a;
}
void main()
{
clrscr();
int choice,s1,s2,s3,ar;
char ch;
do
{
cout<<"\nArea Main Menu\n";
cout<<"1.Triangle\n";
cout<<"2.Rectangle\n";
cout<<"3.Circle\n";
cout<<"Enter your choice";
cin>>ch;
switch(choice)
{
case 1:cout<<"Enter the three sides\n";
cin>>s1>>s2>>s3;
ar=area(s1,s2,s3);
cout<<"The area is"<<ar<<"\n";
break;
case 2:cout<<"Enter length and breath\n";
cin>>s1>>s2;
ar=area(s1,s2);
cout<<"The area is"<<ar<<"\n";
break;
case 3:cout<<"Enter the radius\n";
cin>>s1;
ar=area(s1);
cout<<"The area is"<<ar<<"\n";
break;
case 4:break;
default:cout<<"Wrong choice!!\n";
}
}while(choice>0&&choice<4);
getch();
}
Program No.3:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student
{
int rno;
char name[20];
int age;
public:
void getdata();
void display();
};
void student::getdata()
{
cout<<"Enter your roll no";
cin>>rno;
cout<<"Enter your name";
gets(name);
cout<<"Enter your age";
cin>>age;
}
void student::display()
{
cout<<rno;
puts(name);
cout<<age;
}
void main()
{
clrscr();
student s,s1;
s.getdata();
s.display();
s1.getdata();
s1.display();
getch();
}
Program No.5:
#include<iostream.h>
#include<conio.h>
int Lsearch(int[],int,int);
void main()
{
int AR[50],item,N,index;
cout<<"Enter dasired array size(max.50)...";
cin>>N;
cout<<"\nEnter Array Elements";
for(int i=0;i<N;i++)
{
cin>>AR[i];
}
cout<<"\nEnter Element to be searched for...";
cin>>item;
index=Lsearch(AR,N,item);
if(index==-1)
cout<<"\nSorry!!Given element could not be found";
else
cout<<"\nElementfound at index:"<<index<<",Position:"<<index+1<<endl;
}
int Lsearch(int AR[],int size,int item)
{
for(int i=0;i<size;i++)
{
if(AR[i]==item)
return i;
}
return-1;
}
Program No.6:
#include<iostream.h>
#include<conio.h>
#include<process.h>
int Bsearch(int AR,int size,int item)
{
int beg,last,mid;
beg=0;
last=size-1;
while(beg<=last)
{
mid=(beg+last)/2;
if(item==AR[mid])
{
return mid;
}
else if(item>AR[mid])
{
beg=mid+1;
}
else
{
last=mid-1;
}
}
return-1;
}
void main()
{
int AR[50],item,N,index;
cout<<"Enter desired array size(max.50)...";
cin>>N;
cout<<"\nEnter Array Elements(must be sorted in Asc order)\n";
for(int i=0;i<N;i++)
cin>>AR[i];
cout<<"\nEnter Element to be searched for....";
cin>>item;
index=Bsearch(AR,N,item);
if(index==-1)
cout<<"\nSorry!Given element could not be found";
else
cout<<"\nElement found at index:"<<index<<",Position:"<<index+1<<endl;
}
Program No.7:
#include<iostream.h>
#include<conio.h>
#include<process.h>
int findPos(int AR[],int n,int item)
{
int Pos;
if(item<AR[0])
Pos=0;
else
{
for(int i=0;i<n;i++)
{
if(AR[i]<=item&&AR[i]<=item[i+1])
{
Pos=i+1;
break;
}
}
if(i==n-1)
Pos=n;
}
return Pos;
}
void main()
{
int AR[50],item,n,index;
cout<<"How many elements do you want to creat array with?(max.50)...";
cin>>n;
cout<<"\nEnter Array elements(must be sorted in ascending order)/n";
for(int i=0;i<n;i++)
cin>>AR[i];
char ch='y';
while(ch=='y'||ch=='Y')
{
cout<<"\nEnter Element to be inserted...";
cin>>item;
if(n==50)
{
cout<<"\nOverflow!!\n";
exit(1);
}
index=findPos(AR,n,item);
for(i=n;i>index;i--)
{
AR[i]=AR[i-1];
}
AR[index]=item;
n+=1;
cout<<"\nWant to insert more element";
cin>>ch;
}
cout<<"The array now is as shown below...";
for(i=0;i<n;i++)
cout<<AR[i]<<" ";
cout<<endl;
getch();
}
Program No.8:
#include<iostream.h>
#include<conio.h>
#include<process.h>
int Lsearch(int[],int,int);
int main()
{
int AR[50],ITEM,N,index;
cout<<"How many elements do you want to create array with";
cin>>N;
cout<<"\nEnter array elements...\n";
for(int i=0;i<N;i++)
cin>>AR[i];
char ch='y';
while(ch=='Y'||ch=='y')
{
cout<<"\nEnter element to be deleted...";
cin>>ITEM;
if(N==0)
{
cout<<"Underflow!!!\n";
exit(1);
}
index=Lsearch(AR,N,ITEM);
if(index!=-1)
AR[index]=0;
else
cout<<"Sorry!!No such elements in the array\n";
cout<<"\nThe array is as shown below...\n";
cout<<"Zero(0)signified deleted elements\n";
cout<<"AR[i]"<<" ";
cout<<endl;
cout<<"After this emptiedsquare will be shifted\n";
for(i=index;i<N;i++)
{
AR[i]=AR[i+1];
}
N-=1;
cout<<"\nWant to delete more elements...?";
cin>>ch;
}
cout<<"The array after shifting is...\n";
for(i=0;i<N;i++)
cout<<AR[i]<<" ";
cout<<endl;
return 0;
}
int Lsearch(int[],int size,int item)
{
int Arr[50];
for(int i=0;i<size;i++)
{
if(Arr[i]==item)
return i;
}
return-1;
}