Question

In: Computer Science

C++ How would I sort my output to evaluate it by sorting the column by it's...

C++

How would I sort my output to evaluate it by sorting the column by it's size? I didn't include my full program but here is the main.cpp where i'm supposed to sort the output by column size.

//User Libraries
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

//User Libraries
#include "Table.h"
#include "Triangle.h"

//Global Constants

//Function Prototype
void prntRow(RowAray *,int);
void prntTab(Table *);
void prntTri(Triangle *);


//Execution Begins Here!
int main(int argc, char** argv) {
//Initialize the random seed
srand(static_cast<unsigned int>(time(0)));

//Declare Variables
// creates a random number of rows from 1 to 10
int rows=(rand()%10)+ 1;
//creates a random number of columns from 1 to 10
int cols=(rand()%10 )+1;
int perLine=cols/2;

//Test out the RowAray
RowAray row(cols);
  
//Print the RowAray
cout<<"The Row Array size = "<<row.getSize()
<<" printed "<<perLine<<" per Line";

prntRow(&row,perLine);


//Test out the Table
Table tab(rows,cols);

//Print the Table
cout<<"The table size is [row,col] = ["<<rows<<","<<cols<<"]";
prntTab(&tab);

//Test out the Triangular Table
Triangle tri(rows);

//Print the Triangular Table
cout<<"The triangular table size is [row,row] = ["<<rows<<","<<rows<<"]";
prntTri(&tri);

//Exit Stage Right
return 0;
}

//Fill a triangular matrix
int **fillAry(int *col,int n){
int **array=new int*[n];
for(int i=0;i<n;i++){
array[i]=new int[col[i]];
}
for(int i=0;i<n;i++){
for(int j=0;j<col[i];j++){
array[i][j]=rand()%9+1;//1 Digit numbers [1-9]
}
}
return array;
}
void prntRow(RowAray *a,int perLine){
cout<<endl;
for(int i=0;i<a->getSize();i++){
cout<<a->getData(i)<<" ";
if(i%perLine==(perLine-1))cout<<endl;

}
cout<<endl;
}

void prntTab(Table *a){
cout<<endl;
for(int row=0;row<a->getSzRow();row++){
for(int col=0;col<a->getSzCol();col++){
cout<<a->getData(row,col)<<" ";
}
cout<<endl;
}
cout<<endl;
}

void prntTri(Triangle *a){
cout<<endl;
for(int row=0;row<a->getSzRow();row++){
for(int col=0;col<=row;col++){
cout<<a->getData(row,col)<<" ";
}
cout<<endl;
}
cout<<endl;
}

Solutions

Expert Solution

If you have any doubts, please give me comment...

RowAray.h

#ifndef ROWARAY_H

#define ROWARAY_H

#include <cstdlib>

class RowAray

{

private:

    int size;

    int *rowData;

public:

    RowAray(int size);

    ~RowAray();

    int getSize();

    int getData(int i);

};

#endif

RowAray.cpp

#include "RowAray.h"

RowAray::RowAray(int size)

{

    this->size = size;

    rowData = new int[size];

    for (int i = 0; i < size; i++)

    {

        rowData[i] = (rand() % 90) + 10;

    }

}

RowAray::~RowAray()

{

    delete rowData;

}

int RowAray::getSize()

{

    return size;

}

int RowAray::getData(int i)

{

    return rowData[i];

}

Table.h

#ifndef TABLE_H

#define TABLE_H

#include "RowAray.h"

class Table

{

private:

    int szRow;

    int szCol;

    RowAray **records;

public:

    Table(int row, int col);

    ~Table();

    int getSzRow();

    int getSzCol();

    int getData(int r, int c);

};

#endif

Table.cpp

#include "Table.h"

Table::Table(int row, int col)

{

    this->szRow = row;

    this->szCol = col;

    records = new RowAray *[row];

    for (int i = 0; i < row; i++)

        records[i] = new RowAray(col);

}

Table::~Table()

{

    delete[] records;

}

int Table::getSzRow()

{

    return szRow;

}

int Table::getSzCol()

{

    return szCol;

}

int Table::getData(int r, int c)

{

    return records[r]->getData(c);

}

Table.h

#ifndef TRIANGLE_H

#define TRIANGLE_H

#include "RowAray.h"

class Triangle

{

private:

    int szRow;

    RowAray **records;

public:

    Triangle(int row);

    ~Triangle();

    int getSzRow();

    int getData(int r, int c);

};

#endif

Triangle.cpp

#include "Triangle.h"

Triangle::Triangle(int row)

{

    this->szRow = row;

    records = new RowAray *[row];

    for (int i = 0; i < row; i++)

        records[i] = new RowAray(row);

}

Triangle::~Triangle()

{

    delete[] records;

}

int Triangle::getSzRow()

{

    return szRow;

}

int Triangle::getData(int r, int c)

{

    return records[r]->getData(c);

}

main.cpp

//User Libraries

#include <cstdlib>

#include <ctime>

#include <iostream>

using namespace std;

//User Libraries

#include "Table.h"

#include "Triangle.h"

//Global Constants

//Function Prototype

void prntRow(RowAray *, int);

void prntTab(Table *);

void prntTri(Triangle *);

//Execution Begins Here!

int main(int argc, char **argv)

