In C ++ implement the following two functions: void printTable( double *values,
ID: 3851348 • Letter: I
Question
In C ++ implement the following two functions: void printTable( double *values, int n, intperRow);void sortValues(double *first, double *last);
#endif
With requirements:
To test:
Array x[ ] Results should look like this o void print Table (double *values, int n, int perRow) print the values as a neatly-formatted table with the specified number of values per row, and each value in a field of width 10 showing 2 significant digits. o void sortvalues (double *first double *last) sort the values from first through last, where these parameters are pointers into the same dynamic array. Write your own sorting routine, such as the selection sort procedure presented in Chapter 7 but remember you should use pointer notation, not array notation to do it. You may write helper functions if you want, but they should use pointer notation too Use ptrtest2.cpp to test your implementations. Compile it as you did above, subsituting ptrtest2 for ptrtest1 in both places. This program expects three command line arguments in this order: an input filename, the number of values to read from the file, and the number of values to print per row. Here are two sample runs using data1.txt as the input file test2-out txt. Your results must exactly match if we test your program the same way, but you should ensure the program works for different inputs too.
Explanation / Answer
for Print Table -
*values - pointer to the first element of the array ;
n - total number of values
perRow - number of values to be printed in one row;
hint - this is simple. double data type is of 8 bytes in C so, values +0 will give 1st element, values + 8 second, values + 16 next and so on... then use loops to restrict the elements to perRow.
code ---
void PrintTable (double *values, int n, int PerRow)
{
int i,j,k=0;
// n divided by perRow will give the number of times outer loop will run
int l = n/PerRow;
for(i=0;i<l;i++)
{
for(j=0;j<PerRow;j++)
{ cout<<*(values + k*8)<<" "; k++; }
cout<<endl;
}
}
---------
this code will output the desired result.
-----------------------------------------------------------------------------------------------------------
void SortTable
for this we are given the first and last pointer to the array.
fairly simple, just in your normal selection start with i=*(first) instead of i=0 and end at i==*(last) instead i < size. also, for i++, simply do first + 8, first +16 like above.
void SortTable(double *first, double *last)
{
int *c;
}
------------------------------------------------------------------------
then you can print this with PrintTable
----------------------------------------------------
thank you