0% found this document useful (0 votes)
61 views8 pages

Shahzad

The document contains C++ code examples demonstrating basic file input/output operations like writing to a text file, reading from a text file, opening and closing files, and reading an entire binary file. It also includes examples of 2D array operations like inputting values into a 2D matrix, matrix transpose, matrix multiplication, and sorting arrays using different sorting algorithms like bubble sort and selection sort. Additionally, it shows examples of swapping variable values using pointers in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views8 pages

Shahzad

The document contains C++ code examples demonstrating basic file input/output operations like writing to a text file, reading from a text file, opening and closing files, and reading an entire binary file. It also includes examples of 2D array operations like inputting values into a 2D matrix, matrix transpose, matrix multiplication, and sorting arrays using different sorting algorithms like bubble sort and selection sort. Additionally, it shows examples of swapping variable values using pointers in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

// writing on a text file

#include <iostream>
#include <fstream>
using namespace std;

int main () {
ofstream myfile ("example.txt");
if (myfile.is_open())
{
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
else cout << "Unable to open file";
return 0;
}

// reading a text file


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}

else cout << "Unable to open file";

return 0;
}

// basic file operations


#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}

// reading an entire binary file


#include <iostream>
#include <fstream>
using namespace std;

int main () {
streampos size;
char * memblock;

ifstream file ("example.bin", ios::in|ios::binary|ios::ate);


if (file.is_open())
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();

cout << "the entire file content is in memory";

delete[] memblock;
}
else cout << "Unable to open file";
return 0;
}
#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int a[3][3],i,j;
//Input the numbers
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"Enter number :";
cin>>a[i][j];
}
}
//Display the original matrix
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<a[i][j]<<"t";
}
cout<<endl;
}
cout<<"Transpose of above matrix is :-"<<endl;
//Display the transpose matrix
for(j=0;j<3;j++)
{
for(i=0;i<3;i++)
{
cout<<a[i][j]<<"t";
}
cout<<endl;
}
//get character
getch();
}

#include<stdio.h>
#include<iostream.h>
#include<conio.h>
int a[10][10];int b[10][10];int c[10][10];
void main()
{
int i,j,k,l;
clrscr();

cout<<"Enter the value of the matrix a:";


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>a[i][j];
}
}
cout<<"\n Enter the value of the matrix b:";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>b[i][j];
}
}

cout<<"\n The multiplied value is:";


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=0;
for(k=0;k<2;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<c[i][j];
}
cout<<"\n" ;
}

getch();
}

SORTING By Using Bubble Sorting Method


 

#include<iostream.h>
 #include<conio.h>

 void main()
 { clrscr();
 int i,j,temp;
 int A[5]={6,3,9,1,5};
 cout<<"Sorting Using Bubble Sorting Method \n";
 cout<<"THE ORIGINAL ARRAY IS: \n";
  for(i=0;i<5;i++)
  cout<<A[i];
  cout<<"\n";
 for(i=0;i<5;i++)
 {
 for(j=0;j<5;j++)
 {
 if(A[j]<A[j+1])
 {
 temp=A[j];
 A[j]=A[j+1];
 A[j+1]=temp;
 }
 }
 }
  cout<<"\n";
  cout<<"the sorted array is \n";
 for(i=0;i<5;i++)
 cout<<A[i];
 cout<<endl;
 getch();
 }

 Selection Sorting in Ascending Manner

#include<iostream.h>
 #include<conio.h>

 void main()

 {  clrscr();
 int i,j ,s,temp,p;
 int a[5]={5,9,3,7,1};

 cout<<"\n";
 cout<<"corginal array is: \n";
 for(i=0;i<5;i++)
  cout<<a[i];

 for(i=0;i<5;i++)
 {
    s=a[i];
    p=i;

    for(j=i;j<5;j++)
    {
    if(s>a[j])
    {
     s=a[j];
     p=j;
    }
    }
    temp=a[i];
    a[i]=a[p];
    a[p]=temp;
    }

    cout<<"\n";
 cout<<"sorted array is: \n";
 for(i=0;i<5;i++)
  cout<<a[i];
     getch();
    }

Entering VaLUES INTO 2D matrix

#include<iostream.h>
#include<conio.h>
void main()
{   clrscr();
int a[3][3];
int i,j,k;

cout<<"Entered Elements into 3*3 matrix \n";


for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j];

cout<<"Matrix Entered By you is \n";


for(i=0;i<3;i++)
{for(j=0;j<3;j++)
  cout<<a[i][j]<<" ";
   cout<<endl;
}

getch();

Swapping Values Using Pointer


#include<iostream>
using namespace std;

void main()
{
int x,y;
int *p1,*p2,*p3;
cout<<"ENTERS THE VALUES FOR x= ";
cin>>x;
cout<<"\nENTERS THE VALUES FOR y= ";
cin>>y;
p1=&x;
p2=&y;

p3=p1;
p1=p2;
p2=p3;

cout<<"THE VALUES AFTER SWAPPING ARE \nx= "<<*p1<<"\ny= "<<*p2;

You might also like