Having trouble with arrays.

Please Help - Worked on for 3 days and still having problems.

Page 1 of 1

6 Replies - 716 Views - Last Post: 29 March 2008 - 11:43 AM Rate Topic: -----

#1 Chevrolet Fanatic  Icon User is offline

  • New D.I.C Head

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

Having trouble with arrays.

Post icon  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 load_array(int [],int &n);
void sort_array(int [], int);
void print_array(int [], int [], int);

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

}

void load_array(int a[],int &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  Icon User is offline

  • Code T(h)inkers
  • member icon

Reputation: 21
  • View blog
  • 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 load_array(int [],int &n);
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;
}

void load_array(int a[],int &n)
{
	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! :)
Was This Post Helpful? 0
  • +
  • -

#3 Chevrolet Fanatic  Icon User is offline

  • New D.I.C Head

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

Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 red_4900  Icon User is offline

  • Code T(h)inkers
  • member icon

Reputation: 21
  • View blog
  • 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 load_array(int [],int &n);
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;
	
	load_array(number,n);

	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;
}

void load_array(int a[],int &n)
{
	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<<number[c]<<"\t";		//for easier readability, use tab character instead of empty space
		cout<<sort[d]<<"\n";
	}
}


I tweaked ur code a bit. this should give u guidance on how to do it. hope this helps! :)
Was This Post Helpful? 0
  • +
  • -

#6 Chevrolet Fanatic  Icon User is offline

  • New D.I.C Head

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

#7 Chevrolet Fanatic  Icon User is offline

  • New D.I.C Head

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

Page 1 of 1