Where do I start?

This is a discussion on Where do I start? within the C Programming forums, part of the General Programming Boards category; I have written code that populates an array, prints an array, and sorts the array. However the user can do ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    69

    Where do I start?

    I have written code that populates an array, prints an array, and sorts the array. However the user can do so in any order.

    How would I start a test that would establish whether the array has been sorted or not so that I can sort either sequentially or by binary methods.

    Please give me some direction I'm unable to put my hands around this issue.

    Thanks in advance.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You can use printf() statements to write to the console to tell you what processing is taking effect.

    You could step through the code with a debugger.

    You could read a file with various input, and write it back out to check your order.

    ...

    Todd

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you mean whether you should search sequentially or by binary methods? Why are you searching?

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    69

    searching response

    User will input a target number and will search for it in the array. However the array could be sorted or unsorted. I would like to test for that in oder to determine which method to use.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So use a flag:
    Code:
    int isSorted = 0;
    When they sort, set the flag. Pass the flag to your searching function, and do what is appropriate.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    69
    I think so. What I need to do is at the end of a sort declare it as sorted, so that in the test
    it will come up as sorted in the test sort. And therefore use the binary search or vice
    versa if not sorted.

    Example:
    Code:
    if (for (int i = 0; i < Max_Size; i++))
         temp=*(list+i);
         *list = false;
         *unsorted=temp;
    
    insertionSort ( ary, Max_Size - 1)
    else (for (int i=0; i < Max_Size; i++))
          temp=*(list+i);
          *list=true;
          *unsorted=temp;
    
    if ( unsorted !=false)
       int binarySearch (int* list, int last, int target, int* locn);
    
    else (unsorted==false)
       int seqSearch (int* list, int last, int target, int* locn);
    Am I on the right track?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I think you have the right idea, as long as you're aware that all those statements are syntactically rubbish. I mean, you can't have a for-loop as the test of an if statement, and I have no idea what you're trying to do in those if statements, or are they for loops, anyway. But yes: once you get to searching, you will do either or the other, depending on your "unsorted" flag.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    69

    more confused.

    ok here is a snippets of my code and this is what I'am trying to do:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define ARY_SIZE 50
    
    //Function Declarations
    void generNum (int* randNum);
    void printNum (int* data, int numItems, int countPerLine);
    void bubbleSort (int* randNum, int end);
    
    int main (void)
    {
    	//local Declarations
    	  int randNum [ARY_SIZE];
    	  	  
       //statements
    
    	  //generate random numbers
    	  generNum (randNum);
    	  printNum (randNum, ARY_SIZE, 10);
          bubbleSort (randNum, 49);
    	  printNum (randNum, ARY_SIZE, 10);
    
    	  system ("PAUSE");
    	  return 0;
    } //main
    /*==============================generNum======================================
    Generate random numbers that will be used to fill an array.
         Pre: randNum array is to recieve the random numbers generated.
    	 Post: filled array
    */
    
    void generNum (int* randNum)
    {
    	//local Declarations
    	 int* aRandNo;
    	 
        //statements
    	 srand(time (NULL));
    	 for (int i =0; i < ARY_SIZE; i++)
    	   {
    		   aRandNo = randNum+i;
    		   *aRandNo = rand() % 999 + 1;		   
    	   }//for
         return;
    }//generNum
    /*===============================printNum=======================================
    Prints the data of the array.
           Pre: data- a filled array
                   last- last elements indicated by an index
                   countPerLIne: number of elements on a line
           Post: data printed
    */
    void printNum (int* data, int numItems, int countPerLine)
    {
    	//Local Declarations
    	  int elemPrinted = 0;
    
                   //Statements
    	   printf ("\n");
    	   for (int i = 0; i < numItems; i++)
                         {
    	         elemPrinted++;
    	         printf ("%3d ", *(data+i));
    	         if (elemPrinted >= countPerLine)
    	           {
                                   printf ("\n");
    	               elemPrinted = 0;
    	           }//if
    	     }//for
    	  printf ("\n");
    	  return;
    } // printNum
    /*=================================bubbleSort=================================
    Sort array using bubble sort. Elements are compared and exchanged until list 
    is in order.
       Pre: minimun of 1 random number.
            end contains index of last element in randNum array.
       Post: randNum array is rearranged in sequence low to high.
    */
    
    void bubbleSort (int* randNum, int end)
    {
    	//Local Declarations
    	  int temp;
    	  int* address;
    
        //Statements
    	  //outer Loop
    	  for (int initial = 0; initial <= end; initial++)
    	     {
                          //Inner loop. Bubble starts at end and moves up 1 element each  pass.
    	          for (int mover = end;
    	                mover > initial;
    		mover--)
    	          if (*(randNum +mover) < *(randNum +(mover - 1)))
    	             {
    		 temp          = *(randNum +mover);
                                     address     = randNum +mover;
    		 *address    = *(randNum +(mover - 1));
    		 address     = randNum +(mover - 1);
    		 *address    = temp;
    	             }//if - exchange of numbers 
    	     }//for initial
    	   return;
    } //bubblesort
    /*===============================search========================================
    Determine if the array is sorted or unsorted.
         Pre:  the array
    	 Post: If sorted will search by binary method,
    	       Else unsorted will search by sequential search
    */
    this is just a portion of the code I have the menu else where. I am utterly unsure where to look to begin this next part of the code. I've attempted it from a variety of directions but obviously I'm confused.

    Thanks

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Where do you look? You look at your flowchart, or your pseudocode, or your algorithm.

    On the other hand, I have no idea why you think this function is any longer than
    Code:
    void SearchNum (int *randNum, int isSorted, int target, int *location) {
        if (isSorted)
            binarySearch(randNum, target, location);
        else
            seqSearch(randNum, target, location);
    }
    Oh, and a PS: Don't forget to change that 49 in your code to ARY_SIZE-1.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    69

    thanks

    I will do as suggested. Muchly appreciate your help. I think I understand where my logic went astray. Many thanks.

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    silhoutte75 you should learn on how to intend your code properly. Its horrible to look through it. I was like looking through your code, just few min before I posted, but i stopped looked at your code since the indentation was getting worser.

    Make sure you place things in the right place. That would help us a lot

    ssharish

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Quote Originally Posted by tabstop View Post
    Oh, and a PS: Don't forget to change that 49 in your code to ARY_SIZE-1.
    Better yet, change it to ARY_SIZE and also change the for-loop in the bubble sort to:
    Code:
    	  for (int initial = 0; initial < end; initial++)
    (less-than instead of less-than-or-equal)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  3. C++ gui for windows where to start
    By prixone in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2006, 10:48 PM
  4. GNOME Desktop won't start (Mandriva)
    By psychopath in forum Tech Board
    Replies: 10
    Last Post: 07-19-2006, 01:21 PM
  5. Start bar color in WinXP
    By confuted in forum Tech Board
    Replies: 4
    Last Post: 05-03-2003, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21