Home | | Computer Science 11th std | Example C++ Programs: Arrays and Structures

Computer Science - Example C++ Programs: Arrays and Structures | 11th Computer Science : Chapter 12 : Arrays and Structures

Chapter: 11th Computer Science : Chapter 12 : Arrays and Structures

Example C++ Programs: Arrays and Structures

Computer Science : Arrays and Structures : Example C++ Programs: Arrays and Structures

One-dimensional array


Input values while execution

#include <iostream>

using namespace std;

int main()

{

      int num[5];

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

      {

           cout<< "\n Enter value " << i+1 << "= ";

           cin>>num[i];

      }

}


Accessing array elements

#include <iostream>

using namespace std;

int main()

{

      int num[5] = {10, 20, 30, 40, 50};

      int t=2;

      cout<<num[2] <<endl; // S1

      cout<<num[3+1] <<endl; // S2

      cout<<num[t=t+1]; // S3

}

output:

30

50

40


Program to read and write the values from an array

#include <iostream>

using namespace std;

int main()

{

      int age[4];//declaration of array

      cout<< "Enter the age of four persons:" <<endl;

      for(int i = 0; i < 4; i++)//loop to write array elements

      cin>> age[i];

      cout<<"The ages of four persons are:";

      for(int j = 0; j< 4; j++)

           cout<< age[j]<<endl;

}

Output:

Enter the age of four persons:

18

17

21

23

The ages of four persons are:

18

17

21

23


Traversal of an array

#include <iostream>

using namespace std;

int main()

{

      int num[5];

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

      {

           cout<< "\n Enter value " << i+1 <<"= ";

           cin>>num[i]; // Reading from keyboard

           //Traversing the array elements sequentially and adding 1 to each element num[i] = num[i] + 1;

      }

      cout<< "\n After incrementing, the values in array num..." <<endl;

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

      {

      //Traversing the array elements sequentially and printing each one of them cout<<num[j] <<endl;

      }

}

Output:

Enter value 1= 10

Enter value 2= 20

Enter value 3= 30

Enter value 4= 40

Enter value 5= 50

After incrementing, the values in array num...

11

21

31

41

51


Program to read the marks of 10 students and to find the average of all those marks.

#include <iostream>

using namespace std;

int main()

{

      int marks[10], sum=0;

      float avg;

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

      {

           cout<< "\n Enter Mark " << i+1 << "= ";

           cin>> marks[i];

           sum=sum+marks[i];

      }

      avg=sum/10.0;

      cout<< "\n The Total Marks: " << sum;

      cout<< "\n The Average Mark: " <<avg;

}

Output:

Enter Mark 1= 41

Enter Mark 2= 98

Enter Mark 3= 65

Enter Mark 4= 75

Enter Mark 5= 35

Enter Mark 6= 82

Enter Mark 7= 64

Enter Mark 8= 5

Enter Mark 9= 58

Enter Mark 10= 68

The Total Marks: 591

The Average Mark: 59


C++ program to inputs 10 values and count the number of odd and even numbers

#include <iostream>

using namespace std;

int main()

{

      int num[10], even=0, odd=0;

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

      {

           cout<< "\n Enter Number " << i+1 <<"= ";

           cin>>num[i];

           if (num[i] % 2 == 0)

           ++even;

           else

           ++odd;

      }

      cout << "\n There are "<< even <<" Even Numbers";

      cout << "\n There are "<< odd <<" Odd Numbers";

}

Output:

Enter Number 1= 78

Enter Number 2= 51

Enter Number 3= 32

Enter Number 4= 66

Enter Number 5= 41

Enter Number 6= 68

Enter Number 7= 27

Enter Number 8= 65

Enter Number 9= 28

Enter Number 10= 94

There are 6 Even Numbers

There are 4 Odd Numbers


Program to read the prices of 10 products in an array and then print the sum and average of all the prices

#include <iostream>

using namespace std;

int main()

{

      float price[10], sum=0, avg=0, prod=1;

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

      {

           cout<< "\n Enter the price of item " << i+1 <<"= ";

           cin>> price[i];

           sum+=price[i];

      }

avg=sum/10.0;

cout<< "\n Sum of all prices: " << sum;

cout<< "\n Average of all prices: " <<avg;

}

 

