Please help me with this program! I have been trying to get it to work for days
ID: 643534 • Letter: P
Question
Please help me with this program! I have been trying to get it to work for days now with no success.
Write a C++ 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
#include <iostream>
using namespace std;
void table_print( int *p, int sz ){
for(int i = 0; i < sz; ++i){
cout << *(p + i) << " ";
}
cout << endl;
}
void table_add1(int *p, int sz){
for(int i = 0; i < sz; ++i){
*(p + i) += 1;
}
}
void table_fill( int *p, int sz, int v ){
for(int i = 0; i < sz; ++i){
*(p + i) = v;
}
}
void table_print_rev( int *p, int sz ){
for(int i = 0; i < sz; ++i){
cout << *(p + sz - 1 - i) << " ";
}
cout << endl;
}
void table_copy( int *r, int *s, int sz ){
for(int i = 0; i < sz; ++i){
*(r + i) = *(s + i);
}
}
int table_min( int *p, int sz ){
int min = 55555;
for(int i = 0; i < sz; ++i){
if(*(p + i) < min){
min = *(p + i);
}
}
return min;
}
int table_max( int *p, int sz ){
int max = -55555;
for(int i = 0; i < sz; ++i){
if(*(p + i) > max){
max = *(p + i);
}
}
return max;
}
double table_avg( int *p, int sz){
double sum = 0;
for(int i = 0; i < sz; ++i){
sum += *(p + i);
}
return sum / sz;
}
int main()
{
const int Max = 20;
int ary[Max]; // ............ replace these two statements with dynamically allocated arrays.
int bry[Max];
int *p, *s, *tp;
p = &ary[0]; // ............ change &ary[0] to above used stements.
s = &bry[0]; // ............ change &bry[0] to above used stements.
table_fill( p, Max, 10 );
cout << endl << "Fill array with 10 " << endl;
table_print( p, Max ); cout << endl << endl;
cout << endl << "Add 1 to first 10 elements " << endl;
table_add1( p, 10);
table_print( p, Max ); cout << endl << endl;
tp = s; // save pointer
for (int i=1; i<= Max; i++ )
{
table_fill( s, 1, i );
s++;
}
cout << endl << "Fill array with 1 to 20 " << endl;
s = tp; // restore pointer
table_print( s, Max ); cout << endl << endl;
cout << endl << "Print reverse order " << endl;
table_print_rev( s, Max ); cout << endl << endl;
table_fill( p, Max, 0 );
cout << endl << "Zero out array " << endl;
table_print( p, Max ); cout << endl << endl;
s = p; // What's happening here?
for (int i=Max; i>=0; i--)
{
table_add1( s, i );
}
cout << endl << "Fill array with ???? " << endl;
table_print( p, Max ); cout << endl << endl;
cout << endl << "Print reverse order " << endl;
table_print_rev( p, Max ); cout << endl << endl;
for (int i=0; i<Max/2+1; i++)
{
s = p+10+i;
table_fill( s, 1, i*2 );
s = p+10-i;
table_fill( s, 1, i*3 );
}
cout << endl << "Fill array with <-> " << endl;
table_print( p, Max ); cout << endl << endl;
cout << endl << "Min for previous table " << table_min( p, 15 );
cout << endl << "Max for previous table " << table_max( p+10, 9);
cout << endl << "Average for previous table " << table_avg( p+5, 12);
// .......... You add code here for the two other tasks I have in the specs
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.