Thread: can someone help spot the error

  1. #1
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266

    can someone help spot the error

    hey all, hope you're having a fine weekend all i'm trying to do here is load an array and sort it, but i think my logic doesn't follow. i think the problem is in the sorting. thanks in advance
    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    using namespace std;
    
    main()
    {
    	int const array_size=400;
    	int array[array_size], holder;
    
    //seed
    	srand(time(0));
    
    //load, sort & display array
    	for (int i=0; i<array_size; i++)
    	{	array[i]=1+rand() %array_size*2;
    		if (array[i]<array[i-1]&&i>0)
    		{	holder=array[i];
    			array[i]=array[i-1];
    			array[i-1]=holder;
    		}
    		cout<<array[i]<<" ";
    	}
    	return(0);
    }

  2. #2
    "The Oldest Member Here" Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,039
    I'm not that clear on what your trying to accomplish(but everyone else does, right?)

    Yeah well it compiles fine on my compiler

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    6
    What range of "random" numbers are you looking for?

  4. #4
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    i'm working on a recursive search function, but i can't even get an array loaded with random numbers from 1-twice the size of the array. but the numbers don't seem random. below is what i get, the 1st, 2nd, 3rd, 6th, numbers appear random, but after that they just repeat

    51 193 691 691 691 691 691 713 713 713 713 733 733 733 733 733 733 779 779 779 7
    79 779 779 779 779 779 779 779 779 779 779 779 779 779 779 779 779 779 779 779 7
    79 779 779 779 779 779 779 779 779 779 779 779 779 779 779 779 779 779 779 779 7
    79 779 779 779 779 797 797 797 797 797 797 797 797 797 797 797 797 797 797 797 7
    97 797 797 797 797 797 797 797 797 797 797 797 797 797 797 797 797 797 797 799 7
    99 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 7
    99 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 7
    99 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 7
    99 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 7
    99 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 7
    99 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 7
    99 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 7
    99 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 7
    99 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 7
    99 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 7
    99 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 7
    99 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 7
    99 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 7
    99 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 7
    99 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799 799

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    23

    Cool

    you have two problems in your code. First off you only sort only between two elements that are NEXT to each other...what about the stuff before them? For example:

    21 20 18 2 //pretend thats your array
    20 21 18 2 //after first sort
    20 18 21 2 //after second sort
    20 18 2 21 //this is after your sort algorithm completes

    Your trying to sort in linear time and that is impossible =) You can make a simple sort algorithm with 2 for loops but i'll leave that to you =)

    Secondly you are printing out IMMEDIATELY after your sort 2 elements. What you need to do is print out after the sort is completely done. Whats happening is that say 800 is the biggest number and its the second element in your array.

    1 800 2 3 4 5 6 7 //your array

    Whats going to happen is that you keep printing out array[i]. Well since your making array[i] > array [i-1], through each iteration 800 should be the greatest number thus you will keep printing out 800 after the first 1.

    1 800 800 800 800 800 800 800 //your output

    Hope that helps you...

    -Dennis

  6. #6
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    thanks prophet, you're my new hero it's not the best but it'll work for now:
    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    using namespace std;
    
    main()
    {
    	int const array_size=400;
    	int array[array_size], holder;
    
    //seed
    	srand(time(0));
    
    //load
    	for (int i=0; i<array_size; i++)
    	{	array[i]=1+rand() %array_size*2;
    	}
    	
    //sort
    	for (int i=0; i<array_size; i++)
    	{	for (int x=0; x<array_size; x++)
    		{	if (array[x]<array[x-1] && x!=0)
    			{	holder=array[x];
    				array[x]=array[x-1];
    				array[x-1]=holder;
    			}
    		}
    	}
    //display array
    	for (int i=0; i<array_size; i++)
    	{	cout<<array[i]<<" ";
    	}
    	return(0);
    }

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    23
    That is the most basic way of sorting but your idea is correct now. Glad I could help =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM