In: Computer Science
c++
Create a one-dimension array, y, of this struct data type with a length 4. Pass y into a function: PrintArrayElement( …). Finish the interface of this function, and loop over each array element inside this function
Required program in c++ with comments:
#include <iostream>
#include <cstring>
using namespace std;
//Struct data type for a student's details
struct student
{
  int roll_no;
  string name;
  int phone_number;
};
//Print array elements
/**
 *@param: struct[]: struct type array with information
 * @param: len: length of the struct type array
 */
void PrintArrayElement (struct student y[ ], int len);
int main(){
    //array of struct data type
        struct student y[4];
        int i;
        for(i=0; i<4; i++){  //taking values from user
                cout << "Student " << i + 1 << endl;
                cout << "Enter roll no" << endl;
                cin >> y[i].roll_no;
                cout << "Enter name" << endl;
                cin >> y[i].name;
                cout << "Enter phone number" << endl;
                cin >> y[i].phone_number;
        }
        
    PrintArrayElement(y,4);
        return 0;
}
/**
 * Print array elements
 *@param: struct[]: struct type array with information
 * @param: len: length of the struct type array
 */
void PrintArrayElement(struct student y[ ], int len)
{
    int i;
    for(i=0; i<len; i++){    //printing values
                cout << "Student " << i + 1 << endl;
                cout << "Roll no : " << y[i].roll_no << endl;
                cout << "Name : " << y[i].name << endl;
                cout << "Phone no : " << y[i].phone_number << endl;
        }
}


Output:
C++ shell
cpp.sh
online C++ compiler
about cpp.sh
RunGet URL
optionscompilationexecution
Student 1
Enter roll no
1
Enter name
Tom
Enter phone number
43333333
Student 2
Enter roll no
2
Enter name
John
Enter phone number
544444443
Student 3
Enter roll no
3
Enter name
Marry
Enter phone number
65555555
Student 4
Enter roll no
4
Enter name
Tommy
Enter phone number
34555555
Student 1
Roll no : 1
Name : Tom
Phone no : 43333333
Student 2
Roll no : 2
Name : John
Phone no : 544444443
Student 3
Roll no : 3
Name : Marry
Phone no : 65555555
Student 4
Roll no : 4
Name : Tommy
Phone no : 34555555
 
Exit code: 0 (normal program termination)
C++ Shell, 2014-2015
Screenshot:
