-1

Write a c program that reads a sequence of maximum 9 integers, prints the sum of every contiguous sub sequence of it.

I am able to print all the contiguous sub sequences but not able to print the sum of each sub sequence. Please help me learn. I am a naive learner. Forgive me for my inefficiency in coding. Given below is what I was able to do.

#include<stdio.h>
int main()
{ 
    int a[9];
    int i,j,k,sum[9]={0};
    for(i=0;i<9;i++)
        scanf("%d",&a[i]);

    for(i=0;i<9;i++)
    {
        for(j=i;j<9;j++)
        {
            for(k=i;k<j+1;k++)
                printf("+%d ",a[k]);
            printf("\n");

        }

    }
    getch();
}
Mat
  • 202,337
  • 40
  • 393
  • 406
shravani
  • 1
  • 2

3 Answers3

1

First, regarding this:

sum[9]={0};

Since the problem only asks you to print the sums of the various subsequences, it's not strictly necessary to store them, so I would just declare a single int that you can use for doing the summation. Incidentally, if you did need to store all of the sums, you would need a substantially larger array: one with a position for every distinct subsequence, rather than for every member of the original series.

On to the calculation. Here's where you're enumerating the members of a subsequence:

for(k=i;k<j+1;k++)
    printf("+%d ",a[k]);

What you'll want to do is:

  1. Before the loop, initialize the summation variable to zero, since you're about to begin a new calculation.
  2. Inside the loop, add each a[k] value to the sum.
  3. After the loop, your summation variable will contain the correct value since you've just processed the whole subsequence, so you can simply print it.

This should give you enough to complete the exercise, but if you have any further specific questions, just let me know.

Joe Farrell
  • 3,502
  • 1
  • 15
  • 25
0
#include<stdio.h>
int main()
{
    int a[9]={0};
    int sum=0;
    int i,j;
    for(i=0;i<9;i++)
        scanf("%d",&a[i]);
    for(i=0;i<9-1;i++)
    {
        sum=0;
        for(j=i;j<9;j++)
        {
            sum=sum+a[j];
            printf("sum: %d ",sum);
        }
      printf("\n\n");
   }
return 0;
}

OK first off all i didn't know what was contiguous sub sequence,so sorry if my answer is not so correct with the your problem, i googled and found this What does this definition of contiguous subsequences mean? I made the program according to my understanding of this link. so according to that your problem should find sum of every possible set (in contiguous incrementing order). I have implemented it.

Community
  • 1
  • 1
Mysterious Jack
  • 621
  • 6
  • 18
-1

May be this will spoil your learning process, but here is one solution anyway:

#include<stdio.h>
int main()
{ 
    int a[9];
    int i;
    int cont_sum = 0;
    int prev;
    for(i=0;i<9;i++)
        scanf("%d",&a[i]);

    prev = a[0];
    cont_sum = prev;
    for(i=1;i<9;i++)
    {
        if(a[i] != prev)
        {
            printf("contiguous sub sequence sum: %d\n", cont_sum);
            prev = a[i];
            cont_sum = prev;
        }
        else
        {
            prev = a[i];
            cont_sum += prev;
        }
    }
    printf("contiguous sub sequence sum: %d\n", cont_sum);
    getch();
}
bytefire
  • 4,156
  • 2
  • 27
  • 36