1. In man(), define an integer pointer variable, and use this pointer and new to
ID: 3610569 • Letter: 1
Question
1. In man(), define an integerpointer variable, and use this pointer andnew to dynamically allocate an integer array with10 elements. Then generate 10 random numbers in range (0, 99)using rand() and assign them to the array elements. Use loopto calculate the sum and averageof all these numbers, and output the results. Note that atthe end of the program, you need to use delete todeallocate the array memory. 2. Define a struct workerwhich has two properties: id andwage. id is in integer type, and wage is in thedouble type. Create an array with 6 elements usingworker in main(), and assign the workers' ids from1 to 6, and assign the 6 workers' wage from 1100 to 1600; thendefine a new function and pass the worker array to it. In thefunction, you will double the workers' wage. Finally, inmain(), output the workers' new wage results. Note: It's required to modify the workers' wage in a newfunction other than main(). 1. In man(), define an integerpointer variable, and use this pointer andnew to dynamically allocate an integer array with10 elements. Then generate 10 random numbers in range (0, 99)using rand() and assign them to the array elements. Use loopto calculate the sum and averageof all these numbers, and output the results. Note that atthe end of the program, you need to use delete todeallocate the array memory. 2. Define a struct workerwhich has two properties: id andwage. id is in integer type, and wage is in thedouble type. Create an array with 6 elements usingworker in main(), and assign the workers' ids from1 to 6, and assign the 6 workers' wage from 1100 to 1600; thendefine a new function and pass the worker array to it. In thefunction, you will double the workers' wage. Finally, inmain(), output the workers' new wage results. Note: It's required to modify the workers' wage in a newfunction other than main().Explanation / Answer
//Hope this wil help you..//2.
#include<iostream>
using namespace std;
struct worker {
int id;
double wage;
};
void double_wage(struct worker *w){
int i;
for(i=0;i<6;i++)
w[i].wage = w[i].wage * 2;
}
int main(){
struct worker w[6];
int i;
for(i=0;i<6;i++)
{
w[i].id = i+1;
w[i].wage = (i+11)*100;
}
double_wage(w);
cout<<"After double_wage itis:"<<endl;
for(i=0;i<6;i++)
{
cout<<"Id="<<w[i].id<<endl<<"wage="<<w[i].wage<<endl<<endl;
}
system("pause");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.