Вы находитесь на странице: 1из 52

1.

Program to find the day of the week of the date entered by the user when
the day of the New Year is given or inputted.
#include<iostream.h> //inclusion of preprocessor directives
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main() //definition of main() function
{

char date[11],day[7][10]={"sunday","monday","tuesday","wednesday",
"thursday","friday","saturday"};
char wd[10]; //initialization of variables
int f,i,p,a[3],j=0,n,nd[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int d=0,m=1,b;
do //do-while loop
{ f=0;
cout<<"Date: ";gets(date);
for(i=0;date[i]!='\0';i++) //for loop
{ n=0;
while(date[i]!='/' && date[i]!='\0') //while loop
{ n=n*10+date[i]-48; i++; }
a[j++]=n;
}
if(a[2]%400==0||a[2]%4==0 && a[2]%100!=0) //if statement
nd[2]=29;
if(a[2]<1||a[1]<1||a[1]>12||a[0]<1||a[0]>nd[a[1]])
f=1;
}while(f==1); //do-while loop closes
do //do-while opens
{
cout<<"Day on 1st jan :";
gets(wd);
for(p=0;p<7;p++) //for loop
{ if(strcmp(wd,day[p])==0)//if statement
{ break;}
}
}while(p==7); //loop closes
p--;
do //do-while loop
{
d++;p++; //calculation
if(p==7)
p=0;
if(d>nd[m]) //if statement
{ d=1;m++;}
}while(d!=a[0]||m!=a[1]);
cout<<"Day on "<<date<<" :"<<day[p];
}
OUTPUT
Date: 6/6/2007
Day on 1st jan :monday
Day on 6/6/2007 :wednesday

ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. main() function declaration and definition.
4. Initialization and declaration of program variables.
5. do-while loops to calculate the days and dates to produce the
output.
6. Printing the output onto the screen.
7. main() function closes and termination of program.

VARIABLE LIST
1. date[] Char Array to store the days
2. days[][] Char To store the days in a
week
3. wd[] Char Acts as a sort of counter
of the days
4. a[] Int Array to act as counter
5. I Int Loop variable
6. f Int Counter
7. j Int Loop variable
8. nd[] Int Stores the number of
days of each month.
9. d Int Counter
10. m Int Counter
11. b Int Counter
12. p Int Loop variable

2.Program to solve linear equations through gauss elimination method


#include<iostream.h> //including preprocessor directives
#include<stdio.h>
#include<conio.h>
#include<math.h>
void gauss1(int n,float a[0][0],float b[10],float x[0]); //declaring
functions
void elim(int n,float a[10][10],float b[10]);
void bsub(int n,float a[10][10],float b[10],float x[10]);

void main() //main() definition


{
clrscr();
int status,n,I,j; //initialization of variables
float a[10][10],b[10],x[10];
cout<<”\nWhat is the size of the system(n)\n”;
cin>>n;
cout<<”\n Enter coefficients a(I,j),rowwise \n”;
for(I=1;I<=n;I++) //for loop
{
for(j=1;j<=n;j++) //for loop
cin>>a[I][j];
}
cout<<”Input vector b \n”; //accepting vector which is to
be solved
for(I=1;I<=n;I++) //for loop
cin>>b[I];

//obtain solution by gauss elimination method


gauss1(n,a,b,x); //calling function
cout<<”\n solution vector X \n”;
for(I=1;I,=n;I+) //for loop
cout<<x[I]<<”\t”;
getch();
}
void gauss1(intn,float a[10][10],float b[10],float x[10]) //defining function
{
elim(n,a,b);
bsub(n,a,b,x);
return;
}
void elim(int n,float a[10][10],float b[10]) //function definition
{
int I,j,k;
for(k=1;k<=(n-1);k++) //for loop
{
for(I=k+1;I<=n;I++) //for loop
{
factor=a[I][k]/a[k][k];
for(j=k+1;j,=n;j+) //for loop
{
a[I][j]=a[I][j]-factor*a[k][j];
}
b[I]=b[I]-factor*ab[k];
}
return;
}

void bsub(int n,float a[10][10],float b[10],float x[10]) //function


definition
{
int I,j,k; //initialization of variables
float sum;
x[n]=b[n]/a[n][n];
for(k=n-1;k>=1;k--) //for loop
{
sum=0.0;
for(j=k+1;j<=n;j++) //for loop
{ sum=sum+a[k][j]*x[j];
}
x[k]=(b[k]-sum)/a[k][k];
}
return;
}

OUTPUT
What is the size of the system(n)
3
Enter coefficients a(I,j)rowwise
2 1 3
4 4 7
2 5 9
Input vector b
1 1 3
Solution vector X
-0.5 -1 1

ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration of member functions.
4. Initialization and declaration of program variables.
5. Obtaining solution to the matrix entered by user through gauss
elimination method by calling member functions in the main()
function definition.
6. main() function closes and function definition starts.
7. Mechanism for obtaining solution through gauss elimination
method.
8. Program terminates along with termination of functions.

VARIABLE LIST
1. n Int Parameter variable
2. a[][] Float Parameter variable
3. b[] Float Parameter variable
4. x[] Float Parameter variable
5. status Int Counter
6. I Int Loop variable
7. j Int Loop variable
8. n Int Stores the size of the
system
9. k Int Loop variable
10. sum Float Stores the sum of
elements inside a loop.

3. Program to print the largest element of an array(using a function)


#include<iostream.h>
#include<conio.h> //including preprocessor directives
#include<stdio.h>
float large(float arr[],int n) /defining function
{float max=arr [0];
for(int j=1;j<n;j++)
{if(arr[j]>max) {max=arr [j]; }
}
return max;
}
int main() //main() definition
{
clrscr();
float large(float arr[],int n); //calling function
char ch;int i=0;
float amount[5],biggest; //declaration of variables
for(i=0;i<5;i++) //for loop
{
cout<<”Enter element number.”<<(i+1)<<”\n”;
cin>>amount[i];
cout<<”More elements?(y/n)”;
cin>>ch;
if(ch!=’y’) //if statement
break;
}
if(i<5)
i++;
Biggest=large(amount,i); //obtain the largest element
cout<<<”The largest element of the array is:”<<biggest<<”\n”;
return 0;
}

OUTPUT

Enter element number.


1. 2
2. 3
3. 8
4. 1
5. 4

More elements?(y/n)
N
The largest element of the array is: 8
ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration and definition of member functions.
4. Mechanism to obtain the largest element in a given array.
5. Function closes.
6. main() function definiton.
7. Initialization and declaration of program variables.
8. Obtaining the largest element of the array that is entered by the
user.
9. Printing the largest element.
10.main() function closes.
11.Program terminates along with termination of main() function.

VARIABLE LIST
1. n Int Parameter variable
2. arr[] Float Parameter variable
3. ch Char Stores the answer
entered by user
4. I Int Loop variable
5. amount[] Float Stores the array entered
by user
4. Program to find whether saddlepoint for a user-entered matrix exists or
not.

#include<iostream.h> //including preprocessor directives


#include<stdio.h>
#include<conio.h>
void main() //main() function definition
{
int A[10][10]; //initialization of variables
int i,j, m,n;
int max,min,c=0;
int b[2][10]={0};
cout<<"Enter the no .of rows:”; cin>>m;
cout<<"Enter the no of columns:”; cin>>n;
for(i=0;i<m;i++) //for loop
{
for(j=0;j<n;j++)
{
cin>>A[i][j]; //inputting each element
}
}
cout<<"The matrix is:"<<endl; //printing the matrix
for(i=0;i<m;i++) //for loop
{
for(j=0;j<n;j++)
{
cout<<A[i][j]<<"\t";
}cout<<endl;
}
for(i=0;i<m;i++) //for loop
{ min=A[i][0];
for(j=0;j<n;j++)
{if(A[i][j]<min) //calculating minimum of each
row
min=A[i][j];
}
b[0][c++]=min;

}
c=0;
for(j=0;j<n;j++) //for loop
{
max=A[0][j];
for(i=0;i<m;i++)
{
if(A[i][j]>max) //calculating maximum of each
column
{
max=A[i][j];
}
}
b[1][c++]=max; //storing maximum
}
max=b[0][0];
min=b[1][0];
for(i=0;i<m;i++) //for loop
{
for(j=0;j<n;j++)
{
if(i==0)
{
if(b[i][j]>max) //calculations
max=b[i][j];
}
if(i==1) //if statement
{
if(b[i][j]<min)
min=b[i][j];
}
}
}
if(max==min) //if statement
cout<<"Saddle point exists at "<<max;
else
cout<<"Saddle point doesn’t exist.";
getch();
}

OUTPUT
Enter the number of rows: 2
Enter the number of columns: 2

2 3
4 5
Saddle point exists at 4

ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration and definition of main() function.
4. Inputting the matrix on which the operation is to be done.
5. Calculating whether saddle point exists in the matrix or not.
6. Printing whether saddle point exists and if it exists, the position of
the point.
7. main() function closes.
8. Program terminates along with termination of main() function.

VARIABLE LIST
1. A[][] Int Array on which
operation is to be
performed.
2. I Int Loop variable
3. j Int Loop variable
4. m Int Stores the no of rows of
the matrix
5. n Int Stores the number of
columns of the matrix.
6. b[][] Int Stores the maximum
and minimum of each
row and column.
7. max Int Stores the maximum of
each column
8. min Int Stores the minimum of
each row.
9. c Int Counter.

5. Program to convert a 2-digit octal number into binary number and


print the binary equivalent.

#include<iostream.h> //including preprocessor directives


#include<stdio.h>
#include<conio.h>
void octobin(int oct) //function definition
{
long binn=0;
int a[6]; //initialization of variables
int d1,d2,q,r,i;
d1=oct/10;
d2=oct/10;
for(i=0;i<6;i++) //for loop
{
a[i]=0;
}
for(i=0;i,3;i++) //for loop
{
q=d1/2;
r=d1%2; //calculation
a[i]=r;
d1=q;
}
for(i=0;i<6;i++) //for loop
{
q=d2/2;
r=d2%2; //calculation
a[i]=r;
d2=q;
}
for(i=i-1;i>=0;i--) //for loop
{
binn*=10;
binn+=a[i];
}
cout<<endl<<”The binary equivalent is;”<<binn<<endl; //printing
the result
}
void main() //main() definition
{ int n;
cout<<"Enter an octal number.:";
cin>>n;
octobin(n); //calling function
getch();
}

OUTPUT
Enter an octal number.: 65
The binary equivalent is: 110101

ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration and definition of main() function.
4. Mechanism to convert from a given octal number to its binary
equivalent.
5. Function closes.
6. Definition of main() function.
7. inputting the octal number from the user and printing the binary
equivalent by callin function octobin().
8. main() function closes.
9. Program terminates along with termination of main() function.

VARIABLE LIST
1. oct Int Parameter variable
2. binn Long Stores the binary
equivalent.
3. a[] Int Counter.
4. d1 Int Stores the last digit of
the octal number
5. d2 Int Stores the first digit of
the octal number.
6. q Int Stores the quotient of
d1, d2 divided by 2.
7. r Int Stores the remainder of
d1, d2 divided by 2.
8. i Int Loop variable.

6.Program to find row sum and column sum of a matrix.

#include<iostream.h>
#include<conio.h> //including preprocessor directives
#include<stdio.h>
void main() //main() function
{
int a[2][2],rs[2],cs[2]; int i,j; //initialization of variables
cout<<"Enter elements of matrix ";
for(i=0;i<2;i++) //for loop
{
for(j=0;j<2;j++)
{
cin>>a[i][j]; //inputting each element
}
}

for(i=0;i<2;i++) //for loop


{ rs[i]=0;
for(j=0;j<2;j++)
{
rs[i]=rs[i]+a[i][j]; //calculating row sum
}
}
for(j=0;j<2;j++) //for loop
{
cs[j]=0;
for(i=0;i<2;i++) //calculating column sum
{
cs[j]+=a[i][j];
}
}

for(i=0;i<2;i++) //for loop


{
for(j=0;j<2;j++)
{
cout<<a[i][j]<<" ";
}cout<<rs[i]<<endl; //printing the matrix along with the
//summations
}
for(j=0;j<2;j++)
{
cout<<cs[j]<<" ";
}
getch();
}
OUTPUT
Enter elements of matrix:
2
3
2
5
2 3 5
2 5 7
4 8

ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration and definition of main() function.
4. Initialization and declaration of program variables.
5. Mechanism to find row sum and column sum of a matrix.
6. Printing the matrix along with the row sum and column sum.
7. main() function closes.
8. Program terminates along with termination of main() function.

VARIABLE LIST
1. a[][] Int Matrix on which
operation is to be
performed.
2. rs[] int Stores the row sum of
the matrix.
3. cs[] Int Stores the column sum
of the matrix..
4. I Int Loop variable.
5. j Int Loop variable
7.Program to find embedded or hidden primes in a number entered by the
user.

#include<iostream.h> //including preprocessor directives


#include<stdio.h>
#include<conio.h>

void main() //main() definition


{
int f[10]={0}; //initialization of variables
int a[10]={0};
int d=0,i=0,j=0,n=0,dig=0,r=0;
cout<<"Enter a number:";
cin>>n;
while(n>0) //while loop
{
d=n%10;
a[d]++;
n/=10;
}
for(i=9;i>=0;i--) //for loop
{
if(a[i]>0)
{
for(j=0;j<a[i];j++) //for loop
{
r=r*10+i;
}
}
}
for(i=2;i<=r;i++) //for loop
{
while(i>0) //while loop
{
dig=i%10;
f[dig]++;
i/=10;
}
for(j=0;j<10;j++) //for loop
{
if(f[j]>a[j]) //estimation of embedded primes
break;
}
}
cout<<"Embedded prime is :"<<i; //printing the result
getch(); }

OUTPUT
Enter a number :
5681
Embedded prime is : 5

ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration and definition of main() function.
4. Initialization and declaration of program variables.
5. Mechanism to find embedded prime in the number.
6. Printing the embedded prime.
7. main() function closes.
8. Program terminates along with termination of main() function.

VARIABLE LIST
1. a[] Int Stores the digits of the
number.
2. f[] int Stores the frequency of
each digit.
3. cs[] Int Stores the column sum
of the matrix..
4. I Int Loop variable as well as
stores the embedded
prime.
5. j Int Loop variable
6. d Int Counter
7. n Int Stores the number of
digits of the number.
8. dig Int Counter
9. r Int Counter.

8. Program to output the pattern:


Y H
PY HA
PPY HAP
APPY HAPP
HAPPY HAPPY
APPY HAPP
PPY HAP
PY HA
Y H

#include<iostream.h> //including preprocessor directives


#include<conio.h>
#include<stdio.h>
#include<string.h>
void main() //main() definition
{
clrscr();
int i,j,l,k; //initialization of variables
char a[10];
cout<<"Enter the string:\n";
gets(a);
l=strlen(a); //storing the length of the string in l
k=l-1;
for(i=0;i<l;i++) //for loop
{
for(j=k-i;j<l;j++) //for loop
{
cout<<a[j];
}
cout<<"\t";
for(j=0;j<=i;j++) //printing the upper half of the pattern
{
cout<<a[j];
}
cout<<endl;
}
for(i=l-2;i>=0;i--) //for loop
{
for(j=k-i;j<l;j++)
{
cout<<a[j];
}cout<<"\t";
for(j=0;j<=i;j++) //printing the lower half of the pattern
{
cout<<a[j];
}cout<<endl;
}
getch();
}

OUTPUT
Enter the string:
HAPPY
Y H
PY HA
PPY HAP
APPY HAPP
HAPPY HAPPY
APPY HAPP
PPY HAP
PY HA
Y H
ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration and definition of main() function.
4. Initialization and declaration of program variables.
5. Inputting the string on which the operation is to be performed.
6. Mechanism to produce the given pattern.
7. Printing the pattern.
8. main() function closes.
9. Program terminates along with termination of main() function.

VARIABLE LIST
1. a[] Char Stores the string
2. i int Loop variable
3. j Int Loop variable
4. l Int Stores the length of
string
5. k Int Stores the limit of the
loop

9. Program to output the possible combinations that add up to the


number entered by the user.

#include<iostream.h> //including preprocessor directives


#include<stdio.h>
#include<conio.h>
void main() //main() definition
{
int n,copy,i,j,k,s=0; //initialization of variables
cout<<"Enter number ";
cin>>n;
cout<<"Possible combinations "; //printing the combinations of the
number
for(i=2;i<512;i++) //for loop
{
copy=i;
int a[10]={0};
j=9;
while(copy>0) //while loop
{
a[j--]=copy%2;
copy/=2; //calculation of the combinations
}
s=0;
for(k=9;k>0;k--) //for loop
{
if(a[k]==1)
{
s+=k;
}
}
if(s==n) //if statement
{
for(k=9;k>0;k--)
{
if(a[k]==1)
{ cout<<k<<" "; //generating the
combinations
}
cout<<endl;

}
}
}
getch();
}
OUTPUT
Enter number:
5
Possible combinations
5
41
32

ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration and definition of main() function.
4. Initialization and declaration of program variables.
5. Inputting the number on which the operation is to be performed.
6. Mechanism to produce the combinations and print them.
7. main() function closes.
8. Program terminates along with termination of main() function.

VARIABLE LIST
1. n Int Stores the number.
2. i int Loop variable
3. j Int Loop variable
4. copy Int Stores the copy of the
number of iterations.
5. k Int Loop variable.
6. s Int Counter.
10. Program to find lowest and highest factors of a number entered by
the user and to find the sum of all primes between the lowest and
highest factors.

#include<iostream.h> //including preprocessor directives


#include<conio.h>
#include<stdio.h>
void main() //main() definition
{
int n,l,i,h,j,p,k,s; //declaration of variables
cout<<"Enter number ";
cin>>n;
for(i=2;i<n/2;i++) //for loop
{
if(n%i==0)
{
l=i; break; //finding lowest factor
}
}
h=n/l; //finding highest factor
cout<<”Lowest and highest factors are:”;
cout<<l<<" "<<h<<endl;
for(j=l+1;j<h;j++) //for loop to calculate the sum of prime between
lowest //and highest factors
{ for(k=2;k<=j/2;++k)
{
if(j%k==0)
{
p=0; break;
}
else
{ p=1; break;
}
}
if(p==1) //if statement
{cout<<j<<endl;
s=s+j;
}
}
cout<<"The sum is "<<s; //printing the statement

getch();
}

OUTPUT
Enter number: 14
Lowest and highest factors are:
2 7
The sum is: 17

ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration and definition of main() function.
4. Initialization and declaration of program variables.
5. Inputting the number on which the operation is to be performed.
6. Printing the lowest and highest factors of the number.
7. Printing the sum of the prime numbers between the lowest and
highest factors.
8. main() function closes.
9. Program terminates along with termination of main() function.

VARIABLE LIST
1. n Int Stores the number.
2. i int Loop variable
3. j Int Loop variable
4. l Int Stores the lowest factor
5. k Int Loop variable.
6. s Int Stores the sum.
7. h Int Stores the highest
factor.
8. p Int Counter.

1.Program that appends information to a file.

#include<iostream.h> //including preprocessor directives


#include<conio.h>
#include<stdio.h>
#include<fstream.h>
#include<process.h>
int main() //main() definition
{
char ch;
ifstream fin; //creating link to file
fin.open(“Names”); //opening text file
if(fin.good()) //if statement
{
cout<<”Here are the current contents of the Names file \n”;
//printing
current //contents
while(fin.get(ch)) //while loop
{
cout<<ch;
}
}
cout<<’\n”;
fin.close(); //closing link
ofstream fout(“Names”,ios::app);
if(fout.fail())
{
cout<<”Can’t open Names file for output \n”;
exit(-1);
}
cout<<”Enter new names (enter a blank line to quit): \n”;
char name[40];
cin.get(name,40).get(ch); //accepting names via user
while(name[0]!=’\0’)
{
fout<<name<<”\n”; cin.get(name,40).get(ch);
}
fout.close(); //closing link to file
fin.open(“Names”);
if(fin.good())
{
cout<<”The revised Names file is as shown below \n”; //printing
appended file
whle(fin.get(ch))
cout<<ch;
}
fin.close(); //closing link to the file
return 0;
}

ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration of member functions.
4. Initialization and declaration of program variables.
5. Opening link to file.
6. Displaying initial contents of the file.
7. Mechanism to append information to the file.
8. Printing the revised file.
9. Closing the link to the file.
10.main() function closes.
11.Program terminates along with termination of function.
VARIABLE LIST
1. ch Char Stores content of file.
2. fin Link to file for input.
3. fout Link to file for output.

2. Program to use multiple files in succession.

#include<iostream.h> //including preprocessor directives


#include<stdio.h>
#include<conio.h>
#include<fstream.h>
int main() //main() function
{
clrscr();
//first create two files
ofstream.filout;
filout.open(“Stunames”); //opening file
filout<<”Deepti Dhamija \n”Mahendra Chaurasia \n”<<”Lokesh Vasvani
\n”;
filout.close();
filout.open(“stumarks”);
filout<<”78.92\n”<<”72.17\n”<<69.33\n”;
filout.close();
char line[80]; //declaring variable
ifstream filin;
filin.open(“stunames”); //opening file
cout<<”The contents of stunames file are given below \n”;
filin.getline(line,80);
cout<<line<<”\n”;
filin.getline(line,80);
cout<<line<<”\n”;
filin.getline(line,80);
cout<<line<<”\n”;
filin.close(); //closing link
filin.open(“stumarks”); //opening file
cout<<”\nThe contents of stumarks file are given below \n”; //printing
data
filin.getline(line,80);
cout<<line<<”\n”;
filin.getline(line,80);
cout<<line<<”\n”;
filin.getline(line,80);
cout<<line<<”\n”;
filin.close(); //closing link to the file
return 0;
}

OUTPUT
The contents of stunames file are given below
Deepti dhamija
Mahndra chaurasia
Lokesh Vasvani
The contents of stumarks file are given below
78.92
72.17
69.33

ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration of member functions.
4. Initialization and declaration of program variables.
5. Opening link to file.
6. Displaying contents of the file.
7. Opening link to file.
8. Displaying contents of the file
9. Closing the link to the file.
10.main() function closes.
11.Program terminates along with termination of function.

VARIABLE LIST
1. line[] Char Stores each line of text
file.
2. filout Link to file for output.
3. filin Link to file for input.

3.Program to write and read a structure using write() and read()


function using a binary file.

#include<iostream.h> //including preprocessor


directives
#include<stdio.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
struct customer{ //structure definition
char name[51]; //declaring variables
float balance;
};
int main() //main() definition
{
clrscr();
customer savac; //declaring object
strcpy(savac.name,”Deepa Mehta”);
savac.balance=21310.75;
ofstream.fout;
fout.open(“Saving”,ios::out|ios::binary); //opening file
if(!fout)
{
cout<<”File can’t be opened \n”;
return 1;
}
fout.write((char *)&savac,sizeof(customer)); //writing to file
fout.close();
ifstream fin; //creating link to file
fin.open(“Saving”,ios:in|ios::binary); //opening file
fin.read((char *)&savac,sizeof(customer)); //reading from file
cout<<savac.name;
cout<<” has the balance Rs. “<<savac.balance<<”\n”;
fin.close(); //closing link to the file
return 0;
}

OUTPUT
Deepa Mehta has the balance Rs. 21310.75

ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Definition of a structure customer.
4. Declaration of variables.
5. Structure closes.
6. Definition of main() function.
7. Mechanism to print the balance of the customer.
8. Closing the links to the file.
9. main() function closes.
10.Program terminates along with termination of function.

VARIABLE LIST
1. name[] Char Stores name of
customer.
2. balance Float Stores the balance of
the customer.
3. fin Link to file for input.
4. fout Link to file for output.
4.Program to add, modify,and display records from a file.

#include<iostream.h> //including preprocessor


directives
#include<stdio.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
int count=0;
class student{ //class definition
char name[40];
char grade; //declaring class variables
float marks;
public:
void getdata(void); //declaring member functions
void display(void);
void mod_data();
};
void student::getdata(void) //defining function
{
char ch; //declaring variables
cin.get(ch);
clrscr();
gotoxy(15,10);
cout<<”Add student data \n”; //prompting user
gotoxy(17,12);cout<<”Record#”<<(++count)<<endl;
gotoxy(1,14);
for(int I=0;I,40;I++)name[I]=’’; //for loop
grade’’;marks=0.0;
cout<<”Enter name:”;cin.getline(name,40); //inputting data
cout<<”Enter grade:”;cin>>grade;
cout<<”Enter marks:”;cin>>marks;
cout<<”\n”;
}
void student::display(void) //defining
display()
{
clrscr();
gotoxy(15,10);
cout<<”Student details \n”; //printing student details
gotoxy(1,12);
cout<<”Name:”<<name<<”\t”<<”Grade:”<<grade<<”\t”<<”Marks:”<<
marks<<”\n”;
}
void student::mod_data(void) //defining function
{
char nm[40],gr=’’;float mk=-1; //initialization of variables
clrscr();
gotoxy(15,8);cout<<”Modify student data \n”<<endl;
char ch=cin.get();cout<<ch;
gotoxy(17,10);
cout<<”Current details \n”; //printing current details
gotoxy(17,11);
cout<<”---------------------------\n\n”;
cout””Name:”<<name<<”\tGrade:”<<grade<<”\tMraks:”<<marks<<endl
;
gotoxy(17,16);
cout<<Enter new details \n”; //prompting user to enter
new details
gotxy(17,17);
cout<<”---------------------------\n\n”;
gotoxy(10,24);
cout<<”If you want to retain old name or grade, just press Enter.”;
gotoxy(50,17);cout<<endl;
cout<<”Name:”; cin.getline(nm,40); //accepting data by
user
cout<<”Grade:”; gr=getche();
cout<<”\tMarks:”; cin>>mk;
if(strlen(nm)!=0) strcpy(name,nm);
if((gr!=13)&&(gr!=32)) grade=gr;
//ASCII values for Enter and Space are 13 and 32 respectively

if(mk>=0)marks=mk; //if statement


gotoxy(10,24);
clreol(); //to clear uptil end of the
line
}
int main() //main() definition
{
clrscr();
student stud; //declaration of object
fstream finout;
finout.open(“Stumast.dat”,ios::in|ios::out|ios:binary);
if(!finout)
{
cout<<””Cannot open file!!!\n”;
return 1;
}
int choice,mrec=0,offset=0; //initialization of variables
char ans;
do //do-while statement starts
{
clrscr();
cout<<’\nMain Menu\n”; //displaying main menu
cout<<”1.Add record.”<<endl;
cout<<”1.Modify record.”<<endl;
cout<<”1.Display record.”<<endl;
cout<<”1.Exit.”<<endl;
cout<<”Enter your choice..(1-4)..”;
cin>>choice;
switch(choice) //switch statement
{
case 1: //case 1 definition
stud.getdata(0;
mrec=count;
offset=((mrec-1)*sizeof(student));
finout.seekp(offset,ios::beg); //place of file pointer
finout.write((char *)&stud,sizeof(student)); //writing to file
break;
case 2: //case 2 definition
if(!count) //if block
{
cout<<”No record has been added yet.\n”;
cout<<”Please run option 1 first of all \n\n”;
gotoxy(10,23);cout<<”Press a key to continue….\n”;
getch();
break;
}
cout<<”\nModify which record? #”; //prompting user
cin>>mrec;
if(mrec>count) //if block
{
cout<<”\nOnly “<<count<<”records have been added.”;
cout<<”Invalid record number..\n”;
gotoxy(15,23);
cout<<”Press a key to continue….”<<endl;
getch();
break;
}
else //else body starts
{
offset=(mrec-1)*sizeof(student);
finout.seekg(offset,ios::beg); //place the file pointer
finout.read((char*)&stud,sizeof(student));
stdu.display();
cout<<”Modify this record?(y/n)\n”;
cin>>ans;
if(ans==’y’||ans==’Y’) //if statement
{
cout<<”Enter new details\n”; //prompting user
stud.mod_data();
finout.seekp(offset,ios::beg); //place the file pointer
finout.write((char*)&stud,sizeof(student)); //writing to file
cout<<\nRecord modified\n”;
gotoxy(30,24);
cout<<”Press a key to continue….”<<endl;
getch();
}
break;
}
case 3: //case 3 definition
if(!count) //if statement
{
cout<<”\nNo record has been added yet.\n”;
cout<<Please run option 1 first of all\n”;
gotoxy(10,23);
cout<<”Press a key to continue…”;
getch();
break;
}
cout<<”\n Display which record?\n”; //prompting user
cin>>mrec;cout<<endl;
if(mrec>count) //if statement
{
cout<<”\nOnly “<<count<,<<” records have been added.\n”;
cout<<”Invalid record number..\n”;
gotoxy(15,23);
cout<<”Press a key to continue..\n”;
getch();
break;
} //if closes
else{ offset=(mrec-1)*sizeof(student);
finout.seekg(offset,ios::beg); //place the file pointer
finout.read((char*)&stud,sizeof(student)); //reading file
stud.display(); //displaying data
gotoxy(10,23);
cout<<”Press a key to continue…\n’;
getch();
}
break;
case 4: //case 4 definition
break;
default: cout<<”Wrong choice!!!!!! Valid choice sare 1-4…\n”;
break;
}
}
while(choice>=1&&choice<=3); //do-while loop closes
finout.close(); //link to file closes
return 0;
}
OUTPUT
Main Menu

1. Add record.
2. Modify record.
3. Display record.
4. Exit.
Enter your choice…(1-4)…

ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Definition of a class student.
4. Declaration of variables.
5. Declaration of member functions in the public section of the class.
6. Class closes.
7. Definition of member functions.
8. main() function definition.
9. Initialization and declaration of variables.
10.Printing a menu using switch statement for users to add, modify,
and delete records to the file.
11.main() function closes.
12.Program terminates along with termination of function.

VARIABLE LIST
1. name[] Char Stores name of student.
2. marks Int Stores the marks of a
student.
3. grade Char Stores the grade of a
student.
4. ch Char Stores each character of
the file.
5. count Int Counter.
6. I Int Loop variable
7. nm[] Char Prototype of name[]
8. gr Char Prototype of grade
9. mk Char Prototype of marks
10. choice Char Stores user’s choice in
switch statement.

5.Program to display the size of a file in bytes.

#include<iostream.h> //including
preprocessor directives
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
int main() //main() function definition
{
clrscr();
char filename[13]; //variable declaration
cout<<”Enter filename:\n”;
cin.getline(filename,13);
ifstream infile(filename); //open file
if(!infile)
{
cout<<”Sorry cannot open “<<filename<<” file\n”;
exit(-1);
}
int no_bytes=0; //initializing variables
char ch;
while(cin.get(ch))
no_bytes++;
cout<<”File size is “<<no_bytes<<”bytes”; //printing the size of
file
return 0;
}
OUTPUT
Enter filename:
Test.dat
This is test run
File size is 32 bytes

ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration of variables.
4. Creation of link to open file.
5. Mechanism to calculate the size of the file when the file has been
opened.
6. Printing the file size.
7. main() function closes.
8. Program terminates along with termination of function.

VARIABLE LIST
1. filename[] Char Stores name of file.
2. ch Char Stores each character of
file.
3. no_bytes Int Stores the size of the
file.
4. infile Link to file for reading.

6. A binary file named emp.dat contains employee records. The


employee records are entered using a structure employee. Given the file
emp.dat, program to open the file and compute weekly wages for each
employee as follows:
wages=hrsworked*rate
Output should be:
Employee wages
Employee code Name Hours worked Rate Weekly
wages

#include<iostream.h> //including preprocessor


directives
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
struct employee{ //declaration of structure
int empcode;
char name[5]; //declaration of variables
int hrsworked;
float rate;
};
void main() //main() definition
{
float wages;
ifstream fin; //creation of link to file
fin.open(“emp.dat”,ios::in|ios::binary); //opening file
if(!fin)
{
cout<<”\nError in opening file\n”;
exit(1);
}
employee e1; //declaration of class object
cout<<”\t\t\tEmployee wages\t\t\t”;
cout<<”Employee code\tName\tHours worked\tRate\tWeekly wages\n”;

while(!fin.eof()) //while loop


{
fin.read((char*)&e1,sizeof(e1)); //reading from file
if(fin.eof()) //if statement
break;
wages=e1.hrsworked*e1.rate; //calculating wages
cout<<e1.employeecode<<”\t\t” e1.name<<”\t\t” e1.hrsworked<<”\t\t”
e1.rate<<”\t\t”<< wages<<endl; //printing the report
}
fin.close(); }
OUTPUT
Employee wages
Employee code Name Hours worked Rate Weekly
wages

01 Sushil Karmakar 10 200


2000
02 Pranab Mitra 12
100 1200
03 Sailen Roy 15
300 4500
04 Rakesh Khan 11
200 2200
05 Goutam Sikdar 12
200 2400

ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Definition of a structure employee.
4. Declaration of variables.
5. Structure closes.
6. Definition of main() function.
7. Declaration of variables.
8. Creation of link to open file.
9. Opening file.
10.Reading file and printing the contents.
11.Closing the links to the file.
12.main() function closes.
13.Program terminates along with termination of function.

VARIABLE LIST
1. name[] Char Stores name of
employee.
2. empcode Int Stores employee code.
3. hrsworked Int Stores hours worked by
each employee.
4. rate Int Stores rate of payment.
5. wages Float Stores wages earned per
week by each
employee.
6. fin Link to file for reading
the contents.

7.Program to illustrate the use of manipulators in header file iomanip.h to


produce formatted output.

#include<iostream.h> //including preprocessor directives


#include<iomanip.h>
void tab() //tab() definiton
{
const int width=6; //initialization of constant width
for(int row=1;row<=5;row++) //for loop
{
for(int col=1;col<=5;col++) //for loop
{
cout<<setw(width)<<row*col;
}
cout<<endl<<endl<<endl;
}
}
void patt() //function definition
{
const int width=6;
for(int row=1;row<=5;row++) //for loop
{
if(row%2) //if statement
cout<<setiosflags(ios::left);
else
cout<<resetiosflags(ios::left);
for(int col=1;col<=5;col++) //for loop
{
cout<<setw(width)<<row*col<<”::”;
}
cout<<endl;
}
} //for loops close
void main() //main() defintion
{
tab(); //calling of functions to print pattern
patt();
getch();
} //main() closes

OUTPUT
1 2 3 4 5
2 4 6 8 10
3 5 9 12 15
4 8 12 16 20
5 10 15 20 25

1 ::2 ::3 ::4 ::5 ::


2:: 4:: 6:: 8:: 10::
3 ::6 ::9 ::12 ::15 ::
4:: 8:: 12:: 16:: 20::
5 ::10 ::15 ::20 ::25 ::
ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Definition of a function tab() to produce tabs in output.
4. tab() closes.
5. Defintion of a function patt() to produce pattern as shown in
output.
6. patt() closes.
7. Definition of main() function.
8. Calling functions tab() and patt() to print the output.
9. main() function closes.
10.Program terminates along with termination of function.

VARIABLE LIST
1. width Int Stores the width of the
tab.
2. row Int Stores the row.
3. col Int Stores the column.

8.Given a file studata.dat that stores details of some students which


include name, roll no, class and marks in five subjects. Using such
details, create another file Result.dat to print the result of the students.
Program to execute this operation.

#include<iostream.h> //including preprocessor


directives
#include<stdio.h>
#inlcude<conio.h>
#include<fstream.h>
#nclude<process.h>
struct stu{ //defining structure stu
int rollno;
char name[5]; //declaring variables
int class;
float marks[5];
}s1;
struct res{ //defining structure res
int rollno;
float perc; //declaring variables
char grade;
}r1;
void main() //main() definition
{
ifstream ifin;
ifin.open(“Studata.dat”,ios::on|ios::binary); //opening file
ofstream fout;
fout.open(“Result.dat”,ios::in|ios::binary); //opening file
if(!ifin) //if statement
{
cout<<”Error in opening file STUDATA>DAT!!!!”\n”;
exit(1);
}
if(!fout) //if statement
{
cout<<”Error in creating file RESULT.DAT!!!!\n”;
exit(1);
}
float tot=0;
while(!ifin.eof()) //while loop
{
ifin.read((char*)&s1,sizeof(s1)); //reading file
r1.rollno=s1.rollno;
for(int I=0;tot=0;I<5;I++) //for loop
tot+=s1.marks[I];
r1.perc=tot/5; //calculation of data to print result
if(r1.perc>=75)
r1.grade=’A’;
else if(r1.perc>=60)
r1.grade=’B’;
else if(r1.perc>=50)
r1.grade=’C’;
if(r1.perc>=40)
r1.grade=’D’;
else
r1.grade=’F’;
fout.write((char*)&r1,sizeof(r1)); //writing to file
}
ifin.close(); //closing the links to the file
fout.close();
ifin.open(“Result.dat”,ios::in|ios::binary); //opening file
int I=0;
while(!ifin.eof()) //while loop
{
ifin.read((char*)&r1,sizeof(r1)); //reading from file
cout<<”\nStudent”<<++I<<endl; //printing result
cout<<”Roll no: “<<r1.rolno<<”
Percentage:“<<r1perc<<”Grade:“<<r1.grade<<endl;
}
ifin.close(); getch(); }

OUTPUT
Student 1
Roll no: 125 Percentage: 72.5 Grade: B
Student 2
Roll no: 569 Percentage: 80.5 Grade: A
Student 3
Roll no: 856 Percentage: 92 Grade: A
Student 4
Roll no: 253 Percentage: 60 Grade: B
Student 5
Roll no: 225 Percentage: 85.6 Grade: A

ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Definition of a structure stu.
4. Declaration of variables.
5. Structure closes.
6. Declaration of second structure res.
7. Declaration of variables.
8. Structure closes.
9. Definition of main() function.
10.Creation of link fin and fout to read data from ‘studata’ and writing
data to ‘result’.
11.Opening file ‘studata’ and creating file ‘result’.
12.Printing the contents ‘result’.
13.Closing the links to the file.
14.main() function closes.
15.Program terminates along with termination of function.
VARIABLE LIST
1. name[] Char Stores name of studentr.
2. rollno Int Stores roll no of
student.
3. class Int Stores class of student.
4. perc Float Stores percentage of
student.
5. grade Char Stores grade of student
6. marks[] Int Stores marks of
student.
7. ifin Link to file for reading
contents from ‘studata’.
8. fout Link to file for writing
to file ‘result’.
9. I Int Loop variable.
9.Program to display the contents of a file using get() function.

#include<iostream.h> //including preprocessor directives


#include<stdio.h>
#include<conio.h> //for clrscr()
#include<fstream.h>
int main() //defining main() function
{
clrscr();
char ch; //declaring variable
ifstream fin; //create input stream
fin.open(“Master”,ios::in|ios::binary); //open file
if(!fin) //fin stores zero i.e. false
value
{
cout<<”Cannot open file!! \n”;
return 1;
}
while(fin) //fin will be zero when eof is
reached
{
fin.get(ch); //read a character
cout<<ch; //display the character
}
fin.close(); //closing link to the file
return 0;
} //main() function closes

OUTPUT
THIS IS A TEST RUN OF THE ABOVE PROGRAM.

ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Definition of main() function.
4. Declaration of variables.
5. Creation of link to open file.
6. Printing the contents of the file..
7. Closing the links to the file.
8. main() function closes.
9. Program terminates along with termination of function.

VARIABLE LIST
1. ch Char Stores each character of
the file.
2. fin Link to read contents
from the file.

10.Program for reading and writing class objects.

#include<iostream.h>
//including preprocessor
#include<fstream.h>
//directives
#include<conio.h>
class student{
//declaring class
char name[5];char grade; //declaring
variables
float marks;
public:
void getdata(void) //declaring
member functions
voide display(void);
};
void student::getdata(void) //member function defintion
{
char ch; //declaring variable
cin.get(ch);
cout<<”Enter name:”; cin.getlin(name,40); //prompting user
cout<<”Enter grade:”; cin>>grade;
cout<<”Enter marks:”; cin>>marks;
cout<<’\n”;
} //function closes

void student::display(void) //member function defintion


{
cout<<”Name:”<<name<<”\t”<<”Grade:”<<grade<<”\t”<<”Marks:”<<
marks<<”\t”<<”\n”; //printing data
}
int main() //defining main() function
{
clrscr();
student arts[3]; //declare array of 3 objects
fstream filin;
filin.open(“Stu.dat”,ios::in|ios::out); //opening file
if(!filin)
{
cout<<”Cannot open file!! \n”;
return 1;
}
cout<<”Enter details for three students \n”;
for(int I=0;I<3;I++) //for loop
{
arts[I].getdata(); //inputting data from user by
calling functon
filin.write((char*)&arts[I],sizeof(arts[I]));
}
filin.seekg(0); //seekg(0) resets the file to start,so
that the file
//can be accessed
from the beginnng.
Cout<<”The contents of stu.dat are given below. \n”;
For(I=0;I<3;I++)
{
filin.read((char*)&arts[I],sizeof(arts[I])); //reading data from file
arst[I].display(); //displaying data by calling display()
}
filin.close(); //closing link to the file
return 0;
} //main() function closes

OUTPUT
Enter details for 3 students

Enter name:Darpan Agarwal


Enter grade:B
Enter marks:66

Enter name:Manit Ghosh


Enter grade:B
Enter marks:68

Enter name:Vijay Das


Enter grade:A
Enter marks:80

The contents of stu.dat are shown below.


Name: Darpan Agarwal Grade:B Marks: 66
Name: Manit Ghosh Grade:B Marks: 68
Name: Vijay Das Grade:A Marks: 80

ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Definition of a class student.
4. Declaration of class variables.
5. Declaration of member functions in public section of class.
6. Class closes.
7. member functions definitions.
8. main() function starts.
9. Declaration of variables.
10.Mechanism to read data entered by user, writing them onto the file
and displaying them.
11.Closing the links to the file.
12.main() function closes.
13.Program terminates along with termination of function.

VARIABLE LIST
1. name[] Char Stores name of student.
2. grade Char Stores the grade of
student.
3. marks Float Stores the marks of
student.
4. arts[] Student Object array of class to
access member
functions.
5. filin Link to file for reading
data from file.
6. I Int Loop variable.

Вам также может понравиться