Thread: Can any one tell me how to make this prgram?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    82

    Can any one tell me how to make this prgram?

    Write a program to merge two unsorted 1D
    order.

    For example:

    Input:

    double arrays in descending

    arr1

    8

    arr2

    9

    arr3

    9

    4

    0

    2

    7

    6

    5

    8

    7

    6

    1

    3

    5

    4

    Output:

    Note: You need to define the following functions:

    void inputarray(int size, double* array);
    This function should prompt the user for input and hence input all array elements from the
    user.

    void printarray(int size, double* array);
    This function should print all array elements.

    void sortarray(int size, double * array, char order);
    This function should sort the array which will be given as the first argument. The second
    argument will be ‘A’ or ‘D’ to specify ascending or descending, respectively.

    void mergeandsortarray(int size1, double* arr1, int size2, double* arr2, double* arr3);

    This function should merge the sorted arrays arr1 and arr2 which will be given as the first and
    second arguments. The third argument, arr3, will be the array obviously having the size of array
    being the sum of the sizes of the first two arrays and will store the two arrays in merged and
    ordered form. Note that your function call to this function must pass the sorted arrays arr1 and
    arr2 and then simply merge them. Note that you must NOT use the naïve or simple algorithm
    which would copy the two arrays into arr3 and then sort arr3!

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Last edited by manasij7479; 11-28-2011 at 02:14 PM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Start off with your include files (headers), and
    Code:
    #include <yourIncludefile.h>
    
    //put your function prototypes here
    
    int main(void) {
       //other code here
    
    
       return 0;
    }
    More functions down here. You have a list of them, by name, and telling you what parameters they need. The whole prototype is right there! You can copy and paste!

    This is the part of the assignment where you have to step up and get it all started. If you get stuck, post up what you have, and tell us what has you stumped.

    Don't wait around for someone to start your program for you. The above is all you'll get (and that's more than most). We don't start programs for people. (99% of the time).

    Get organized, and get cracking. These kinds of assignments always take a good deal longer than you'd think because the requirements are rather long and have lots of details to be dealt with.
    Last edited by Adak; 11-28-2011 at 02:24 PM.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    82
    well thanks for such advice.. but i am not asking you to make a program for me... i just need your help.. I m new to dynamic arrays..... I need help to understand the logic if any one can help..

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by fredsilvester93 View Post
    I m new to dynamic arrays..... I need help to understand the logic if any one can help..
    Dynamically allocate your array... with malloc.. calloc... or whatever you like.
    If you need your array's size to change when necessary, just realloc it with the necessary amount of memory.
    (Or realloc it to...say double the size when you're out of space...that will/may(I'm not sure) improve performance... )
    Last edited by manasij7479; 11-28-2011 at 02:52 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I did not find the tutorial on dynamic arrays, so I'm posting up a sample program that uses one 2D array.

    It doesn't use realloc(), but that is something I very seldom need to use.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main(void)  {
    	
       int **mat; // Pointer to pointer
       int rows, cols, i, j;
       printf("How many rows you want ");
       scanf("%d", &rows);
       //rows = 10;
       //cols = 10;
       mat = malloc(rows*sizeof(int*));        // array of number of rows
    
    
       printf("How many cols ");
       scanf("%d", &cols);
       for (i=0; i<rows; i++) {                // for each row ...
           mat[i] = malloc(cols * sizeof(int)); // add these many cols
       }
       for (i = 0; i<rows; i++) {
          for (j = 0; j<cols; j++) {
             mat[i][j] = (i+1) * (j+1);                
             printf("%4d ", mat[i][j]);         //these two print lines
             //printf("%4d ", *(*(mat+i)+j));  //do the same thing
          }
          putchar('\n');
       }
          
       printf("\n\n"); 
    	return 0;
    }
    Last edited by Adak; 11-28-2011 at 03:09 PM.

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Adak View Post
    I did not find the tutorial on dynamic arrays, so I'm posting up a sample program that uses one 2D array.

    It doesn't use realloc(), but that is something I very seldom need to use.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main(void)  {
        
       int **mat; // Pointer to pointer
       int rows, cols, i, j;
       printf("How many rows you want ");
       scanf("%d", &rows);
       //rows = 10;
       //cols = 10;
       mat = malloc(rows*sizeof(int*));        // array of number of rows
    
    
       printf("How many cols ");
       scanf("%d", &cols);
       for (i=0; i<rows; i++) {                // for each row ...
           mat[i] = malloc(cols * sizeof(int)); // add these many cols
       }
       for (i = 0; i<rows; i++) {
          for (j = 0; j<cols; j++) {
             mat[i][j] = (i+1) * (j+1);                
             printf("%4d ", mat[i][j]);         //these two print lines
             //printf("%4d ", *(*(mat+i)+j));  //do the same thing
          }
          putchar('\n');
       }
          
       printf("\n\n"); 
        return 0;
    }
    Does dynamic mean user-specified or changeable on demand ?
    ..I thought the later.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Either or both, imo. You have to have it before you can change it, was my idea here.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by manasij7479 View Post
    Does dynamic mean user-specified or changeable on demand ?
    ..I thought the later.
    I'd vote for "Not fixed size"...

    Altough I might be persuaded to flip over to "Created at runtime" if the right candidate came along.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Change style of my prgram
    By Antonio Diaz in forum Windows Programming
    Replies: 0
    Last Post: 05-20-2011, 11:27 AM
  2. Replies: 2
    Last Post: 04-27-2011, 04:14 PM
  3. Here's another prgram I need help on!
    By jturner38 in forum C Programming
    Replies: 4
    Last Post: 03-03-2009, 09:42 PM
  4. Making your prgram repeat
    By chibby2 in forum C++ Programming
    Replies: 1
    Last Post: 07-03-2005, 11:14 AM
  5. Replies: 6
    Last Post: 04-20-2002, 06:35 PM