1. Create an array of structs containing material properties. There will be 3 fi
ID: 3806723 • Letter: 1
Question
1. Create an array of structs containing material properties. There will be 3 fields: name of material, modulus of elasticity, and yield strength, called “name,” “E,” and “syt” respectively. The struct will be called “material.” Include the following data:
Material Elasticity (GPa) Yield Strength (MPa)
Stainless Steel 193 276
Copper 117 69
Zinc 83 324
2. Create a cell array containing the same data as above.
3. Wrap a function around problem 1. It will have 1 input and no outputs. The input will be the index of the material in the struct (1, 2, or 3). The function will print the name of the material, elasticity, and yield strength to the screen. For example, for an input value of 2 it will print Material copper Elasticity 117 GPa Yield Strength 68 MPa
Explanation / Answer
#include<iostream>
#include<iomanip>
using namespace std;
//Structure Material
struct Material
{
char name[30];
int E;
int syt;
};
//print Material at index
void printMaterial(Material m[],int index)
{
cout<<" Material "<<m[index-1].name<<" Elasticity "<<m[index-1].E<<" GPa Yield Strength "<<m[index-1].syt<<" MPa";
}
int main() {
// Declare an array of Material objects
struct Material m[3];
int i,index;
for(i=0;i<3;i++) //input 3 Materials
{
cin.ignore();
cout<<" Enter name of material : ";
cin.getline(m[i].name,30);
cout<<" Enter modulus of elasticity : ";
cin>>m[i].E;
cout<<" Enter yield strength : ";
cin>>m[i].syt;
}
cout<<" Enter the index of material to show its details :";
cin>>index;
printMaterial(m,index);
return (0);
}
output:
Enter name of material : Stainless Steel
Enter modulus of elasticity : 193
Enter yield strength : 276
Enter name of material : Copper
Enter modulus of elasticity : 117
Enter yield strength : 69
Enter name of material : Zinc
Enter modulus of elasticity : 83
Enter yield strength : 324
Enter the index of material to show its details :2
Material copper Elasticity 117 GPa Yield Strength 68 MPa
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.