Program to accept the sales of each day of the month and print the average sales for each month

#include <iostream>

using namespace std;

int main()

{

      int days;

      float sales[5], avgSales=0, totalSales=0;

      cout<< "\n Enter No. of days: ";

      cin>> days;

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

      {

           cout<< "\n Enter sales on day - " << i+1 <<": ";

           cin>> sales[i];

           totalSales+=sales[i];

}

avg=total sales/days;

cout<< "\n Average Sales = " <<avgSales;

return 0;

}

Program for Linear Search

#include <iostream>

using namespace std;

int Search(int arr[], int size, int value)

{

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

      {

           if (arr[i] == value)

           return i; // return index value

      }

return -1;

}

int main()

{

      int num[10], val, id;

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

      {

      cout<< "\n Enter value " << i+1 <<"= ";

      cin>>num[i];

      }

      cout<< "\n Enter a value to be searched: ";

      cin>>val;

      id=Search(num,10,val);

      if(id==-1)

      cout<< "\n Given value is not found in the array.."; else

      cout<< "\n The value is found at the position" << id+1; return 0;

}


Program to demonstrate a character array.

#include <iostream>

using namespace std;

int main()

{

      char country[6];

      cout<< "Enter the name of the country: ";

      cin>>country;

      cout<<" The name of the country is "<<country;

}

OUTPUT

Enter country the name: INDIA

The country name is INDIA


Write a program to demonstrate various methods of initializing the character arrays

#include <iostream>

using namespace std;

int main()

{

      char arr1[6]="INDIA";

      char arr2[6]={'I','N','D','I','A','\0'};

      char arr3[]="TRICHY";

      char arr4[]={'T','R','I','C','H','Y','\0'};

      char arr5[8]="TRICHY";

      cout<<"arr1 is :" <<arr1<< " and its size is "<<sizeof(arr1)<<endl;

      cout<<"arr2 is :" <<arr2<< " and its size is "<<sizeof(arr2)<<endl;

      cout<<"arr3 is :" <<arr3<< " and its size is "<<sizeof(arr3)<<endl;

      cout<<"arr4 is :" <<arr4<< " and its size is "<<sizeof(arr4)<<endl;

      cout<<"The elements of arr5"<<endl;

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

      cout<<arr5[i]<<" ";

      return 0;

}

Output

arr1 is :INDIA and its size is 6

arr2 is :INDIA and its size is 6

arr3 is :TRICHY and its size is 7

arr4 is :TRICHY and its size is 7

The elements of arr5

T R I C H Y


Write a program to display a line of text using get() function.

//str10.cpp

//To read a line of text

#include <iostream>

using namespace std; int main()

{

char str[100];

cout<< "Enter a string: ";

cin.get(str, 100);

cout<< "You entered: " <<str<<endl;

return 0;

}

Output

Enter a string: I am a student

You entered: I am a student


Write a Program to check palindrome or not

#include<iostream>

using namespace std;

int main( )

{

      int i, j, len, flag =1;

      char a [20];

      cout<<"Enter a string:";

      cin>>a;

      for(len=0;a[len]!='\0';++len)

for(!=0,j=len-1;i<len/2;++i,--j)

      {

      if(a[j]!=a[i])

           flag=0;

      }

      if(flag==1)

           cout<<"\n The String is palindrome";

      else

           cout<<"\n The String is not palindrome";

      return 0;

}

Output:

Enter a string : madam

The String is palindrome


Two-dimensional array


Write a program to perform addition of two matrices

#include<iostream>

#include<conio>

using namespace std;

int main()

{

      int row, col, m1[10][10], m2[10][10], sum[10][10];

      cout<<"Enter the number of rows : ";

      cin>>row;

      cout<<"Enter the number of columns : ";

      cin>>col;

      cout<< "Enter the elements of first matrix: "<<endl;

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

      for (int j = 0;j <col;j++ )

      cin>>m1[i][j];

      cout<< "Enter the elements of second matrix: "<<endl;

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

      for (int j = 0;j<col;j++ )

      cin>>m2[i][j];

      cout<<"Output: "<<endl;

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

      for (int j = 0;j<col;j++ )

      {

      sum[i][j]=m1[i][j]+m2[i][j];

      cout<<sum[i][j]<<" ";

      }

      cout<<endl<<endl;

      }

