Question

Sort using a 2-Dimension Array of characters Input the size of the 2 dimensional character array,...

Sort using a 2-Dimension Array of characters

Input the size of the 2 dimensional character array, then sort by row.

Input

678↵
567↵
456↵

Expected Output

456↵
567↵
678↵

Input

Ted↵
Mary↵
Bobby↵

Expected Output

Bobby↵
Mary↵
Ted↵

/*
* File: main.cpp
* Author: YOUR NAME HERE
* Created on DATE AND TIME HERE
* Purpose: Sorting a 2-D array of characters if row and columns match
*/

//System Libraries Here
#include <iostream>//cin,cout
#include <cstring> //strlen(),strcmp(),strcpy()
using namespace std;

//User Libraries Here

//Global Constants Only, No Global Variables
//Allowed like PI, e, Gravity, conversions, array dimensions necessary
const int COLMAX=80;//Only 20 required, and 1 for null terminator

//Function Prototypes Here
int read(char [][COLMAX],int &);//Outputs row and columns detected from input
void sort(char [][COLMAX],int,int);//Sort by row
void print(const char [][COLMAX],int,int);//Print the sorted 2-D array

//Program Execution Begins Here
int main(int argc, char** argv) {
//Declare all Variables Here
const int ROW=30; //Only 20 required
char array[ROW][COLMAX]; //Bigger than necessary
int colIn,colDet,rowIn,rowDet;//Row, Col input and detected
  
//Input the size of the array you are sorting
cout<<"Read in a 2 dimensional array of characters and sort by Row"<<endl;
cout<<"Input the number of rows <= 20"<<endl;
cin>>rowIn;
cout<<"Input the maximum number of columns <=20"<<endl;
cin>>colIn;
  
//Now read in the array of characters and determine it's size
rowDet=rowIn;
cout<<"Now input the array."<<endl;
colDet=read(array,rowDet);
  
//Compare the size input vs. size detected and sort if same
//Else output different size
if(rowDet==rowIn&&colDet==colIn){
sort(array,rowIn,colIn);
cout<<"The Sorted Array"<<endl;
print(array,rowIn,colIn);
}else{
if(rowDet!=rowIn)
cout<<(rowDet<rowIn?"Row Input size less than specified.":
"Row Input size greater than specified.")<<endl;
if(colDet!=colIn)
cout<<(colDet<colIn?"Column Input size less than specified.":
"Column Input size greater than specified.")<<endl;
}
  
//Exit
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*
* File: main.cpp
* Author: YOUR NAME HERE
* Created on DATE AND TIME HERE
* Purpose: Sorting a 2-D array of characters if row and columns match
*/

//System Libraries Here
#include <iostream> //cin,cout
#include <string>
#include <cstring> //strlen(),strcmp(),strcpy()
using namespace std;

//User Libraries Here

//Global Constants Only, No Global Variables
//Allowed like PI, e, Gravity, conversions, array dimensions necessary
const int COLMAX = 80; //Only 20 required, and 1 for null terminator

//Function Prototypes Here
int read(char array[30][COLMAX], int &row) //Outputs row and columns detected from input
{
int n = 0, k = 0;
string temp;
int j = 0;
cin.ignore();
getline(cin, temp);
for (auto i : temp)
{
array[0][j] = i;
j++;
}
k = temp.length();
for (int i = 1; i < row; i++)
{
j = 0;
getline(cin, temp);
for (auto l : temp)
{
array[i][j] = l;
j++;
}
if (temp.length() != k)
{
cout << "Please input correctly\n";
i--;
}
k = temp.length();
}
return k;
}
void sort(char array[][COLMAX], int row, int col) //Sort by row
{
bool sort = false;
while (!sort)
{
sort = true;
int j = 0;
for (int i = 0; i < row - 1; i++)
{
if (array[i][j] > array[i + 1][j])
{
sort = false;
string temp;
for (int p = 0; p < col; p++)
{
temp += array[i][p];
array[i][p] = array[i + 1][p];
}
for (int p = 0; p < col; p++)
{
array[i + 1][p] = temp[p];
}
}
}
}
}
void print(const char array[][COLMAX], int row, int col) //Print the sorted 2-D array
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
cout << array[i][j];
cout << endl;
}
}

//Program Execution Begins Here
int main(int argc, char **argv)
{
//Declare all Variables Here
const int ROW = 30; //Only 20 required
char array[ROW][COLMAX]; //Bigger than necessary
int colIn, colDet, rowIn, rowDet; //Row, Col input and detected

//Input the size of the array you are sorting
cout << "Read in a 2 dimensional array of characters and sort by Row" << endl;
cout << "Input the number of rows <= 20" << endl;
cin >> rowIn;
cout << "Input the maximum number of columns <=20" << endl;
cin >> colIn;

//Now read in the array of characters and determine it's size
rowDet = rowIn;
cout << "Now input the array." << endl;
colDet = read(array, rowDet);
//Compare the size input vs. size detected and sort if same
//Else output different size
if (rowDet == rowIn && colDet == colIn)
{
sort(array, rowIn, colIn);
cout << "The Sorted Array" << endl;
print(array, rowIn, colIn);
}
else
{
if (rowDet != rowIn)
cout << (rowDet < rowIn ? "Row Input size less than specified." : "Row Input size greater than specified.") << endl;
if (colDet != colIn)
cout << (colDet < colIn ? "Column Input size less than specified." : "Column Input size greater than specified.") << endl;
}

//Exit
return 0;
}

OUTPUT

COMMENT DOWN FOR ANY QUERY

PLEASE GIVE A THUMBS UP

Add a comment
Know the answer?
Add Answer to:
Sort using a 2-Dimension Array of characters Input the size of the 2 dimensional character array,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT