3 Replies - 761 Views - Last Post: 14 February 2013 - 12:33 AM Rate Topic: -----

#1 danielZX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 buffalobill  Icon User is offline

  • D.I.C Head

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

Re: 2 Dimensional Array error code

Posted 11 February 2013 - 08:15 PM

When you are loading your array you need 2 loops and one input like:
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.
Was This Post Helpful? 1
  • +
  • -

#4 danielZX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;



Please help me solve this error.Thank you.

This post has been edited by Skydiver: 14 February 2013 - 05:50 AM
Reason for edit:: Put code in code tags. Please learn to do it yourself.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1