Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please the 3 pictures to slove #3 in c++ language #include <iostream> #include <

ID: 3835368 • Letter: P

Question

please the 3 pictures to slove #3 in c++ language

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
using namespace std;
struct employee
{
float salary;
int id;
string lname,fname;
}emp[20];
static int i=0;
void readdata()
{
char line[100];
int j=0;
ifstream in_s;
in_s.open("HW10Prob1.dat");
if(in_s.fail())
{
cout<<"Input file failed to open ";
}
else
{
while (!in_s.eof())
{
while(in_s>>line)
{
char *pch, *pEnd;
pch = strtok (line,"$");
if(j==0)
emp[i].fname=pch;
else if(j==1)
emp[i].lname=pch;
else if(j==2)
emp[i].salary=strtod(pch,&pEnd);
else if(j==3)
emp[i].id=strtol(pch,&pEnd,10);
j++;
if(j==4)
{
j=0;
i++;
}
}
}
}
}
double calcAvg()
{
int k;
double totalsalary=0,avgsalary;
for(k=0;k<i;k++)
{
totalsalary+=emp[k].salary;
}
avgsalary=totalsalary/i;
return avgsalary;
}
void sortdata(employee e[])
{
employee temp;
int k,j;
for(k=0;k<i;k++)
{
for(j=0;j<i;j++)
{
if(e[k].salary<e[j].salary)
{
temp=e[k];
e[k]=e[j];
e[j]=temp;
}
}
}
}

int main()
{
readdata();
sortdata(emp);
int k;
cout<<" Name Salary ID";
cout<<" ------------------------------------";
for(k=0;k<i;k++)
{
cout<<endl<<emp[k].fname<<" "<<emp[k].lname<<" "<<emp[k].salary<<" "<<emp[k].id;
}
cout<<endl;
cout<<" Average Salary : $"<<calcAvg();
cout<<endl;
return 0;
}

.

00 points) Problem 3. Dynamic Memory Allocation Re-do Problem 2, but this time you cannot assume an array size of 20. Instead ask the user for the size of the array and using dynamic memory allocation, create an array of the appropriate size. Assume that the user knows the exact of the array the program will need to process. HINT: This is the same as dynamic memory allocation that we saw in Chapter 9 Remember, what we use in dynamic memory allocation--a data type, a corresponding pointer, and an array size. Think what should be the data type and array size for your program.

Explanation / Answer

ANSWER::

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
   clrscr();
   int *val, *rows, *cols;
   int maxr, maxc, i, j;
   cout<<"Enter the dimension of the array (row col): ";
   cin>>maxr>>maxc;
   val = new int[maxr * maxc];
   rows = new int[maxr];
   cols = new int[maxc];

   for(i=0; i<maxr; i++)
   {
       cout<<" Enter elements for row "<<i+1<<" : ";
       rows[i] = 0;
       for(j=0; j<maxc; j++)
       {
           cin>>val[i*maxc + j];
           rows[i] = rows[i] + val[i*maxc + j];
       }
   }

   for(j=0; j<maxc; j++)
   {
       cols[j] = 0;
       for(i=0; i<maxr; i++)
       {
           cols[j] = cols[j] + val[i*maxc + j];
       }
   }
   for(i=0; i<maxr; i++)
   {
       for(j=0; j<maxc; j++)
       {
           cout<<val[i*maxc + j]<<" ";
       }
       cout<<rows[i]<<" ";
   }

   for(j=0; j<maxc; j++)
   {
       cout<<cols[j]<<" ";
   }
   cout<<" ";

   getch();
}