Write a program to do some array processing. The following are the functions to
ID: 642961 • Letter: W
Question
Write a program to do some array processing. The following are the functions to handle such array operations. You cannot use the [] syntax on any code in your program, just pointer syntax. I have in my code two arrays declared using [ ]. Change the code to have the arrays declared dynamically.
table_print( int *p, int sz ) print array of size sz
table_add1( int *p, int sz) add one to every element in array of size sz
table_fill( int *p, int sz, int v ) fill array with value v of size sz
table_print_rev( int *p, int sz ) print array in reverse order of size sz
table_copy( int *r, int *s, int sz ) copy array s data into array r of size sz
table_min( int *p, int sz ) min value of array of size sz
table_max( int *p, int sz ) max value of array of size sz
table_avg( int *p, int sz) average of array of size sz
bool table_contain( int *p, int sz, int v) compare to see if v is in the array of size sz
Add to my main program code to determine how many values in the array are smaller than the average using only the functions above to do the work.
Add to my main program code to compute a person
Explanation / Answer
Program Code:
// WorkingWithArrays.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void table_print( int *p, int sz ) ; //print array of size sz
void table_add1( int *p, int sz); //add one to every element in array of size sz
void table_fill( int *p, int sz, int v ); //fill array with value v of size sz
void table_print_rev( int *p, int sz ); //print array in reverse order of size sz
void table_copy( int *r, int *s, int sz );// copy array s data into array r of size sz
int table_min( int *p, int sz ) ; // min value of array of size sz
int table_max( int *p, int sz ); // max value of array of size sz
int table_avg( int *p, int sz); //average of array of size sz
bool table_contain( int *p, int sz, int v) ; // compare to see if v is in the array of size sz
int main()
{
const int Max = 20;
const int MaxGrads=15;
int *ary;
ary=new int[Max];
int *bry;
bry=new int[Max];
int *p, *s, *tp;
p=ary;
cout<<"Enter the elements into the array: ";
for(int i=0;i<MaxGrads;i++)
{
cin>>bry[i];
}
cout << endl << endl;
table_print( bry, MaxGrads );
cout << endl << endl;
cout << endl << "Print reverse order " << endl;
table_print_rev( bry, MaxGrads );
cout << endl << endl;
cout << endl << "Min for previous table " << table_min( bry, MaxGrads );
cout << endl << "Max for previous table " << table_max( bry, MaxGrads);
int avg=table_avg( bry, MaxGrads);
cout << endl << endl;
cout<<"The elements that are less than the average values are: ";
for(int i=0;i<MaxGrads;i++)
{
if(bry[i]<avg)
cout<<bry[i]<<" ";
}
cout<<endl;
cout<<endl;
cout << "The final average of the persons grade is = " << table_avg( bry, MaxGrads);
cout<<endl;
system("pause");
return 0;
}
void table_print( int *p, int sz )
{
cout<<"The elements in the array are: "<<endl;
for(int i=0;i<sz;i++)
cout<<p[i]<<" ";
cout<<endl;
}
void table_add1( int *p, int sz)
{
cout<<"The elements in the array are: "<<endl;
for(int i=0;i<sz;i++)
p[i]=p[i]+1;
}
void table_fill( int *p, int sz, int v )
{
cout<<"The elements in the array are: "<<endl;
for(int i=0;i<sz;i++)
p[i]=v;
}
void table_print_rev( int *p, int sz )
{
cout<<"The elements in the array are: "<<endl;
for(int i=sz-1;i>=0;i--)
cout<<p[i]<<" ";
}
void table_copy( int *r, int *s, int sz )
{
r=s;
}
int table_min( int *p, int sz )
{
int smallest_number=p[0];
for(int i=0;i<sz;i++)
{
if(p[i]<smallest_number)
smallest_number=p[i];
}
return smallest_number;
}
int table_max( int *p, int sz )
{
int largest_number=p[0];
for(int i=0;i<sz;i++)
{
if(p[i]>largest_number)
largest_number=p[i];
}
return largest_number;
}
int table_avg( int *p, int sz)
{
int total=0;
int average=0;
for(int i=0;i<sz;i++)
total+=p[i];
average=total/sz;
return average;
}
bool table_contain( int *p, int sz, int v)
{
for(int i=0;i<sz;i++)
if(p[i]==v)
return true;
return false;
cout<<endl;
}
Sample output:
Enter the elements into the array: 80
85
80
90
94
87
92
90
83
85
87
83
83
83
83
The elements in the array are:
80 85 80 90 94 87 92 90 83 85 87 83 83 83 83
Print reverse order
The elements in the array are:
83 83 83 83 87 85 83 90 92 87
94 90 80 85 80
Min for previous table 80
Max for previous table 94
The elements that are less than the average values are:
80 80 83 83 83 83 83
The final average of the persons grade is = 85
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.