Site icon T4Tutorials.com

Structures Programming Examples in C++

let us see some important Structures Programming Examples in C++.

Structures with user define functions in C++

Let’s see the Sum of series of number program in C++ by using Structures with user define functions.

#include<iostream>
using namespace std;
struct sum
{
	int series;
	int numbers;
    int sum_of_series;
};
int main(){
	sum s;
	s.numbers=10;	 
    s.sum_of_series=0;
for(s.series=1;s.series<=s.numbers;s.series++)
	{
	s.sum_of_series=s.sum_of_series+s.series;
	cout<< s.series;
	}
	cout<<"\n sum:"<<s.sum_of_series<<endl;
	return 0;
}

Output

12345678910

sum: 55

Array of Structures in C++

Let’s see the Sum of series of number programs in C++ with the help of an array of structures.

#include<iostream>
using namespace std;//Array of structure
struct sum
{
	int series;
	int numbers;
	int sum_of_series;

};
int main(){
	sum s[10];
	int i;
	
	s[i].sum_of_series=0;
	s[i].numbers=10;

for(s[i].series=1;s[i].series<=s[i].numbers;s[i].series++)
	{
	s[i].sum_of_series=s[i].sum_of_series+s[i].series;
	cout<<s[i].series;
	}
	cout<<"\n sum:"<<s[i].sum_of_series<<endl;

	return 0;
}

 

Output

12345678910

sum: 55

Array within Structures in C++

Let’s see the Sum of series of number programs in C++ with the help of an array within structures.

#include<iostream>
using namespace  std;
struct sum
{
	int sum_of_series;
	int numbers;
	int series[100];
};
int main(){
	sum s;
	int i;
	s.numbers=12;
    s.sum_of_series=0;
for(s.series[i]=1;s.series[i]<=s.numbers;s.series[i]++)
	{
	s.sum_of_series=s.sum_of_series+s.series[i];
	cout<<s.series[i];
	}
	cout<<"\n sum:"<<s.sum_of_series<<endl;

	return 0;
}

Output

123456789101112

sum: 78

Pass by Value using Structures in C++

Let’s see the Sum of series of number programs in C++ with the help of Pass by Value using Structures in C++.

#include<iostream>
using namespace std; //Pass by value
struct series{
   int sum_of_series;
   int numbers;
};
void sum(struct series a);
	int main(){
	struct series a;

	
	sum(a);
		return 0;	
		
	}
	void sum(struct series a){
		
	int sum_of_series=0;
	int numbers=14;
	
	cout<<"Series:";
		for(int i=1;i<=numbers;i++){
			sum_of_series=sum_of_series+i;
			
			cout<<i<<"+";
			}
		cout<<endl<<"sum_of_series:"<<"="<<sum_of_series<<endl;
		
	}

 

Pass by reference using Structures in C++

Let’s see the Sum of series of number programs in C++ with the help of Pass by reference using Structures in C++.

//***SUM OF SERIES USING STRUCTURE TO FUNCTION_PASSING BY VALUE***//
#include<iostream>
using namespace std;
void pattern(char,int);
struct series{
   int sum_of_series;
   int numbers;
};
void sum(struct series &a);
	int main(){
		cout<<"***Sum of series :using structure to function paasing by reference***"<<endl;
		pattern('*',45);
		struct series a;
		sum(a);
		pattern('*',45);
		return 0;	
		
	}
	void sum(struct series &a){
		int sum_of_series=0;
		int numbers=14;
		cout<<"Series:";
		for(int i=1;i<=numbers;i++){
			
			cout<<i<<"+";
			sum_of_series=sum_of_series+i;
			
		}
		cout<<endl<<"sum_of_series:"<<"="<<sum_of_series<<endl;
		
	}
	void pattern(char ch, int n){
	int	j;
		for(int j=0;j<n;j++)
		cout<<ch;
		cout<<endl;
	}

 

Structures and pointers in C++

Let’s see the Sum of series of number programs in C++ with the help of Structures and pointers in C++.

#include<iostream>//Pointer to structure
using namespace std;
struct sum
{
	int sum_of_series,numbers,series;

};
int main(){
	int series;
	
	sum *ptr,s;
	ptr=&s;
	 (*ptr).sum_of_series=0;
	(*ptr).numbers=4;
	

	//s.numbers=12;
	//s.sum_of_series=0;
	for(series=1;series<=(*ptr).numbers;series++)
	{
	(*ptr).sum_of_series=(*ptr).sum_of_series+series;
	//cout<<"series:"<<series<<endl;
	cout<<series ;
}
	cout<<"\nsum_of_series:"<<(*ptr).sum_of_series<<endl;
	return 0;
}

Output

1234

sum: 10

 

Exit mobile version