# Having trouble with arrays.

Page 1 of 1

## 6 Replies - 1036 Views - Last Post: 29 March 2008 - 11:43 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=47560&amp;s=683517ce91c85940ee775a200d3defbe&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Chevrolet Fanatic

Reputation: 0
• Posts: 27
• Joined: 28-February 08

# Having trouble with arrays.

Posted 28 March 2008 - 07:54 PM

The problem I am having is with my Unsorted Array and Sorted Array that takes the 10 numbers input at the first of the program and then displays them first Unsorted (in the order they were input) in a column and then Sorted in a column.

The Sorted line of numbers first number is -858993460, then it starts off with the numbers that were input but only displays 9 of them, not the whole 10. Plus the Unsorted shows the numbers sorted now for some reason. I'm about to go crazy because I have no idea why in the world the Unsorted is now showing them sorted, and why the Sorted column is also showing them sorted but is displaying -858993460 as the first number.

Enter 10 Numbers
20
15
33
52
17
64
84
29
44
68

Unsorted Sorted
15 -858993460
17 15
20 17
29 20
33 29
44 33
52 44
64 52
84 64
68 84

Here is my code:

```#include <iostream>
using namespace std;

void sort_array(int [], int);
void print_array(int [], int [], int);

void main()
{
int number[99];
int sort[1];
int n=10;

sort_array(sort,n);
print_array(number,sort,n);

}

{
cout<<"Enter 10 Numbers"<<"\n";

for(int b=0; b<n; b++)
{
cin>> a[b];
}

cout<<"\n";
}

void print_array(int number[], int sort[], int n)
{
cout<<"Unsorted"<<"  "<<"Sorted"<<"\n";
for(int c=0, d=0; c<n; c++, d++)
{
cout<<number[c]<<"		 ";
cout<<sort[d]<<"\n";
}
}

void sort_array(int sort[],int n)
{
int temp;
for(int x=0; x<n-1; x++)
{
for(int y=0; y<n-1; y++)
{
if(sort[y]>sort[y+1])
{
temp = sort[y+1];
sort[y+1]=sort[y];
sort[y]=temp;
}
}
}
}
```

Any help would be greatly appreciated, thanks a lot!

This post has been edited by Chevrolet Fanatic: 28 March 2008 - 09:07 PM

Is This A Good Question/Topic? 0

## Replies To: Having trouble with arrays.

### #2 red_4900

• Code T(h)inkers

Reputation: 21
• Posts: 1,120
• Joined: 22-February 08

## Re: Having trouble with arrays.

Posted 28 March 2008 - 09:29 PM

Let me give u a hint:

```#include <iostream>
using namespace std;

void sort_array(int [], int);
void print_array(int [], int [], int);

int main()		//for good programming practice, use int instead of void
{
int number[10];
int sort[10];
int n=10;

load_array(number,n);		//u inserted the array 'number' here
sort_array(sort,n);			//and array 'sort' here does not hold the same value as array 'number'
print_array(number,sort,n);

return 0;
}

{
cout<<"Enter 10 Numbers"<<"\n";

for(int b=0; b<n; b++)
{
cin>> a[b];		//which means here, u inserted number for the array 'number'
}

cout<<"\n";
}

void sort_array(int sort[],int n)
{
int i;
for(i=0;i<n;i++)
cout<<sort[i]<<endl;			//here I show u whats really in the array 'sort'

int temp;
for(int x=0; x<n-1; x++)
{
for(int y=0; y<n-1; y++)
{
if(sort[y]>sort[y+1])
{
temp = sort[y+1];		//what u do here is sorting the number in the array 'sort', not the array 'number'
sort[y+1]=sort[y];
sort[y]=temp;
}
}
}
}

void print_array(int number[], int sort[], int n)
{
cout<<"Unsorted"<<"  "<<"Sorted"<<"\n";
for(int c=0, d=0; c<n; c++, d++)
{
cout<<number[c]<<"\t";
cout<<sort[d]<<"\n";
}
}
```

