-moz-user-select:none; -webkit-user-select:none; -khtml-user-select:none; -ms-user-select:none; user-select:none;
Showing posts with label Coding. Show all posts
Showing posts with label Coding. Show all posts

Monday, 25 April 2016

File Access Methods in C++

I have discussed the concept of "files" in my previous blog.Today I am discussing the different file access method.

File access Method

The way in which a file can be accessed is called file access method.It depends on the manner in which data is stored in the files.Different file access methods are given below:

Sequential Access Method:

                                                                   It is used to access data in the same manner in which it is stored in a file.This method reads and writes data in a sequence.In order to access the last record,it has to to read all the records before the last one.

The record is not fixed in this access method.It means that each record occupies different amount of memory.

One of its advantages is that memory is not wasted.One of its disadvantages is that it takes very long time to search a record.For example,if the user wants to search the 4th record,it has to read the first 3 records to access the 4th record.

Random Access Method:

                                          It is used to access the data directly without accessing the preceding data.This method does not read or write data in a sequence.It is faster than sequential access method.

It stores data in fixed length records.This is the main disadvantage of this access method.It wastes memory space.Suppose a record of 10 bytes is used to store a complete sentence in a file.If a sentence consists of a single letter like 'H', it will occupy 10 bytes to store this letter.

However,this method speeds up access time because the position of each record can be determined easily as each record takes fixed length.Database management systems store files in a random access format.

Saturday, 23 January 2016

Concept of files in C++

Many students are confused with the concept of file handling in C++.So,today I will give you a little introduction about the concept of files in C++ which the essence of file handling.This would be a series of blogs which will help you to understand this concept easily.So,stay tuned !!1

Concept of files

As we see that a program inputs data from the user and stores it in variables.The data stored in the variables is temporary.When the program ends,all data stored in variables is also destroyed. The user has  to input data again from keyboard when the program is executed next time. the data has to be entered each time the program is executed that wastes time. The user may also type wrong data at different times.

Definition of files

A file is a collection of related records.It stores data about an entity(if you are unfamiliar with the concept of entity, remember that an entity is an object about which data is to be gathered.e.g., student is an entity). A data file can be used to provide input to a program.In can also be used to store the output of a program permanently.


Advantages of files

Now we will discuss the advantages of files in C++.

  • Files can store large amount of data permanently.
  • Files can be updated easily.
  • One file can be used by many programs for same input.
  • Files save time and effort to input data via keyboard.

Types of files

C++ provides two types of files.This categorization is based on how data is stored in files. Following are the types of files in C++.

Text files

A type of file that stores data as readable and printable characters in called text file.A source program of C++ is an example of text file.


Binary files

A type of file that stores data as non-readable binary code is called binary file.An object file of a C++ program is an example of binary file.


Reference

This reference of this article is taken from the book,Object Oriented Programming using C++(IT series).

Saturday, 21 November 2015

Binary search in C++

"Sequential search" and "binary search" are one of the most important concepts in C++.I have  discussed the concept of sequential search previously.So, today I am going to discuss the concept of binary search in C++.

Binary search

Binary search is a quicker method of searching for value in an array.It is very quick but remember that it can only search a sorted array.It cannot be applied on an unsorted array.

Procedure

The following steps should be followed to search a number in an array by this method.

  • It locates the middle element of array and compares with the search number.
  • If they are equal, search is successful and the index of middle element is returned.
  • If they are not equal, it reduces the search to half of the array.
  • If the search number is less than the middle element,it searches the first half of array.Otherwise it searches the second half of the array.The process continues until the required number is found or loop completes without successful search.
The following program demonstrates the concept of binary search in C++.

Source code

#include<iostream>
#include<conio.h>
using namespace std;
void main( )
{
int array[10]={10,20,30,40,50,60,70,80,90,100};
int n;
int mid;
int start=0
int end=9;
int loc=-1;
int i;
cout<<"Enter any number:";
cin>>n;
while(start<=end)
{
mid=(start+end)/2;
if(array[mid]==n)
{
loc=mid;
break;
}
else if(n<array[mid])
end=mid-1;
else
start=,id+1;
}
if(loc==-1)
cout<<"Number not found."<<endl;
else
cout<<"Number found at index:"<<loc<<endl;
getch( );
}


Output

Enter any number:40
Number found at index:3

Sunday, 15 November 2015

Sequential search in C++

Sequential search

Sequential search is also called linear search or serial search.It is a simple to search an array for the desired value.It follows the following steps to search a value in an array:

