Question

The purpose of this problem is to sort a 2 dimensional array of characters. Specify the...

The purpose of this problem is to sort a 2 dimensional array of characters. Specify the size of the array and input the array. If the array is longer than specified, output if it is larger or smaller. Utilize the outputs supplied in main().

Example supplied in 2 test cases.

Input

3↵
3↵
678↵
567↵
456↵

Expected Output

Read·in·a·2·dimensional·array·of·characters·and·sort·by·Row↵
Input·the·number·of·rows·<=·20↵
Input·the·maximum·number·of·columns·<=20↵
Now·input·the·array.↵
The·Sorted·Array↵
456↵
567↵
678↵

Input

3↵
5↵
Ted↵
Mary↵
Bobby↵

Expected Output

Read·in·a·2·dimensional·array·of·characters·and·sort·by·Row↵
Input·the·number·of·rows·<=·20↵
Input·the·maximum·number·of·columns·<=20↵
Now·input·the·array.↵
The·Sorted·Array↵
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

Hi there,

Please find the code below.

/*
* 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 arr[][COLMAX],int v)//Outputs row and columns detected from input
{
int i;
int l;
int max_col_len=-1;
for(i=0;i<v;i++)
{
cin>>arr[i];
l=strlen(arr[i]);
if(l>max_col_len)
//cout<<"he"<<endl;
max_col_len=l;
}
//cout<<max_col_len;
return max_col_len;
  
}
void sort(char str[][COLMAX],int x,int y)//Sort by row
{
int i,j;
char t[y];
for(i=1; i<x; i++)
   {
       for(j=1; j<y; j++)
       {
           if(strcmp(str[j-1], str[j])>0)
           {
               strcpy(t, str[j-1]);
               strcpy(str[j-1], str[j]);
               strcpy(str[j], t);
           }
       }
   }
  
}
void print(const char c[][COLMAX],int x,int y)//Print the sorted 2-D array
{
int i;
for(i=0;i<x;i++)
cout<<c[i]<<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:

Hope it helps.

Add a comment
Know the answer?
Add Answer to:
The purpose of this problem is to sort a 2 dimensional array of characters. Specify the...
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
  • read in numbers into array , print out, sort - my program reads in and prints...

    read in numbers into array , print out, sort - my program reads in and prints out but crashes before the sort - crashes after it prints out the scores - *important I have to use pointers!!!! c++ #include <iostream> using namespace std; void sortarray(int *tarray, int ); void displayarray(int *[], int); void averageshowarray(int *[], int); int main() {     int Size;     int count;     cout << "enter the size: " << endl;     cin >> Size;     int...

  • Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template...

    Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template of the solution, if you need Write a program that asks users to input 10 integers into an array, write a function that takes the array as its argument, use bubble sort to sort the array and output the sorted array in increasing order. #include <iostream> using namespace std; C:IWINDOWSSystems2cmd.exe lease input 10 integers: void bubblesort(int a[10]) int help; int b[10]; for (int i...

  • C++ ONLY!!! NOT JAVA. The standard deviation is calculated from an array X size n with...

    C++ ONLY!!! NOT JAVA. The standard deviation is calculated from an array X size n with the equation std=sqrt(sum((xi-mean)2/(n-1))) Sum the square of difference of each value with the mean. Divide that sum by n-1. Take the square root and you have the standard deviation. //System Libraries #include <iostream> //Input/Output Library #include <cstdlib> //Srand #include <ctime> //Time to set random number seed #include <cmath> //Math Library #include <iomanip> //Format Library using namespace std; //User Libraries //Global Constants, no Global Variables...

  • Am I using the right cin statement to take in values for my array? If I...

    Am I using the right cin statement to take in values for my array? If I print them out it prints "0x61fea0" with the input 1 2 3 4 5 ///////////////////////////////// #include <iostream> using namespace std; const int ARRAY_LENGTH = 5; int main(){ int numbers[ARRAY_LENGTH]; int threshold; //@todo prompt user to enter array values cout << "Please input 5 numbers: " << endl; for(int i = 0; i < ARRAY_LENGTH; i++){ //@todo add cin statement to read in values for...

  • // This program performs a linear search on a character array // Place Your Name Here...

    // This program performs a linear search on a character array // Place Your Name Here #include <iostream> using namespace std; int searchList(char[], int, char); // function prototype const int SIZE = 8; int main() { char word[SIZE] = "Harpoon"; int found; char ch; cout << "Enter a letter to search for:" << endl; cin >> ch; found = searchList(word, SIZE, ch); if (found == -1) cout << "The letter " << ch << " was not found in the...

  • /* Array expander: Write a function that accepts an int array as argument. The function should...

    /* Array expander: Write a function that accepts an int array as argument. The function should create a new array that is n times the size of the argument array, where n is a user input. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array */ #include <iostream> using namespace std; const int NUM_ELEM...

  • Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int...

    Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int n; std::cin >> n; array a(n); for (int i = 0; i < n; i++) { std::cin >> a.data()[i]; } std::cout << "array size:" << a.max_size() << std::endl; std::cout << "array front:" << a.front() << std::endl; std::cout << "array back:" << a.back() << std::endl; int* data = a.data(); std::cout << "array elements using data:" << std::endl; for (int i = 0; i < n;...

  • IN C++ PLEASE -------Add code to sort the bowlers. You have to sort their parallel data...

    IN C++ PLEASE -------Add code to sort the bowlers. You have to sort their parallel data also. Print the sorted bowlers and all their info . You can use a bubble sort or a shell sort. Make sure to adjust your code depending on whether or not you put data starting in row zero or row one. Sort by Average across, lowest to highest. The highest average should then be on the last row.. When you sort the average, you...

  • Can you fix this program and run an output for me. I'm using C++ #include using...

    Can you fix this program and run an output for me. I'm using C++ #include using namespace std; //function to calculate number of unique digit in a number and retun it int countUniqueDigit(int input) {    int uniqueDigitCount = 0;    int storeDigit = 0;    int digit = 0;    while (input > 0) {        digit = 1 << (input % 10);        if (!(storeDigit & digit)) {            storeDigit |= digit;       ...

  • One dimensional array What this code print #include <iostream> using namespace std; int main () {...

    One dimensional array What this code print #include <iostream> using namespace std; int main () { const int SIZE = 7; int numbers [SIZE] = {1, 2, 4, 8): // Initialize first 4 elements cout << “Here are the contents of the array:\n"; for (int index = 0; index < SIZE: index++} cout << numbers[index] << “ “; cout << endl; return 0; }

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