need help in modify my code

This is a discussion on need help in modify my code within the C Programming forums, part of the General Programming Boards category; hello, my code scans through an array , build sub-arrays, and print them out. the sub arrays contains numbers from ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    1

    need help in modify my code

    hello, my code scans through an array , build sub-arrays, and print them out. the sub arrays contains numbers from original array,
    that sums up to half the array sum. (ex: arr:1,2,3,4 subs:1,4 and 2,3)
    i need to modify it so it would only print out the first sub-array that it finds, and the rest of the array (ex: 1,2,2,3,1,1 out will be: 1,2,2 and 3,1,1 only)
    hope you can help me.
    p.s. no static var are allowed

    Code:
    #include <stdio.h>
    // printing all the options
    #define true 1
    #define false 0
    
    
    void printArr(int arr[], int n)
    {
        int i;
    
        puts("");
        for (i=0; i<n; i++)
            printf (" %d",arr[i]);
        puts("");
    }
    int SubsetSum(int *arr, int n, int S, int subseq[],int count)
    {
        int flag;
        if (S==0) 
        {
            printArr(subseq,count);
            return true;
        }
        if (S<0 || !n) 
            return false;
    
        subseq[count] = arr[0];
        flag = SubsetSum(arr+1,n-1,S-arr[0],subseq, count+1);
        
        return (SubsetSum(arr+1,n-1,S,subseq,count) || flag);
    }
    
    void GetArray(int arr[], int i, int size)
    {
        int num=0;
        if (i < size)
        {
            printf("enter the %d element\n",i);
            scanf("%d",&num);
            arr[i]=num;
            GetArray(arr, i+1, size);
        }
    
    }
    
    void main()
    {
        int arr[20]={0}, subseq [12];
        int S=0 ,arrsize=0,i;
        scanf("%d",&arrsize);
        GetArray(arr, 0, arrsize);
        for (i=0; i<arrsize; i++)
            S+=arr[i];
        S=S/2;
        
        if (!SubsetSum(arr,arrsize,S,subseq,0))
            printf("\nThere is no subsequence\n");
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    Oh great, another "I found this code, it doesn't do exactly what I want it to, but I've no idea how to change it because I didn't write it" poser.
    recursive maximal combination
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adjacency matrix, please modify my code
    By enakta13 in forum C Programming
    Replies: 2
    Last Post: 09-27-2012, 03:13 AM
  2. help modify code 6 x 24 to 10 x 24 led matrix
    By visualfx in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2012, 02:04 PM
  3. Replies: 15
    Last Post: 05-11-2011, 05:06 PM
  4. can someone help me modify the code of Metapad?
    By Rigel in forum C Programming
    Replies: 2
    Last Post: 10-17-2009, 09:17 AM
  5. modify my code
    By BEN10 in forum C Programming
    Replies: 3
    Last Post: 03-28-2009, 01:48 AM

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