getch();

return 0;

}



Array of strings


C++ program to demonstrate array of strings using 2d character array

#include<iostream>

using namespace std;

int main( )

{

      // initialize 2d array

      char colour [4][10]={"Blue","Red","Orange", "yellow"};

      // printing strings stored in 2d array

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

      cout << colour [i] << "\n";

}

Output:

Blue

Red

Orange

Yellow


Passing Arrays to functions


C++ program to display marks of 5 students (one dimensional array)

#include <iostream>

using namespace std;

void display (int m[5]);

int main()

{

      int marks[5]={88, 76, 90, 61, 69};

      display(marks);

      return 0;

}

void display (int m[5])

{

      cout << "\n Display Marks: " << endl;

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

      {

           cout << "Student " << i+1 << ": " << m[i]<<endl;

      }

}

Output:

Display Marks:

Student 1: 88

Student 2: 76

Student 3: 90

Student 4: 61

Student 5: 69


C++ program to display values from two dimensional array

#include <iostream>

using namespace std;

void display (int n[3][2]);

int main()

{

      int num[3][2] = { {3, 4}, {9, 5}, {7, 1} };

      display(num);

      return 0;

}

void display(int n[3][2])

{

      cout << "\n Displaying Values" << endl;

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

           {

           for (int j=0; j<2; j++)

           {

           cout << n[i][j] << " ";

           }

           cout << endl << endl;

           }

}

Output:

Displaying Values

3 4

9 5

7 1

In the above program, the two-dimensional array num is passed to the function display() to produce the results.

 

Function with character array as argument

#include <iostream>

using namespace std;

int main()

{

      char str[100];

void display(char s[]);

      cout<< "Enter a string: ";

      getline(cin, str);

display(str);

      return 0;

}

 

void display(char s[])

{

cout<< "You entered char array: " << s <<endl;

}

output

Enter a string: welcome to C++ programming

You entered char array: welcome to C++ programming


Structure 

The following C++ program reads student information through keyboard and displays the same

#include <iostream>

using namespace std;

struct Student

{

      int age;

      float height, weight;

} mahesh; void main( )

{

      cout<< “ Enter the age:”<<endl;

      cin>>mahesh.age;

      cout<< “Enter the height:”<<endl;

      cin>>mahesh.height;

      cout<< “Enter the weight:”<<endl;

      cin>>mahesh.weight;

      cout<< “The values entered for Age, height and weight are”<<endl;

      cout<<mahesh.age<< “\t”<<mahesh.height<< “\t”<<Mahesh. weight;

}

Output:

Enter the age:

18

Enter the height:

160.5

Enter the weight:

46.5

The values entered for Age, height and weight are

18  160.5    46.5

 

The following C++ Program assigns data to members of a structure variable and displays the contents

#include<iostream>

using namespace std;

struct Employee

{

char name[50];

int age;

      float salary;

};

int main()

{

      Employee e1;

      cout<< "Enter Full name: ";

      cin>>e1.name;

      cout<<endl<<"Enter age: ";

      cin>>e1.age;

      cout<<endl<< "Enter salary: ";

      cin>>e1.salary;

      cout<< "\nDisplaying Information." <<endl;

      cout<< "Name: " <<e1.name <<endl;

      cout<<"Age: " <<e1.age <<endl;

      cout<< "Salary: " <<e1.salary;

return 0;

}

Output:

Enter Full name:

Ezhil

Enter age:

27

Enter salary:

40000.00

Displaying Information.

Name: Ezhil

Age: 27

Salary: 40000.00


nested structures act as members of another structure and the members of the child structure can be accessed as parent structure name. Child structure name. Member name.

struct Student

{

      int age;

      float height, weight;

      struct dob

      {

      int date;

      char month[4];