Steps

  • Visit the first element of the array and compare its value with the required value.
  • If the value of the array matches with the required value,the search is complete.
  • If the value of array does not match,move to next element and repeat same process.
Loops are frequently used to visit elements of array for searching a value.You can start the counter variable of loop from 0 and move it to the last index of an array.

An example to demonstrate this concept is given below:

Source code


#include<iostream>
#include<conio.h>
using namespace std;
void main( )
{
int array[11]={10,20,30,40,50,60,70,80,90,100,110};
int i;
int n;
int location=-1;//location is initially null
cout<<"Enter value to search:";
cin>>n;
for(i=0;i<11;i++)   //you cannot write "for(i=0;i<=11;i++)" ........writing this will generate an error
if(array[i]=n)
loc=i;
if(loc== -1)
cout<<"Value not found in the array.";
else
cout<<"Value found at index"<<loc;
getch( );
}

Output

Enter value to search:40
Value found at index:3

Reference

The reference of this article has been taken from the book, "Object Oriented Programming in C++" by Robert Lafore.

Monday, 9 November 2015

C++ manipulators

C++ manipulators

Manipulators are used to change/rotate the output in different styles.They are the mlost common way to control the format of output in C++.

Some of the most important manipulators that are used in C++ are:
  • endl
  • setw
  • setprecision
Today,in my blog,I will discuss the manipulators mentioned above and their functionality in C++.

'endl' Manipulator:

                                      The word  'endl' stands for end of line.It is used to move the cursor to the beginning of the next line.It requires no parameter.

Example:

cout<<"codingwithcplusplus.blogspot.com"<<endl;
cout<<"Blogging my passion";

It will show the output as below:

codingwithcplusplus.blogspot.com
Blogging my passion

'setw' Manipulators:

                                                    The word 'setw' stands for set width.It is used to display the value of an expression in specified columns.The output is right justified by default.It is necessary to include header file "iomanip.h" to use this manipulator.

Example:

cout<<"Hello"<<setw(4)<<"World";

It will show the output as below:

Hello    World

I think no more explanation is required. :-)

'setprecision' Manipulator:

                                               It is used to set the number of digits to be displayed after decimal point.The value is rounded with the use of this manipulator.

Example:

Suppose the value of x=10.0

cout<<setprecision(2)<<x;

It will show the output as:

10.00

Now,consider

cout<<setprecision(5)<<x;

It will show the output as:

10.00000








Saturday, 24 October 2015

Concept of enumeration in C++

You have often heard about "enumeration" in C++.Today I will explain this concept with the help of articles taken from different websites.

Definition of enumeration

Enum(enumeration) is a user-defined type consisting of a set of enumerators( enumerator --- named integer constant).

The idea behind enumerated types is to create new data types that can take on only a restricted range of values. Moreover, these values are all expressed as constants rather than magic numbers--in fact, there should be no need to know the underlying values.

Declaration of enumeration

The enum is declared as:

enum enum-type-name { enum-list } enum-variable;

In this form, enum-type-name is optional. However, if you want to use enum type in several places, it is better to use another way of enum declaration:

enum enum-type-name { enum-list };

//... (and somewhere below)

enum enum-type-name enum-variable;

Of course, in the second case enum-type-name cannon be omitted.

An example is given below which will show you how to declare enumeration in C++.
enum e_acomany
{
Audi,
BMW, 
Cadillac, 
 Ford,
 Jaguar, 
 Lexus, 
 Maybach, 
 RollsRoyce, 
 Saab
};

Below is a sample code that will help you to undersatnd this concept.

Source code

#include<iostream>
using namespace std;
enum colours{blue,red};
int main( )
{
colours c;
c=blue;
cout<<"Blue is at number:"<<c<<endl;
c=red;
cout<<"Red is at number:"<<c<<endl;
return 0;
}


Output

Blue is at number:0
Red is at number:1

Credits


For this blog,I have taken help from the following websites:







Saturday, 17 October 2015

Importance of pointers in C++

Today I found a very helpful article on "programmers.stackexchange.com" regarding the importance of pointers in C++.This article was written by David Thornley.So,I decided to share it with all my readers.

Importance of pointers in C++

