c++ program difficulties

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.4:
#include<iostream.h>
#include<conio.h>
class Deposit
{
long int principal;
int time;
float rate;
float total_amt;
public:
Deposit();
Deposit(long p,int t,float r);
Deposit(long p,int t);
Deposit(long p,float r);
void calc_amt(void);
void display(void);
};
Deposit::Deposit()
{
principal=time=rate=0.0;
}
Deposit::Deposit(long p,int t,float r)
{
principal=p;
time=t;
rate=r;
}
Deposit::Deposit(long p,int t)
{
principal=p;
time=t;
rate=0.08;
}
Deposit::Deposit(long p,float r)
{
principal=p;
time=2;
rate=r;
}
void Deposit::calc_amt(void)
{
total_amt=principal+(principal*time*rate)/100;
}
void Deposit::display(void)
{
cout<<"\nPrincipal Amount"<<principal;
cout<<"\nPeriod of Investment:"<<principal;
cout<<"\nRate of Interest"<<rate;
cout<<"\tTotal Amount:Rs"<<total_amt<<"\n";
}
void main()
{
clrscr();
Deposit D1,D2(2000,2,0.07f),D3(4000,1),D4(3000,0.12f);
D1.calc_amt();
D2.calc_amt();
D3.calc_amt();
D4.calc_amt();
cout<<"Object1\n";D1.display();
cout<<"Object2\n";D2.display();
cout<<"Object3\n";D3.display();
cout<<"Object4\n";D4.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;
}












Put in code tags please, makes it easier to see and read
Topic archived. No new replies allowed.