      int year;

      };

}mahesh;

void main( )

{

      cout<< “ Enter the age:”<<endl;

      cin>>mahesh.age;

      cout<< “Enter the height:”<<endl;

      cin>>mahesh.height;

      cout<< “Enter the weight:”<<endl;

      cin>>mahesh.weight;

      cout<< “The Date of birth:”<<endl;

      cout<< “ Enter the day:”<<endl; cin>>mahesh.dob.date;

      cout<< “Enter the month:”<<endl;

      cin>>mahesh.dob.month;

      cout<< “Enter the year:”<<endl;

      cin>>mahesh.dob.year;

      cout<< “The values entered for Age, height and weightare”<<endl;

      cout<<mahesh.age<< “\t”<<mahesh.height<< “\t”<<mahesh.weight<<endl;

      cout<< “His date of Birth is:”<<endl<<mahesh.dob.date<< “-”<<mahesh.

      dob.month<< “-” <<mahesh.dob.year;

}

Output:

Enter the age:

18

Enter the height:

160.5

Enter the weight:

46.5

The Date of birth

Enter the day:

25

Enter the month:

NOV

Enter the year:

2017

The values entered for Age, height and weight are

18  160.5  46.5

His date of Birth is:

25-NOV-2017


The following program reads the details of 20 students and prints the same.

#include <iostream>

using namespace std;

struct Student

{

      int age;

      float height, weight;

      char name[30];

};

void main( )

{

Student std[20];

int i;

      cout<< “ Enter the details for 20 students”<<endl;

      for(i=0;i<20;i++)

{

      cout<< “ Enter the details of student”<<i+1<<endl;

      cout<< “ Enter the age:”<<endl; cin>>std[i].age;

      cout<< “Enter the height:”<<endl;

      cin>>std[i].height;

      cout<< “Enter the weight:”<<endl;

      cin>>std[i].weight;

}

      cout<< “The values entered for Age, height and weight are”<<endl;

      for(i=0;i<20;i++)

      cout<<”Student ”<<i+1<< “\t”<<std[i].age<< “\t”<<std[i].height<< “\t”<<std[i].weight;

}

Output:

Enter the details of 20 students

Enter the details for student1

Enter the age:

18

Enter the height:

160.5

Enter the weight:

46.5

Enter the details for student2

Enter the age:

18

Enter the height:

164.5

Enter the weight:

61.5

.......................

.....................

The values entered for Age, height and weight are

Student 1    18      160.5 46.5

Student 2    18      164.5 61.5


Let an organization has three employees.If we want to read and print all their details thenan array of structures is desirable for employees of this organization. This canbe done through declaring an array of employee structures.

include<iostream>

using namespace std;

struct Employee

{

char name[50];

int age;

float salary;

};

int main()