u should do a copy of the array 'number' to the 'sort' array, and then sort the number in the 'sort' array. hope this help!

### #3 Chevrolet Fanatic

Reputation: 0
• Posts: 27
• Joined: 28-February 08

## Re: Having trouble with arrays.

Posted 28 March 2008 - 09:49 PM

Ok I understand it a little better now, but still yet in the Sorted row it displays like this for some reason instead of actually displaying the numbers like it should:

Sorted
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460

So do I need to take and make the "sort" array sort the integers in the "number" array? I'm still confused a little because I don't know what to do in the sort array really still yet. I thought that I had to keep the sort and number arrays separate from each other so they would be able to be displayed like I was wanting.

I've been looking at it for a while, and still don't really see why the sort is not working. I'm just completely confused still yet it seems like.

This post has been edited by Chevrolet Fanatic: 28 March 2008 - 10:44 PM

### #4 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

## Re: Having trouble with arrays.

Posted 28 March 2008 - 11:15 PM

In your original code, you declared sort[] as an array of length 1. Also you never put anything into that array. So the value in sort[0] was just whatever junk was already in that memory location.

The compiler probably gave sort[] the memory space right before number[], so when you were sorting what you thought were sort[1]-sort[9], you were actually sortin number [0]-number[8].

Since you haven't posted your new code, I can't tell exactly what's wrong, but it looks like you may now have allocated more memory to sort[] (sort[] should be the same size as number[]), but still didn't put any data there.

What you should do is, after you read in the input to number[], copy item for item into the corresponding element of sort[]. Then run your sorting function on sort[] without touching number[] at all. Then print.

This post has been edited by r.stiltskin: 28 March 2008 - 11:17 PM

### #5 red_4900

• Code T(h)inkers

Reputation: 21
• Posts: 1,120
• Joined: 22-February 08

## Re: Having trouble with arrays.

Posted 28 March 2008 - 11:27 PM

``` #include <iostream.h>
using namespace std;

void sort_array(int [], int);
void print_array(int [], int [], int);

int main()
{
int number[10];			//dont waste memory, if u know u would only insert 10, make the array's element 10
int sort[10];			//'sort' array should be of the same size as the 'number' array
int n=10;
int i;

for(i=0;i<n;i++)
sort[i]=number[i];	//copying every element in 'number' array to 'sort' array

sort_array(sort,n);
print_array(number,sort,n);

return 0;
}

{
cout<<"Enter 10 Numbers"<<"\n";

for(int b=0; b<n; b++)
{
cin>> a[b];
}

cout<<"\n";
}

void sort_array(int sort[],int n)
{
int temp;
for(int x=0; x<n-1; x++)
{
for(int y=0; y<n-1; y++)
{
if(sort[y]>sort[y+1])
{
temp = sort[y+1];
sort[y+1]=sort[y];
sort[y]=temp;
}
}
}
}

void print_array(int number[], int sort[], int n)
{
cout<<"Unsorted"<<"  "<<"Sorted"<<"\n";
for(int c=0, d=0; c<n; c++, d++)
{
cout<<sort[d]<<"\n";
}
}
```

I tweaked ur code a bit. this should give u guidance on how to do it. hope this helps!

### #6 Chevrolet Fanatic

Reputation: 0
• Posts: 27
• Joined: 28-February 08

## Re: Having trouble with arrays.

Posted 28 March 2008 - 11:53 PM

Thanks for the help guys, I really really appreciate it. I'll go over it again tomorrow when I'm not half asleep and let you know how it goes. Hopefully I will get it now.

### #7 Chevrolet Fanatic

Reputation: 0
• Posts: 27
• Joined: 28-February 08

## Re: Having trouble with arrays.

Posted 29 March 2008 - 11:43 AM

Yep I just finished it and it worked like a charm now. I learned a lot from the help. The tab character makes the two columns a lot easier to read now too instead of just using spaces like I did have it.

Thanks a lot for the help guys, I appreciate it a ton!