Click here to Skip to main content
15,918,303 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
ex: 1, 2 ,3
1
2
3
1,2--3
1,3--4
2,3--5
max sub array is 2,3--5
..
here is my code. i got output. but can any one give simple logic to solve this?
C
#include < stdio.h >
#include < conio.h >
void main()
{
  int a[ 20], count = 1, m, copy[20], maxarry[20], n, x, i, j, noe, sum, l, max;
  clrscr();
  printf("\n ENter the no of elements.");
  scanf("%d", &n);
  printf("\n Enter array");
  for (i = 0; i < n; i++)>
    scanf( "%d" ,&a[i]);
  for (i = 0; i < n; i++)>
  {
    copy[i]=a[i];
  }
  for (i = 0; i < n; i++)>
    printf("%d\n", a[i]);
  //noe ia no of elemnts in subarray
  noe=2;
  max=0;
  //possible sub arrays is minimum 2 elements maximum n-1 elements so till noe value < n form sub arrays
  while(noe < n)>
  {
    for (i = 0; i < n; i++)>
    {
      /**take the element  a[i] and find possible combinations with copy[j]
      a[] = 1 2 3 c[] = 1 2 3 j = i + 1, with a[i] and find possible sub arrays from copy[i+1]*/
      for (j = i + 1; j < n; j++)>
      {
        count = 1;
        //check the possibility of an index j to form sub array
        if ((n - j) >= (noe - 1))
        {
          printf("%d,", a[i]);
          x = j;
          sum = a[i];
          while (count < noe)>
          {
            count++;
            printf("%d,", copy[x]);
            sum += copy[x];
            x++;
          }
          printf("---%d", sum);
          if (sum > max)
          {
            maxarry[0] = a[i];
            for (l = 1, m = j; m++)
              maxarry[l]=c[m];
              max = sub;
          }
          printf("\n");
        }
      }
    }
    k++;
  }
  printf("sub array with max sum");
  for (i = 0; i < l; i++)>
    printf("%d,", maxarray[i]);
  printf("-----%d", max);
  getch();
}

[edit]Code block added - OriginalGriff[/edit]
[edit2]Code block re-added & idented for readability purpose - phil.o[/edit2]
Posted
Updated 26-Jun-15 22:12pm
v5
Comments
OriginalGriff 27-Jun-15 1:55am    
I added a code block to preserve your formatting and indentation in teh hope that it would become more readable, but frankly, it made your code look even worse.

I'm not looking at that code. Why not? Because it's pretty much unreadable.
Fix the indentation so what should be indented is. Change your variable names so they mean something instead of being easy to type.
Then look at it in it's finished glory and see if it makes any sense. If it does, edit your question (use the "Improve question" widget and show it to us.

Help us to help you!
moyna coder 27-Jun-15 3:00am    
okay.
moyna coder 27-Jun-15 3:21am    
thank you . while changing the name of the variables in my program. i came to know that i used a variable name in two different context. now i got the output. thank you.
and can you give any other idea to code this program in easy way

1 solution

"thank you . while changing the name of the variables in my program. i came to know that i used a variable name in two different context. now i got the output. thank you.
and can you give any other idea to code this program in easy way"


Seriously, start by getting the indentation right. Which is easier to read:
C++
for(i=0;i<n;i++)
{
/**take the element  a[i] and find
possible combinations with copy[j]
a[]= 1 2 3 c[]=1 2 3
j=i+1,  with a[i] and find
possible sub arrays from copy[i+1]*/
for(j=i+1;j<n;j++)
{
count=1;
//check the possibility of an index j to form sub array
if((n-j)>=(noe- 1 ))
{
printf("%d," ,a[i]);
x=j;
sum=a[i];
while(count<noe)
{
count++;
printf("%d," ,copy[x]);
sum+=copy[x];
x++;
}
printf("- - -%d" ,sum);
if(sum>max)                
{
maxarry[0]=a[i];                      
for(l=1,m=j;m++)
maxarry[l]=c[m];
max=sub;
}
printf("\n" );
}    }
    }
k++;
}
Or
C++
for(i=0;i<n;i++)
    {
    /**take the element  a[i] and find
    possible combinations with copy[j]
    a[]= 1 2 3 c[]=1 2 3
    j=i+1,  with a[i] and find
    possible sub arrays from copy[i+1]*/
    for(j=i+1;j<n;j++)
        {
        count=1;
        //check the possibility of an index j to form sub array
        if((n-j)>=(noe- 1 ))
            {
            printf("%d," ,a[i]);
            x=j;
            sum=a[i];
            while(count<noe)
                {
                count++;
                printf("%d," ,copy[x]);
                sum+=copy[x];
                x++;
                }
            printf("- - -%d" ,sum);
            if(sum>max)                
                {
                maxarry[0]=a[i];                      
                for(l=1,m=j;m++)
                maxarry[l]=c[m];
                max=sub;
                }
            printf("\n" );
            }
        }
    }
k++;
}
With the second one, you can see at a glance what the flow of control is, and that you have three nested loops - you can't see that at all easily from the first one, because you have to spend loads of time matching brackets by hand.

Second, always use descriptive names - "i", "j", "k", and "n" don't mean anything in particular and it is horribly easy to get confused - as you have seen when you tried to change them! :laugh: It's a lot, lot easier to work with "real" names that talk about what the variable is there for from the start, because you don't have to spend time remembering which is which as you go along.

Finally, that's whats called "monolithic code" - it's all one block, and that means it's harder to understand.
Try making separate functions: input, processing, output.
Then break processing into parts: Generate all possible arrays, sort the arrays.

This way the tasks become simpler, and easier to understand and code. It may seem a little overkill for a trivial project like this, but it's a good idea, because it is the way you have to do it in the real world and it really does make it easier to focus on the various tasks, and get each one right before you move on to the next.
Try it: it's not difficult, and it makes life easier. (Backup what you've got, then give it a try - that way if you get horribly confused you've still got something to hand in to your tutor! :laugh: )


[edit]I hate damn markdown! Strikethrough in code? Come on...[/edit]
 
Share this answer
 
v2
Comments
moyna coder 27-Jun-15 4:49am    
thank you:@##..for ur tips in writing code..i think it will be useful
OriginalGriff 27-Jun-15 4:52am    
You're welcome!
CPallini 27-Jun-15 5:50am    
5.
moyna coder 28-Jun-15 7:16am    
5?
CPallini 28-Jun-15 7:56am    
5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900