Thread: I don't know how to start, need Help

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    37

    Question I don't know how to start, need Help

    use Arrays, functions, loop, and if-else

    can someone help me how to start this Code, i don't understand exactly how to implement this code. i don't know how to start. I'm a beginner and this is a beginning class.



    Code:
    Complete the following program. You will mainly implement three out of five functions
    used in this program!
    
    main()
    {
    
    
    /* Declare three integer arrays as follows */
    int a[50], b[70], c[120];
    
    
    /* Implement a function set_array_rand(int x[], int n)
    and call it to generate the values in array a and b
    randomly. */
    set_array_rand(a, 50);
    set_array_rand(b, 70);
    
    
    /* Using the selection_sort(double x[], int n)
    function we implemented in class, sort the elements in
    a and b arrays. */
    selection_sort(a, 50);
    selection_sort(b, 70);
    
    
    
    /* Implement a MERGE function and call it as follows
    to merge the values in arrays a and b into array c
    such that the values in c will be sorted after merging
    */
    merge_sort(a, 50, b, 70, c, 120);
    
    
    
    /* Print the values in array c */
    print_array(c, 120);
    }
    
    
    
    void set_array_rand(int x[], int n)
    {
    
    /* Randomly generate elements of x array */
    for(int i=0; i< n; i++)
    x[i] = rand_int(30, 100);
    
    }
    
    int rand_int(int a,int b)
    {
    
    return rand()%(b-a+1) + a;
    }
    
    void selection_sort(int x[], int n)
    {
    
    /* YOUR CODE */
    }
    
    
    
    void merge_sort(int a[], int na, int b[], int nb, int c[],
    int nc)
    {
    
    
    /* merge the values in a and b into c while keeping
    the values sorted. For example, suppose we have the
    following arrays a = { 3, 7, 9, 12} and b = {4, 5, 10}
    When we merge these two arrays, we will get
    c = {3, 4, 5, 7, 9, 10, 12} */
    
    /* YOUR CODE */
    }
    
    
    void print_array(int x[], int n)
    {
    
    
    /* YOUR CODE */
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Do a wikipedia search for the various search algorithms you have to implement. Take your best guess at the implementation, try it out, and compare the result with what you expected. If you run into any problems, let us know, but you need to try this yourself first.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    37
    Quote Originally Posted by sean View Post
    Do a wikipedia search for the various search algorithms you have to implement. Take your best guess at the implementation, try it out, and compare the result with what you expected. If you run into any problems, let us know, but you need to try this yourself first.
    i did a research and read about arrays, saw some examples and i tried the first two where it says "your code"
    but if you could explain me a little bit of how to get this going, i will appreaciatted.

    Code:
    main()
    {
    /* Declare three integer arrays as follows */
    int a[50], b[70], c[120];
    
    /* Implement a function set_array_rand(int x[], int n)
    and call it to generate the values in array a and b
    randomly. */
    
    set_array_rand(a, 50);
    set_array_rand(b, 70);
    
    /* Using the selection_sort(double x[], int n)
    function we implemented in class, sort the elements in
    a and b arrays. */
    
    selection_sort(a, 50);
    selection_sort(b, 70);
    
    
    /* Implement a MERGE function and call it as follows
    to merge the values in arrays a and b into array c
    such that the values in c will be sorted after merging
    */
    merge_sort(a, 50, b, 70, c, 120);
    
    
    /* Print the values in array c */
    print_array(c, 120);
    }
    
    void set_array_rand(int x[], int n)
    {
         
    /* Randomly generate elements of x array */
    for(int i=0; i< n; i++)
    x[i] = rand_int(30, 100);
    }
    int rand_int(int a,int b)
    {
    return rand()%(b-a+1) + a;
    }
    void selection_sort(int x[], int n)
    {
    
    /* YOUR CODE */
    
    
    for (i=0; i<100; i++)
        data[i] = (rand() % 100) + 30;
    
    
    
    }
    
    
    
    void merge_sort(int a[], int na, int b[], int nb, int c[],
    int nc)
    {
    /* merge the values in a and b into c while keeping
    the values sorted. For example, suppose we have the
    following arrays a = { 3, 7, 9, 12} and b = {4, 5, 10}
    When we merge these two arrays, we will get
    c = {3, 4, 5, 7, 9, 10, 12} */
    
    /* YOUR CODE */
    
    int a[]={3, 7, 9, 12,};
    int b[]={4,5,10} 
      
      int a, b, count;
      
      for(a = 0; a < 12;  i++){
        count = 1;
        
      for(b = 0; b < 10;  j++){
          if (x[a] == x[b]) count++;
        }
        printf(“%d %d \n”, x[a], count);
      }   
    
    
    
    
    
    }
    
    
    void print_array(int x[], int n)
    {
         
         
    /* YOUR CODE */
    }

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    51
    Start your own program and go step by step, implementing each "YOUR CODE" segment one at a time.

    Start with a simple "main()" that calls "set_array_rand(a, 50)", get that working, and so on. There is a lot that is done for you, you just need to fill in the blanks. Write down on paper in plain english (or what ever language) what the program currently does and compare to what it needs to do and fill in the blanks.

    It seems to me that any more "help" at this point would really be just writing it for you.

    Give it a try and if you have specific questions about YOUR code post it.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Code:
    /* my reply */
    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"

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I suppose to should have learnt by now about array and sorting algorithm. Since you proferssor had already meantioned it in the assignment

    Code:
    we implemented in class
    Anyways, start from here. Write a simple code to delare an array of 10 elements and populate it with the random values ??

    Here is a templete for you.

    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main()
    {
        int array[ < fill the no. of elements > ]
        /* Declare more variables if needed */
        
        srand(time(NULL));
        
        for( < use for loop to step through the array > )
             array[ index ] = random value;
        
        for( < use for loop to step through the array > )
             printf array [index] 
        
        getchar();
        return 0;
    }
    And some useful link
    rand - C++ Reference

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

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, 09: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, 11: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