Pointers are necessary for dynamic memory location, many data structures, and efficient handling of large amounts of data. Without pointers, you'd have to allocate all the program data globally or in functions or the equivalent, and you'd have no recourse if the amount of data grew beyond what you had originally allowed for. I hesitate to use absolutes here, but as far as I know all modern computer languages have pointers in some form or other.
In most languages that use pointers, there are certain sorts of references that are pointers, and perhaps certain sorts of references that aren't, and there is no further notational difference. A Lisp cons cell is a pair of pointers, although a fixnum is not a pointer. In Java, the variable used for the instance of a class is a pointer, but an int isn't. The language syntax doesn't reflect that.
C is unusual in that pointers are optional, explicit, and allow explicit pointer arithmetic. It is perfectly possible to write struct foo bar; struct foo * baz;, and once you've allocated memory for bazyou can use both bar and baz to represent struct foos. Since pointers are optional, it is useful to have notational differences. (It's essential in C++ for smart pointers, as given boost::shared_ptr<foo> bar;, bar.reset() has one meaning and bar->reset() is likely to have a much different one.)
(Actually, explicit pointers were often used in other languages when C was originally being developed, such as ^ in Pascal. C is an older language than most in common use today, and it shows.)
One of C's design goals was to write Unix in, and therefore it needed to handle memory locations in a detailed manner. (C is actually one of a family of system implementation languages common when it was being designed, another example being Cybol for Control Data computers. C is the one that became a big hit.) Therefore, it is possible to manipulate C pointers directly, assigning memory addresses and calculating new ones. This also led to some design decisions in C. C arrays are based heavily on pointer arithmetic, and indeed an array decays into a pointer in very many situations. Passing variables to C functions by reference is done by pointer. There was no strong need for arrays and passing variables by reference in the form that other contemporary languages had, so C didn't get those.
So, the answer is that, in most languages nowadays, you use pointers constantly without being reminded of the fact. In C, and to a lesser extent C++, you use pointers either to do low-level things, or as accomplish higher-level things that there's no special notation for.

Thursday, 8 October 2015

Pointer Declaration in C++

Pointer Declaration
As I have discussed in the previous blog that a pointer is a variable whose value is the address of another variable.Remember that the declaring a pointer is same as declaring a simple variable.An asterisk "*" is used in the declaration that indicates that the variable is a pointer variable.

Syntax


The syntax of declaring a pointer is as follows:


DataType *var;

Example

int *p;

Now,I will demonstrate this concept by a simple program.

Source code

#include<iostream>
#include<conio.h>
using namespace std;
void main( )
{
int n;
int *ptr; // it indicates that "ptr" is a pointer
cout<<"Enter an integer";
cin>>n;
ptr=&n;
cout<<"The value of n:"<<n<<endl;
cout<<"The address of n:"<<ptr<<endl;
getch( );
}

Output

Concept of pointers


Monday, 28 September 2015

Example of pointers in C++

As we know that a pointer is a variable whose value is the address of another variable. To demonstrate it,consider the following example.


Source code


#include <iostream>

using namespace std;

int main ()

{

   int  var1;

   cout << "Address of var1 variable: ";

   cout << &var1 << endl;

   return 0;

}

Output


Example of pointer
Example of pointer


Reference


To know the reference of this blog,

Thursday, 3 September 2015

Concept of pointers in C++

Pointers are an extremely powerful programming tool. They can make some things much easier, help improve your program's efficiency, and even allow you to handle unlimited amounts of data. For example, using pointers is one way to have a function modify a variable passed to it. It is also possible to use pointers to dynamically allocate memory, which means that you can write programs that can handle nearly unlimited amounts of data on the fly--you don't need to know, when you write the program, how much memory you need.      

Concept of pointers

Pointers are aptly named: they "point" to locations in memory. Think of a row of safety deposit boxes of various sizes at a local bank. Each safety deposit box will have a number associated with it so that the teller can quickly look it up. These numbers are like the memory addresses of variables. A pointer in the world of safety deposit boxes would simply be anything that stored the number of another safety deposit box. Perhaps you have a rich uncle who stored valuables in his safety deposit box, but decided to put the real location in another, smaller, safety deposit box that only stored a card with the number of the large box with the real jewelry. The safety deposit box with the card would be storing the location of another box; it would be equivalent to a pointer. In the computer, pointers are just variables that store memory addresses, usually the addresses of other variables. 

Declaration of pointers

The general form of a pointer variable declaration is:
type *var-name;
Here, type is the pointer's base type; it must be a valid C++ type and var-name is the name of the pointer variable. The asterisk you used to declare a pointer is the same asterisk that you use for multiplication. 


Reference

