Question

In: Computer Science

c++ Create a one-dimension array, y, of this struct data type with a length 4. Pass...

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

Solutions

Expert Solution

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:


Related Solutions

Using C++ language, create a program that uses a struct with array variables that will loop...
Using C++ language, create a program that uses a struct with array variables that will loop at least 3 times and get the below information: First Name Last Name Job Title Employee Number Hours Worked Hourly Wage Number of Deductions Claimed Then, determine if the person is entitled to overtime and gross pay. Afterwards, determine the tax and net pay. Output everything to the screen. Use functions wherever possible. Bonus Points: Use an input file to read in an unknown...
Name your c++ file Word_LastNameFirstName.cpp. Create a struct that has a word and the length of...
Name your c++ file Word_LastNameFirstName.cpp. Create a struct that has a word and the length of the word. Next, use the struct to store the word read from a text file call “word.txt” and the length of the word. Print out the word x number of times based on the length of the word as shown below. Also, print a statement with the length and determine if the string length has an odd number or even number of characters. You...
Create a C++ program that creates instances of a Struct using an Array (you can choose...
Create a C++ program that creates instances of a Struct using an Array (you can choose the number of instances to create). You can also use either user input or an initialization list to initialize 3 peoples names. Make sure that the values stored in each member are printed to the screen.
Write a program in c++ that reads x[4]. Then create the array y[6||6], such that the...
Write a program in c++ that reads x[4]. Then create the array y[6||6], such that the first quarter of y consists of the value of the first element of x. The second quarter of y consists of the value of the second element of x. The third quarter of y consists of the value of the third element of x. The fourth quarter of y consists of the value of the fourth element of x?
The goal is to Create an array of struct “employee” Fill the array with information read...
The goal is to Create an array of struct “employee” Fill the array with information read from standard input using C++ style I/O Shuffle the array Select 5 employees from the shuffled array Sort the shuffled array of employees by the alphabetical order of their last Name Print this array using C++ style I/O Random Number Seeding We will make use of the random_shuffle() function from the standard algorithm library. You can use srand() function provided in with a seed...
In C create an array of 4 integers. Assign a pointer to the array. Use the...
In C create an array of 4 integers. Assign a pointer to the array. Use the pointer to find the average value of the elements in the array and display the results on the screen.
Create a C++ program that follows the specifications below: *Define a struct with 4 or more...
Create a C++ program that follows the specifications below: *Define a struct with 4 or more members. *Application must have at least one user-defined function *Declare an array of your struct using a size of 10 or more *Load the date for each element in your array from a text file *Display the data in your array in the terminal *provide brief comments for each line of code
C++ Build a struct with many data members inside (for example, a large array). Design any...
C++ Build a struct with many data members inside (for example, a large array). Design any function that processes the data inside the struct (e.g. adding a few values together and returning the sum.) Write two versions of this function, one that passes by value and one that passes a const reference. Measure the time required to call each function 10,000 times.
c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
(C++ ONLY) You will define your own data type. It will be a struct called Fraction....
(C++ ONLY) You will define your own data type. It will be a struct called Fraction. The struct will have 2 integer fields: numerator and denominator. You will write a function called reduce that takes a Fraction as a parameter and returns that Fraction in its reduced form. For example, if the fraction 2/4 is passed to the function, the fraction 1/2 will be returned. Consider any fraction with a denominator of 1 to be in reduced form. You will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT