Help with C++ program

This is a discussion on Help with C++ program within the C++ Programming forums, part of the General Programming Boards category; 2005-11-14 : 15:18 -Edit- -Reply- -Top- help with concept!!!!! Updated by philippe on 2005-11-14 15:19 -------------------------------------------------------------------------------- hi, I am trying ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    5

    Help with C++ program

    2005-11-14 : 15:18 -Edit- -Reply- -Top- help with concept!!!!!
    Updated by philippe on 2005-11-14 15:19

    --------------------------------------------------------------------------------

    hi,

    I am trying to code in C++. Now I am given an array containing both positive and negative integers and am required to find the subarray with the largest sum.

    I have written the following code which i feel should work. I would appreciate your comment on this in terms of efficiency, correctness etc.

    Code:
    int* largest(int* a, int size)
    {
      int sum=0,initial=0,final=0,flag=0;
      int storesum=0,storeinitial=0,storefinal=0;
      int *subarray; 
    
      for(int i=0;i <=size;i++)
     {
       if(flag==0 && a[i] >0)
       {
        initial = i;
        flag =1;
      }
      if(a[i] > 0) sum=sum+a[i];
      if(a[i] < 0)
      {
       flag = 0;
       final = i-1;
       if(storesum < sum)
      {
       storesum=sum;
       storeinitial = initial;
       storefinal = final;
      }
     } 
    }
    
     int j=0;
     for(int i=storeinitial;i<=storefinal;i++)
     subarray[j++]=a[i];
    
     return(subarray);
    }
    Please feel free to propose any other methods to carry out the same

    cheers
    pf
    Last edited by philippe; 11-14-2005 at 04:48 PM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Try reposting your code with indenting.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Yes indent
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    hey i didnot intend it ... but improved it a bit aesthetically

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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