The reference of this blog is taken from



       

Saturday, 22 August 2015

Matrix multiplication in C++

You have often learnt the method of matrix multiplication in Mathematics.

Matrix multiplication


MAtrix multiplication

Now,we will perform the same operations in C++ using the concept of 2-d arrays.So,below is the source code of the program.

Source code

#include<iostream>
#include<conio.h>
using namespace std;
void addition();
void subtraction();
void multiplication();

int mat1 [3][3], mat2[3][3],mat3[3][3], i ,j, k, sum,ab,ch;
int main()
{
int choice;
{
x:
cout<<"\t"<<"\t"<<"MAIN MENU"<<"\t"<<"\t"<<endl<<endl<<endl;
cout<<"Press 1 for 3*3 matrix multiplication."<<endl<<endl;
cout<<"Press 2 to exit from the program."<<endl<<endl;
cout<<"Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
multiplication();
break;
case 2:
return 0;
default:
cout<<"Invalid choice !!"<<endl<<endl;
}

goto x;
}
}
void multiplication()
{     
           cout<<"\nEnter values for first 3 x 3 matrix:\n";
           for ( i = 0 ; i <= 2 ; i++ )
                {
                   for (j = 0 ; j <= 2 ; j++ )
                        cin>>mat1 [i][j];
                }
                        cout<<"\n Enter values for second 3 x 3 matrix:\n";
                   for ( i = 0 ; i <= 2 ; i++ )
                        {
                            for ( j = 0 ; j <= 2 ; j++ )
                                   cin>>mat2[i][j];
                        }
                       cout<<"\n The first 3 x 3 matrix entered by you is:\n";
                            for ( i = 0 ; i <= 2 ; i++ )
                                 {
                                       for ( j = 0 ; j <= 2 ; j++ )
                                              cout<< mat1[i][j]<<"\t";
                                              cout<<endl<<endl;
                                 }
                                              cout<<"\n the second 3 x 3 matrix entered :\n";
                                       for ( i = 0 ; i <= 2 ; i++ )
                                            {
                                                for ( j = 0 ; j <= 2 ; j++ )
                                                       cout<< mat2[i][j]<<"\t" ;
                                                       cout<<endl<<endl;
                                            }
                                       for ( i = 0 ; i <= 2 ; i++ )
                                            {
                                                for ( j = 0 ; j <= 2 ; j++ )
                                                      {
                                                          sum = 0;
                                                            for ( k = 0 ; k <=2 ; k++ )
                                                                   sum = sum + mat1 [i][k] * mat2[k][j];
                                                                   mat3[i][j] = sum ; 
                                                       }
                                             }
                                                                   cout<<"\nThe product of the above two matrices is:\n";
                                                                   for ( i = 0 ;i<= 2 ; i++ )
                                                                        {
                                                                             for ( j = 0 ; j <= 2 ; j++ )
                                                                                      cout<<mat3[i][j]<<"\t";
                                                                                      cout<<endl<<endl;
                                                                         }
                                                                     }

Output

Matrix multiplication in C++

                                                                    

Monday, 17 August 2015

Matrix addition and subtraction in C++

You are well aware of the matrix addition and subtraction in Mathematics.

Matrix addition

Matrix addition

Matrix subtraction

Matrix subtraction

Now,we will perform the same operations in C++ using the concept of 2-d arrays.So,below is the source code of the program.

Source code

#include<iostream>
#include<conio.h>
using namespace std;
void addition();
void subtraction();