{

Employee e1[3];

int i;

cout<< “Enter the details of 3 employees”<<endl;

for(i=0;i<3;i++)

{

      cout<< “Enter the details of Employee”<< i+1<<endl;

      cout<< "Enter name: ";

      cin>>e1[i].name;

      cout<<endl<<"Enter age: ";

      cin>>e1[i].age;

      cout<<endl<< "Enter salary: ";

      cin>>e[i]1.salary;

}

      cout<< "Displaying Information" <<endl;

      for(i=0;i<3;i++)

{

      cout<< “ The details of Employee” <<i+1<<endl;

      cout<< "Name: " <<e1[i].name <<endl;

      cout<<"Age: " <<e1[i].age <<endl;

      cout<< "Salary: " <<e1[i].salary;

return 0;

}

Output:

Enter the details of 3 employees

Enter the details of Employee 1

Enter name:

Lilly

Enter age:

42

Enter salary:

40000.00

Enter the details of Employee 2

Enter name:

Aster

Enter age:

38

Enter salary:

60000.00

Enter the details of Employee 3

Enter name:

Jasmine

Enter age:

45

Enter salary:

80000.00

Displaying Information.

The details of Employee 1

Name:Lilly

Age: 42

Salary: 40000.00

The details of Employee 2

Name:Aster

Age:38

Salary:60000.00

The details of Employee 3

Name:Jasmine

Age:45

Salary:80000.00


Passing structures to functions

example:

#include <iostream>

using namespace std;

struct Employee

{

char name[50];

int age;

float salary;

};

void printData(Employee);  // Function declaration

int main()

{

      Employee p;

      cout<< "Enter Full name: ";

      cin>>p.name;

      cout<< "Enter age: ";

      cin>>p.age;

      cout<< "Enter salary: ";

      cin>>p.salary;

      //Function call with structure variable as an argument

      printData(p);

return 0;

}

void printData(Employee q)

{

      cout<< "\nDisplaying Information." <<endl;

      cout<< "Name: " << q.name <<endl;

      cout<<"Age: " <<q.age<<endl;

      cout<< "Salary: " <<q.salary;

}

Output:

Enter Full name: Kumar

Enter age: 55

Enter salary: 34233.4

Displaying Information.

Name: Kumar

Age: 55

Salary: 34233.4


#include <iostream>

using namespace std;

struct Employee {

char name[50];

int age;

float salary;

};

Employee readData(Employee);

void printData(Employee);

int main()

{

      Employee p;

      p = readData(p);

      printData(p);

return 0;

}

      Employee readData(Employee p) {

      cout<< "Enter Full name: ";

      cin.get(p.name, 50);

      cout<< "Enter age: ";

      cin>>p.age;

      cout<< "Enter salary: ";

      cin>>p.salary;

return p;

}

void printData(Employee p)

{

      cout<< "\nDisplaying Information." <<endl;

      cout<< "Name: " << p.name <<endl;

      cout<<"Age: " <<p.age<<endl;

      cout<< "Salary: " <<p.salary;

}

Output:

Enter Full name: Kumar

Enter age: 55

Enter salary: 34233.4

Displaying Information.

Name: Kumar

Age: 55

Salary: 34233.4


#include <iostream>

using namespace std;    

struct Employee {

char name[50];

int age;

float salary;

};

void readData(Employee &);

void printData(Employee);

int main() {

      Employee p;

       readData(p);

      printData(p);

return 0;

}

void readData(Employee &p) {

      cout<< "Enter Full name: ";

      cin.get(p.name, 50);

      cout<< "Enter age: ";

      cin>>p.age;

      cout<< "Enter salary: ";

      cin>>p.salary;

}

void printData(Employee p)

{

      cout<< "\nDisplaying Information." <<endl;

      cout<< "Name: " << p.name <<endl;

      cout<<"Age: " <<p.age<<endl;

      cout<< "Salary: " <<p.salary;

}

Output:

Enter Full name: Kumar

Enter age: 55

Enter salary: 34233.4

Displaying Information.

Name: Kumar

Age: 55

Salary: 34233.4


Similar to built-in data types, structures also can be returned from a function.

#include<iostream.h>

using namespace std;

struct Employee

{

int Id;

char Name[25];

int Age;

long Salary;

};

      Employee Input();

void main()

{

      Employee e;

      Emp = Input();

      cout<< “The values Entered are”<<endl:

      cout<< "\n\nEmployee Id : " <<e.Id;

      cout<< "\nEmployee Name : " <<e.Name;

      cout<< "\nEmployee Age : " <<e.Age;

      cout<< "\nEmployee Salary : " <<e.Salary;

}

      Employee Input()

{

      Employee e;

      cout<< "\nEnter Employee Id : ";

      cin>>e.Id;

      cout<< "\nEnter Employee Name : ";

      cin>>e.Name;

      cout<< "\nEnter Employee Age : ";

      cin>>e.Age;

      cout<< "\nEnter Employee Salary : ";

      cin>>e.Salary;

return;

}

Output :

Enter Employee Id : 10

Enter Employee Name : Ajay

Enter Employee Age : 25

Enter Employee Salary : 15000

Employee Id : 10

Employee Name : Ajay

Employee Age : 25

Employee Salary : 15000



Tags : Computer Science , 11th Computer Science : Chapter 12 : Arrays and Structures
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 12 : Arrays and Structures : Example C++ Programs: Arrays and Structures | Computer Science


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.