# 2 Dimensional Array error code

Page 1 of 1

## 3 Replies - 1118 Views - Last Post: 14 February 2013 - 12:33 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=311666&amp;s=5af5cb0a24d04fec405c71222b7052ea&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 danielZX

Reputation: 0
• Posts: 2
• Joined: 09-February 13

# 2 Dimensional Array error code

Posted 09 February 2013 - 02:37 AM

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.

This post has been edited by ButchDean: 09 February 2013 - 03:08 AM

Is This A Good Question/Topic? 0

## Replies To: 2 Dimensional Array error code

### #2 anonymous26

• D.I.C Lover

Reputation: 1
• Posts: 3,638
• Joined: 26-November 10

## Re: 2 Dimensional Array error code

Posted 09 February 2013 - 03:06 AM

1. Why are you using a 2D array?
2. Please post the actual error?

What you should be using is an array of type 'string' and taking it from there.

Also, please use code tags! It makes it clear as you are typing your post.

### #3 buffalobill

Reputation: 21
• Posts: 188
• Joined: 08-July 08

## Re: 2 Dimensional Array error code

Posted 11 February 2013 - 08:15 PM

```char letter;
for(int i=0;i<5;i++)        //manages the rows
for(int j=0;j<20;j++){   //manages the columns
cin>>letter;          //get one alphabetical letter from user
arr[i][j]=letter;}    //place the letter in arr[0][0]

```

//repeat for 100 alpha letters until arr[4][19] gets the last letter
Test by printing out the 100 letters using two similar loops and cout.
If OK move on to the sorting task.

### #4 danielZX

Reputation: 0
• Posts: 2
• Joined: 09-February 13

## Re: 2 Dimensional Array error code

Posted 14 February 2013 - 12:33 AM

buffalobill regarding the code you share. It works fine. But I have a problem with the swapping part like:

```
//codes
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]; //received error like error C2440: "=" : cannot convert from 'char[20]' to 'int'
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;

```