int mat1[2][2], mat2[2][2],mat3[2][2], i ,j, k, sum,ab,ch;
int main()
{
int choice;
{
x:
cout<<"\t"<<"\t"<<"MAIN MENU"<<"\t"<<"\t"<<endl<<endl<<endl;
cout<<"Press 1 for 2*2 matrix addition."<<endl<<endl;
cout<<"Press 2 for 2*2 matrix subtraction."<<endl<<endl;
cout<<"Press 3 to exit from the program."<<endl<<endl;
cout<<"Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
addition();
break;
case 2:
subtraction();
break;
case 3:
return 0;
default:
cout<<"Invalid choice !!"<<endl<<endl;
}

goto x;
}
}
void addition()
{

 cout<<"\nEnter values for first 2 x 2 matrix:\n";
           for ( i = 0 ; i <= 1 ; i++ )
                {
                   for (j = 0 ; j <= 1 ; j++ )
                        cin>>mat1 [i][j];
                }
                        cout<<"\n Enter values for second 2 x 2 matrix:\n";
                   for ( i = 0 ; i <= 1 ; i++ )
                        {
                            for ( j=0 ; j<=1 ; j++ )
                                   cin>>mat2[i][j];
                        }
                       cout<<"\n The first 2 x 2 matrix entered by you is:\n";
                            for ( i=0 ; i<=1 ; i++ )
                                 {
                                       for ( j=0 ; j<=1 ; j++ )
                                              cout<< mat1[i][j]<<"\t";
                                              cout<<endl<<endl;
                                 }
                                              cout<<"\n the second 2 x 2 matrix entered :\n";
                                       for ( i = 0 ; i <= 1 ; i++ )
                                            {
                                                for ( j = 0 ; j <= 1 ; j++ )
                                                       cout<< mat2[i][j]<<"\t";
                                                       cout<<endl<<endl;
                                            }
                                            for(i=0;i<=1;i++)
                                            {
                                            for(j=0;j<=1;j++)
                                            {
                                                                   mat3[i][j] = mat1[i][j] + mat2[i][j];
}
}
cout<<"The addition of matrices:"<<endl<<endl;
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
cout<<mat3[i][j]<<"\t";
cout<<endl<<endl;
}
}
void subtraction()
{
cout<<"\nEnter values for first 2 x 2 matrix:\n";
           for ( i = 0 ; i <= 1 ; i++ )
                {
                   for (j = 0 ; j <= 1 ; j++ )
                        cin>>mat1 [i][j];
                }
                        cout<<"\n Enter values for second 3 x 3 matrix:\n";
                   for ( i = 0 ; i <= 1 ; i++ )
                        {
                            for ( j = 0 ; j <= 1 ; j++ )
                                   cin>>mat2[i][j];
                        }
                       cout<<"\n The first 2 x 2 matrix entered by you is:\n"<<endl;
                            for ( i = 0 ; i <= 1 ; i++ )
                                 {
                                       for ( j = 0 ; j <= 1 ; j++ )
                                              cout<< mat1[i][j]<<"\t";
                                              cout<<endl<<endl;
                                 }
                                              cout<<"\n the second 2 x 2 matrix entered :\n"<<endl;
                                       for ( i = 0 ; i <= 1 ; i++ )
                                            {
                                                for ( j = 0 ; j <= 1 ; j++ )
                                                       cout<< mat2[i][j]<<"\t";
                                                       cout<<endl<<endl;
                                            }
                                            for(i=0;i<=1;i++)
                                            {
                                            for(j=0;j<=1;j++)
                                            mat3[i][j]=mat1[i][j]-mat2[i][j];
}
cout<<"The subtraction of matrices:"<<endl<<endl;
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
cout<<mat3[i][j]<<"\t";
cout<<endl<<endl;
}
}

Output

Matrix addition and subtraction


Saturday, 15 August 2015

Initialization of 2-d arrays in C++

Initializing 2-dimensional array

The 2-D arrays which are declared by the keyword int and able to store integer values are called two dimensional integer arrays. The process of assigning values during declaration is called initialization. TheseArrays can be initialized by putting the curly braces around each row separating by a comma also each element of a matrix should be separated by a comma.

Example: 

int mat[3][3]={ {2,3},{4,5},{6,7} }

Initializing 2-d character array

The 2-D arrays which are declared by the keyword char and able to store characters are called two dimensional character arrays. 2-D character array can be initialized by putting the curly braces around each row as well as apostrophe around every alphabet.

Example:

char character[3][3]={'b', 'a' ,'t' ,'f', 'a' ,'t' ,'m' ,'a' ,'t'};

reference

The reference of this article is taken from


In my next blog inshAllah,I will post an important example of 2-d arrays that will be very helpful in understanding this concept.So,stay tuned.

Wednesday, 12 August 2015

Two-dimensional arrays in C++

Definition

In C++ Two Dimensional array in C++ is an array that consists of more than one rows and more than one column. In 2-D array each element is refer by two indexes. Elements stored in these Arrays in the form of matrices. The first index shows a row of the matrix and the second index shows the column of the matrix.

Syntax

data type name of array[no. of rows][no. of columns] 

For example,

int item[4][5];

Accessing each location


In order to store values in a C++ two dimensional arrays the programmer have to specified the number of row and the number of column of a matrix. To access each individual location of a matrix to store the values the user have to provide exact number of row and number of column.

Example:

int matrix[2][2];

matrix[0][0]=20;

