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

Example Programs 2 Structure

The document contains 8 code examples demonstrating various concepts in C++. The first two examples show how to display and add array elements using functions and pointers. The next three examples demonstrate adding, subtracting, and multiplying complex numbers using structures and functions. The sixth example adds complex numbers using structure pointers. The seventh compares structures and unions. The final example creates a dynamic array using new and delete operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views8 pages

Example Programs 2 Structure

The document contains 8 code examples demonstrating various concepts in C++. The first two examples show how to display and add array elements using functions and pointers. The next three examples demonstrate adding, subtracting, and multiplying complex numbers using structures and functions. The sixth example adds complex numbers using structure pointers. The seventh compares structures and unions. The final example creates a dynamic array using new and delete operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1. C++ Program to display the array elements using function and pointers.

#include<iostream>

using namespace std;

void disparr(int[],int);

int main()

int arr[20],n;

cout<<"Enter the size of the array: ";

cin>>n;

cout<<"\n Enter the array elements: ";

for(int i=0;i<n;i++)

cin>>arr[i];

cout<<"The array elements are: "<<endl;

disparr(arr,n);

void disparr(int *j, int size)

for(int i=0;i<size;i++)

cout<<*j<<" ";

j++;

}
2. C++ Program to add two complex numbers using structure.

#include<iostream>

using namespace std;

typedef struct

float real;

float img;

} Complex;

int main()

Complex A,B,C;

cout<<"Enter real and imaginary parts of A: ";

cin>>A.real>>A.img;

cout<<"Enter real and imaginary parts of B: ";

cin>>B.real>>B.img;

C.real=A.real+B.real;

C.img=A.img+B.img;

cout<<"The sum is: "<<C.real<<"+"<<C.img<<"i";

3. C++ Program to add two complex numbers using structure and function.

#include<iostream>

using namespace std;

typedef struct
{

float real;

float img;

} Complex;

void sum(Complex &,Complex &);

int main()

Complex A,B;

cout<<"Enter real and imaginary parts of A: ";

cin>>A.real>>A.img;

cout<<"Enter real and imaginary parts of B: ";

cin>>B.real>>B.img;

sum(A,B);

void sum(Complex &P, Complex &Q)

Complex C;

C.real=P.real+Q.real;

C.img=P.img+Q.img;

cout<<"The sum is: "<<C.real<<"+"<<C.img<<"i";

}
4. C++ Program to subtract two complex numbers using structure and function.

#include<iostream>

using namespace std;

typedef struct

float real;

float img;

} Complex;

void diff(Complex &,Complex &);

int main()

Complex A,B;

cout<<"Enter real and imaginary parts of A: ";

cin>>A.real>>A.img;

cout<<"Enter real and imaginary parts of B: ";

cin>>B.real>>B.img;

diff(A,B);

void diff(Complex &P, Complex &Q)

Complex C;

C.real=P.real-Q.real;

C.img=P.img-Q.img;
cout<<"The difference is: "<<C.real<<"+"<<C.img<<"i";

5. C++ Program to multiply two complex numbers using structure and function.

#include<iostream>

using namespace std;

typedef struct

float real;

float img;

} Complex;

void mul(Complex &,Complex &);

int main()

Complex A,B;

cout<<"Enter real and imaginary parts of A: ";

cin>>A.real>>A.img;

cout<<"Enter real and imaginary parts of B: ";

cin>>B.real>>B.img;

mul(A,B);

void mul(Complex &P, Complex &Q)

Complex C;
C.real=P.real*Q.real-P.img*Q.img;

C.img=P.real*Q.img+P.img*Q.real;

cout<<"The product is: "<<C.real<<"+"<<C.img<<"i";

6. C++ Program to add two complex numbers using structure pointers.

#include<iostream>

using namespace std;

typedef struct

float real;

float img;

} Complex;

void sum(Complex *,Complex *);

int main()

Complex A,B;

cout<<"Enter real and imaginary parts of A: ";

cin>>A.real>>A.img;

cout<<"Enter real and imaginary parts of B: ";

cin>>B.real>>B.img;

sum(&A,&B);

}
void sum(Complex *P, Complex *Q)

Complex C;

C.real=P->real + Q->real;

C.img=P->img + Q->img;

cout<<"The sum is: "<<C.real<<"+"<<C.img<<"i";

7. C++ Program to show the difference between Structure and Union.

#include<iostream>

using namespace std;

struct Complex1

int real;

float img;

} c1;

union Complex2

int real;

float img;

} u1;

int main()

cout<<"Enter the real and img part of structure: ";


cin>>c1.real>>c1.img;

cout<<"The complex number is: "<<c1.real<<"+"<<c1.img<<"i ";

cout<<"\n The size of the structure is: "<<sizeof(c1)<<endl;

cout<<"Enter the real and img part of union: ";

cin>>u1.real>>u1.img;

cout<<"The complex number is: "<<u1.real<<"+"<<u1.img<<"i "<<endl;

cout<<"\n The size of the union is: "<<sizeof(u1);

8. C++ program to create a dynamic array using new and delete operators.

#include<iostream>

using namespace std;

int main()

int *p=new int[5];

cout<<"Enter the array elements: ";

for(int i=0;i<5;i++)

cin>>*(p+i);

cout<<"\n The array elements are: ";

for(int i=0;i<5;i++)

cout<<*(p+i)<<" ";

delete []p;

You might also like