Object

Write a program that demonstrates the process of arrays of structures.

Algorithm

  1. Declare an integer variable i.
  2. Declare a structure with the tag entry.
  3. Declare strings fname that contains 20 elements, lname that contains 20 elements and phone that contains 15 elements as the members of structure.
  4. Declare a variable list[4] of data type struct entry.
  5. Repeat FOR i = 0 to 3 by 1
    • Read structure members list[i].fname, list[i].lname and list[i].phone.
    • [End of FOR loop.]
  6. Repeat FOR i = 0 to 3 by 1
    • Write structure members list[i].fname, list[i].lname and list[i].phone.
    • [End of FOR loop.]
  7. Exit.

Flowchart

C++ Source Code

// program 100
#include<iostream>
using namespace std;

int main() 
{
  int i;
  
struct entry 
{
  char fname[20];
  char lname[20];
  char phone[15];
};
  struct entry list[4];

  for (i = 0; i < 4; i++) 
  {
    cout << "\nEnter your first name: ";
    cin >> list[i].fname;
    cout << "Enter your last name: ";
    cin >> list[i].lname;
    cout << "Enter your phone no.: ";
    cin >> list[i].phone;
  }

system("cls");
  cout << "\n\tDEMONSTRATES ARRAYS OF STRUCTURES";
  for (i = 0; i < 4; i++) 
  {
    cout << "\n\n\tName: " << list[i].fname << " " << list[i].lname;
    cout << "\n\tPhone Number: " << list[i].phone;
  }

  return 0;
}

C Source Code

/*program 100*/
#include<stdio.h>
#include <stdlib.h>

int main()
{
  int i;
  struct entry
  {
   char fname[20];
   char lname[20];
   char phone[15];
  };
  struct entry list[4];
  for(i=0; i<4; i++)
     {
     printf("\nEnter your first name: ");
     scanf("%s", &list[i].fname);
     printf("Enter your last name: ");
     scanf("%s", &list[i].lname);
     printf("Enter your phone no.: ");
     scanf("%s", &list[i].phone);
     }

  system("cls");
  printf("\n\tDEMONSTRATES ARRAYS OF STRUCTURES");
  for(i=0; i<4; i++)
     {
     printf("\n\n\tName: %s %s", list[i].fname, list[i].lname);
     printf("\n\tPhone Number: %s", list[i].phone);
     }
  return 0;
}

Output

Input given
Enter your first name: Amir
Enter your last name: Saleem
Enter your phone no.: 021-123456

Enter your first name: Abdul
Enter your last name: Qadeer
Enter your phone no.: 021-234567

Enter your first name: Hadia
Enter your last name: Shahzad
Enter your phone no.: 021-345678

Enter your first name: Noor
Enter your last name: Abbas
Enter your phone no.: 021-456789
Output

        DEMONSTRATES ARRAYS OF STRUCTURES

        Name: Amir Saleem
        Phone Number: 021-123456

        Name: Abdul Qadeer
        Phone Number: 021-234567

        Name: Hadia Shahzad
        Phone Number: 021-345678

        Name: Noor Abbas
        Phone Number: 021-456789
Design a site like this with WordPress.com
Get started