matrix[0][1]=25;

matrix[1][0]=30;

matrix[1][1]=35;

Entering data

Nested loop is used to enter data in 2-D arrays. It depends upon the programmer which loop he wants to use it could be While loop or it could be a For loop. The outer loop acts as the number of rows of a matrix and the inner loop acts as the number of columns of a matrix.

Example:















#include<iostream.h>
#include<conio.h>
main()
{
int matrix [5] [5];
for (int m1=0 ; m1<5 ; m1++)
{
for (int m2=0 ; m2<5 ; m2++)
{
Matrix [m1] [m2] = 5 ;
}
}
getch();
}

                 Reference

The reference of this article has been taken from

Sunday, 9 August 2015

Swapping two numbers

"Swapping two number" is one of the most popular and easiest program of C++. If you want to make a good logic in C++,then you should practice this code.

Today I am here with the source code of this program.Hope you will like my effort.

Source code

sSwapping in C++

Output

Output of swapping

Logic

Below is the logic of this program.

I have taken 3 variables for this program,"a,b and tem(or temporary).

According to logic,

Suppose the value of a is 10 and the value of b is 20.

According to the given condition,

tem=a;
tem=10;
a=b
10=b or b=10;
b=tem
20=tem
b=10.

So,after swapping,the value of "a" becomes 20 and the value of "b" becomes 10. 

Saturday, 8 August 2015

Arrays in C++

 Concept of arrays

One of the most important concepts in C++ is arrays.Today I will explain the concept of  arrays with daily life examples.For this article,I am taking help from the Android application "C++ little drops".

Suppose we want to calculate the average of the marks for a class of 20 students ,we certainly do not want to create 30 variables: std1,st2,std3,.......std.20.Instead,we can use a single variable,called an array with 30 elements.

Definition of arrays

An array is a list of elements of the same type(e.g., int), identified by a pair of square brackets [ ]. 

Declaration of arrays

To use an array, we need to declare the array with 3 things: a name, a type and a dimension( or size, or length).Remember that an array always starts with index 0.It means that the first value in an array is stored in index 0.The syntax is as follows:

Syntax

type arrayName[array length];

It is recommended to use a plural name for array.e.g., marks,numbers, books,etc.

int  books[10];

We can also initialize the array during declaration with a comma-separated list of values,as follows:

int numbers{2,4,5,6,3,9,12,14};

Example

A simple program will help you understand this concept more clearly.

Source code

#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int numbers[5]={1,2,3,4,5};  //an array "numbers"
cout<<"The first number: "<<numbers[0]<<endl;  // number stored in index 0;
cout<<"The second number: "<<numbers[1]<<endl;
cout<<"The third number: "<<numbers[2]<<endl;
cout<<"The fourth number: "<<numbers[3]<<endl;
cout<<"The fifth number:"<<numbers[4]<<endl;
}

Output

Output of arrays

Wednesday, 22 July 2015

Convertion of marlas into square feet

My aim is to implement C++ in our daily lives and for this purpose,today I am here with a very easy but interesting example of "switch" statement.This program will convert marlas being enterd by the user into square feet and square feet being enterd by the user into marlas.I have also used the concept of "goto" statement in this program.

Below is the source code of this program.

Source code


#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int choice;
float marlas,squarefeet;
x: //label
cout<<"\t"<<"\t"<<"MAIN MENU"<<"\t"<<"\t"<<endl<<endl;
cout<<"Press 1 for converting marlas into square feet."<<endl<<endl;
cout<<"Press 2 for converting square feet into marlas."<<endl<<endl;
cout<<"Enter your choice:";
cin>>choice;
cout<<endl;
switch(choice)
{
case 1:
cout<<"Enter the required marlas:";
cin>>marlas;
cout<<endl;
squarefeet=marlas*5399.21;
cout<<"The required square feet="<<squarefeet<<endl<<endl;
break;
case 2:
cout<<"Enter the required square feets:";
cin>>squarefeet;
cout<<endl;
marlas=squarefeet*0.0001;
cout<<"The required marlas="<<marlas<<endl<<endl;
break;
default:
cout<<"Invalid choice."<<endl;
}
goto x;
}

Output

convertion of marlas into square units

Explanation

As we know that the formula for converting marlas into square feet is as follows:

squarefeet=marlas*5399.21;

The formula for converting square feet into marlas is as follows:

marlas=squarefeet*0.0001;

The "goto" statement used in the program will repeat the menu again and again.