{

    //Initialize the random seed

    srand(static_cast<unsigned int>(time(0)));

    //Declare Variables

    // creates a random number of rows from 1 to 10

    int rows = (rand() % 10) + 1;

    //creates a random number of columns from 1 to 10

    int cols = (rand() % 10) + 1;

    int perLine = cols / 2;

    //Test out the RowAray

    RowAray row(cols);

    //Print the RowAray

    cout << "The Row Array size = " << row.getSize()

         << " printed " << perLine << " per Line";

    prntRow(&row, perLine);

    //Test out the Table

    Table tab(rows, cols);

    //Print the Table

    cout << "The table size is [row,col] = [" << rows << "," << cols << "]";

    prntTab(&tab);

    //Test out the Triangular Table

    Triangle tri(rows);

    //Print the Triangular Table

    cout << "The triangular table size is [row,row] = [" << rows << "," << rows << "]";

    prntTri(&tri);

    //Exit Stage Right

    return 0;

}

//Fill a triangular matrix

int **fillAry(int *col, int n)

{

    int **array = new int *[n];

    for (int i = 0; i < n; i++)

    {

        array[i] = new int[col[i]];

    }

    for (int i = 0; i < n; i++)

    {

        for (int j = 0; j < col[i]; j++)

        {

            array[i][j] = rand() % 9 + 1; //1 Digit numbers [1-9]

        }

    }

    return array;

}

void prntRow(RowAray *a, int perLine)

{

    cout << endl;

    for (int i = 0; i < a->getSize(); i++)

    {

        cout << a->getData(i) << " ";

        if (i % perLine == (perLine - 1))

            cout << endl;

    }

    cout << endl;

}

void prntTab(Table *a)

{

    cout << endl;

    for (int row = 0; row < a->getSzRow(); row++)

    {

        for (int col = 0; col < a->getSzCol(); col++)

        {

            cout << a->getData(row, col) << " ";

        }

        cout << endl;

    }

    cout << endl;

}

void prntTri(Triangle *a)

{

    cout << endl;

    for (int row = 0; row < a->getSzRow(); row++)

    {

        for (int col = 0; col <= row; col++)

        {

            cout << a->getData(row, col) << " ";

        }

        cout << endl;

    }

    cout << endl;

}


Related Solutions

Excel sorting In excel how do you sort a column of months in ascending order and...
Excel sorting In excel how do you sort a column of months in ascending order and not alphabetical when the month is in word format?
Implementation of Quick sort and heap sorting algorithms in C++
Implementation of Quick sort and heap sorting algorithms in C++
It's time to write a sorting algorithm! In this lab, you'll be writing Bubble Sort. Much...
It's time to write a sorting algorithm! In this lab, you'll be writing Bubble Sort. Much like the previous lab, you will be tasked with prompting the user for a list of values until the user enters 0 (you may use the same initializeVector that you wrote in the last lab). You will then write bubblesort which sorts the vector from smallest to largest. You will then call a function displayVector which displays the vector to the screen. Keep everything...
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT...
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT AND HEAP SORT IN THE SAME PROGRAMM
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT...
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT AND HEAP SORT IN THE SAME PROGRAM PS: YOU ARE ANSEWRING THE SAME PROGRAMS I WANT DIFFERENT ONE PLEASE , THANK YOU . BECAUSE THE ONE WERE POSTING DOESNT WORKING !!
Comparing (Sort Algorithms) Both of the two sorting algorithms will do "sort" on arrays which would...
Comparing (Sort Algorithms) Both of the two sorting algorithms will do "sort" on arrays which would contain x randomly generated integers where the value of x would be 10000, 20000, 40000 and 80000 (inputs). The parts of the program should be followed as..... 1. Set x = 10000, randomly generate x integers. Call qsort function to sort these integers and get the execution time. 2. Randomly generate another x integers. Call your own sorting algorithm and get the execution time....
the sort below shows an algorithm for sorting aSort(A, i, j) // where A is the...
the sort below shows an algorithm for sorting aSort(A, i, j) // where A is the array to be sorted; i is the start index and j is the end index. n = j – i + 1 If (n < 18) { sort A[i…j] by insertion-sort return } m1 = i + 2 * n / 3 m2 = i + n / 3 aSort(A, i, m1) aSort(A, m2, j) aSort(A, i, m1) questions: 1) Please state the asymptotic...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C program which accepts 1 command-line argument: the name of a text file which contains integers, one-per line. Your C program must be named project3. Your C program needs to implement the QuickSort algorithm to sort the integers read from the file specified on the command-line. Your QuickSort implementation must utilize the median-of-three algorithm for choosing a pivot, and BubbleSort any sub arrays with less...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
How would I make a bubble sort and an optimized bubble sort with the code given?...
How would I make a bubble sort and an optimized bubble sort with the code given? I also need to implement a timer into each sort and display runtime with the sorts. NODE.H _______________________________________________________________________________________________________ /* node.h */ /* two classes 1: node.h 2. singlylinkedlist.h nod1 (value + pointer) ---> node2 ---> node3 ---> |||| <--- node.h ^ | singlylinkedlist ----------------*node head; */ #ifndef NODE_H #define NODE_H #include <iostream> using namespace std; class Node {    friend class singlyLinkedList; public:   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT