[ Home Page ] [ Back ] [ Content of Contributions ] [ Movement: Green Leaf World ]
Program Reference: Cpp_Programs/ Day_04/ Prog_01.cpp
/*
Working with Array: Using Library Class Array
Attach basic array functions like search, sort, insert, delete etc
*/
#include<iostream>
using namespace std;
class My_Array
{
private:
int iArr[10], iNoE;
public:
My_Array(){iNoE=0;}
void Insert( int x )
{iArr[iNoE++]= x;}
void Show()
{
cout<<"\n\tContents are:: ";
for(int i=0; i<iNoE; i++)cout<<iArr[i]<<" ";
}
};
int main (void)
{
My_Array t;
cout<<"\nWorking with Array: using Our Class";
cout<<"\n\tInserting 10";
t.Insert(10);
cout<<"\n\tInserting 20";
t.Insert(20);
cout<<"\n\tInserting 30";
t.Insert(30);
t.Show();
cout<<"\nEnd of the Program"<<endl<<endl;
return 1;
}
Output
Working with Array: using Our Class
Inserting 10
Inserting 20
Inserting 30
Contents are:: 10 20 30
End of the Program
Program Reference: Cpp_Programs/ Day_04/ Prog_02.cpp
/*
Working with Array: Using Library Class Array
Compile this program using $g++ Prog_02.cpp -std=c++11
*/
#include<iostream>
#include<array>
using namespace std;
int main (void)
{
//DECLARING ARRAY iArr FOR 10 int DATA
array<int, 10> iArr;
cout<<"\nWorking with Array: using Class Array\n";
//Set each cell to 0
iArr.fill(0);
//USING at() FUNCTION
for(int i=0; i<3; i++)
{cout<<"\tEnter Data: "; cin>>iArr.at(i);}
//USING get() FUNCTION
cout<<"\n\t Contents:: ";
cout<<get<0>(iArr)<<" ";
cout<<get<1>(iArr)<<" ";
cout<<get<2>(iArr)<<" ";
//USING at() FUNCTION
cout<<"\n\t Contents:: ";
cout<<(iArr).at(0)<<" ";
cout<<(iArr).at(1)<<" ";
cout<<(iArr).at(2)<<" ";
cout<<"\nEnd of the Program"<<endl<<endl;
return 1;
}
Output
Working with Array: using Class Array
Enter Data: 10
Enter Data: 20
Enter Data: 30
Contents:: 10 20 30
Contents:: 10 20 30
End of the Program
Program Reference: Cpp_Programs/ Day_04/ Prog_03.cpp
/*
Working with Array: Using Library Class Array
*/
#include<iostream>
#include<array>
using namespace std;
int main (void)
{
//DECLARING ARRAY iArr FOR 10 int DATA
array<int, 10> iArr;
cout<<"\nWorking with Array: using Class Array\n";
//Set each cell to 0
iArr.fill(0);
//USING at() FUNCTION
for(int i=0; i<3; i++)
{cout<<"\tEnter Data: "; cin>>iArr.at(i);}
cout<<"\tEnter Data: "; cin>>iArr.back();
//USING size() FUNCTION
cout<<"\n\tContents of the array:: ";
for(int i=0; i<iArr.size(); i++)
{cout<<iArr.at(i)<<" ";}
cout<<"\n\t Content(Begining):: "<<iArr.begin();
cout<<"\n\t Content(End):: "<<iArr.end();
cout<<"\n\t Content(Front):: "<<iArr.front();
cout<<"\n\t Content(Back):: "<<iArr.back();
cout<<"\nEnd of the Program"<<endl<<endl;
return 1;
}
Output
Working with Array: using Class Array
Enter Data: 10
Enter Data: 20
Enter Data: 30
Enter Data: 40
Contents of the array:: 10 20 30 0 0 0 0 0 0 40
Content(Begining):: 0xbf947088
Content(End):: 0xbf9470b0
Content(Front):: 10
Content(Back):: 40
End of the Program
Program Reference: Cpp_Programs/ Day_04/ Prog_04.cpp
/*
Working with Array: Using Library Class Array
*/
#include<iostream>
#include<array>
using namespace std;
int main (void)
{
array<int, 10> iArr1;
array<int, 10> iArr2;
cout<<"\nWorking with Array: using Class Array\n";
iArr1.fill(1);
iArr2.fill(2);
cout<<"\n\t\tContents(1st Array) before swapping: ";
for(int i=0; i<10; i++)
{cout<<iArr1.at(i)<<" ";}
cout<<"\n\t\tContents(2nd Array) before swapping: ";
for(int i=0; i<10; i++)
{cout<<iArr2.at(i)<<" ";}
iArr1.swap(iArr2);
cout<<"\n\n\t\tContents(1st Array) after swapping: ";
for(int i=0; i<10; i++)
{cout<<iArr1.at(i)<<" ";}
cout<<"\n\t\tContents(2nd Array) after swapping: ";
for(int i=0; i<10; i++)
{cout<<iArr2.at(i)<<" ";}
cout<<"\nEnd of the Program"<<endl<<endl;
return 1;
}
Output
Working with Array: using Class Array
Contents(1st Array) before swapping: 1 1 1 1 1 1 1 1 1 1
Contents(2nd Array) before swapping: 2 2 2 2 2 2 2 2 2 2
Contents(1st Array) after swapping: 2 2 2 2 2 2 2 2 2 2
Contents(2nd Array) after swapping: 1 1 1 1 1 1 1 1 1 1
End of the Program