Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

2 Dimensional Array error code

$
0
0
Hi,

I am new in C++ programming. I got a question that I don't know how to answer. It is about sorting two dimensional arrays. So here is the question. I must create a code that it can accept 5 names and sort in both ascending and descending order. The thing is I don't know how to sort it in 2 Dimensional array. So please I need your help. Thanks.

Here is the code i done so far:
//bubble sort alphabets
#include<iostream>
using namespace std;
void main()
{
char arr[5][20];
int swap;
cout<<"Enter 5 alphabets\n";
for(int i=0;i<5;i++)
cin>>arr[i];
for (int i=0 ; i<5; i++)
{
for (int j=0; j < 5 - i - 1; j++)
{
if (arr[j] > arr[j+1]) 
{
swap = arr[j];
arr[j] = arr[j+1];
arr[j+1] = swap;
}
}
}
cout<<"Sorted list in ascending order:\n";
for (int i = 0 ; i < 5 ; i++ )
cout<<arr[i]<<endl;


system("pause");
}


I wanted to arrange 2 Dimensional array but when i do I got error. but I do not know where is my error is.
This is the code that I got error.
//bubble sort alphabets
#include<iostream>
using namespace std;
void main()
{
	int i;
	int z;
  char arr[5][20];
  int swap;
  cout<<"Enter  5 alphabets\n";
  for( i=0;i<5;i++)
	cin>>arr[i][z];
  for(z=0;z<20;z++)
	  cin>>arr[i][z];
  for (i=0 ; i<5; i++)
  {
    for (int j=0; j < 5 - i - 1; j++)
    {
      if (arr[j][z] > arr[j+1][z]) 
      {
        swap       = arr[j][z];
        arr[j][z]   = arr[j+1][z];
        arr[j+1][z] = swap;
      }
    }
  }
 cout<<"Sorted list in ascending order:\n";
 for ( i = 0 ; i < 5 ; i++ )
     cout<<arr[i]<<endl;
 for(z=0;z<20;z++)
	 cout<<arr[z]<<endl;
 
  system("pause");
}


There is error like exception problems.
Please i really need help in solving this. I don't know where is my error is. If this is my code problem. Please let me know.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>