hi,
In my array
arr={10,20,30,40}

i want to display output like
10
20
30
40
10+20
10+30
10+40
20+30
20+40
30+40
10+20+30
10+20+40
20+30+40
10+20+30+40

How to display output like this??
how to do this please help me??

Recommended Answers

All 30 Replies

Are you sure that you're a software engineer (as described in your profile) ?

10
20
30
40

(Just display the array)

10+20
10+30
10+40
20+30
20+40
30+40

(Take the first element and display all the elements after it, take the second element and display all the elements after it, take the third element and ...)

Can You please give me sample code??

commented: Nope -1

Can You please give me sample code??

Why should I? If I give you sample code, I'm actually doing your homework, so the answer is (and will always be): NO!

int array[] = {10,20,30,40};
find length of array and store in count=4

for (i=0;i<=count;i++)
{
printf("%d",arr[i]);
}

how to move on next step

That's already a lot better, you're making progress :)
To find the length of the array you can do something like this: const int count=sizeof(array)/sizeof(int); or, if your array is always of the same length: const int count=4; ( const is optional in both cases, but I would recommend it in the second one) for (i=0;i<[B]=[/B]count;i++) has to be: for (i=0;i<count;i++) (otherwise you're overriding the array's bounds)

Now you've already the code to print out the whole array you've already completed 30% of your homework :P

To achieve the next milestone in your program (:P) the following might help you:

10+20
10+30
10+40
20+30
20+40
30+40

You've to run the loop three times (the number of elements in the array decreased by one)
Every time you display all the elements coming after the element, so if you've 10, you're displaying each time 10+ '20,30,40', for 20 it's the same: 20+ '30,40', and so on :)

For the third part:

10+20+30 => all elements except the last one
10+20+40 => all elements except the one before the last one
20+30+40 => all elements except the first one
10+20+30+40 => all elements

:)

#include<stdio.h>
#include<conio.h>
main()
{
printf("hello");
int arr[] = {10,20,30,40};
int count=sizeof(arr)/sizeof(int); 
int i,j;
  for (i=0;i<count;i++)
  {
      printf("%d",arr[i]);
      printf("\n");
      for(j=i+1;j<count;j++)
      {
            printf("%d + %d",arr[i],arr[j]);  printf("\n");              
      }
      
  }
getch(); 
}

but problem is how to make this dynamic??
or display all numbers

>>but problem is how to make this dynamic??
What do you mean by dynamic?

BTW, do you realize that the code which you have written is no where near to c++?
This is a C code. And you participate in a C++ forum. No doubt you have a red dot on the reputation bar.
C++ is not C. There is a hell lot of difference between these two language.
You should start learning C++. Buy some books like Accelerated C++
and learn the language first.

commented: Wrong. C IS C++. But C++ is not C -4
commented: C is C++ with minor exceptions, by the way: siddhant3s, you didn't say something wrong :) +4

>but problem is how to make this dynamic??
or display all numbers
I don't understand that, can you give me an example of this?

BTW, your code is in C, though it's valid C++ code the C++ way of I/O is preferred over the I/O from stdio.h Another remark: maybe your compiler isn't complaining about this (in this case you're probably using a dinosaur compiler): main() has to be int main() , the main function always has to return a value :)

Edit:: Sorry siddhantes I didn't see your post

To the OP: Please read Why shouldn't you use an old compiler and What should main() return


>>BTW, your code is in C, though it's valid C++ code the C++ way of I/O is
>>preferred over the I/O from stdio.h
May be not. As you already said the OP is using void as the return value of main; this makes it a invalid C and C++ code.

>>Sorry siddhantes I didn't see your post
I don't mind as long as you spell my name right :)

commented: How do I spell your name right? +4

I am using dev-cpp++ 4.9.9.2

write now i get output
hello
10
10 + 20
10 + 30
10 + 40
20
20 + 30
20 + 40
30
30 + 40
40

so third step i want to add 1 more for loop....
but when my array having length varies means for any length of array what to do??

You need two sets of loops. The first loop will only display the values of the array.

for (i=0;i<count;i++)
  {
      printf("%d\n",arr[i]);
  }

Then you need another set of loops, similar to the one you already posted, but just prints printf("%d + %d\n",arr[i],arr[j]); >>but when my array having length varies means for any length of array what to do??
Your program already takes care of that. Change the array size, recompile, and rerun to see what happens.

but i was asking when my array
int arr[] = {10,20,30,40,50,60,70,80,90,100};
or

int arr[] = {10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100};

For this how to write the code so i got the output..

but i was asking when my array
int arr[] = {10,20,30,40,50,60,70,80,90,100};
or

int arr[] = {10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100};

For this how to write the code so i got the output..

I already addressed this in one of my previous posts :)

Edit:: Ah, now I understand what you meant with: 'how to make this dynamic'

I already addressed this in one of my previous posts :)

Edit:: Ah, now I understand what you meant with: 'how to make this dynamic'

I think you are talking about your
this post
That's already a lot better, you're making progress
To find the length of the array you can do something like this:
const int count=sizeof(array)/sizeof(int);
or, if your array is always of the same length: const int count=4;
( const is optional in both cases, but I would recommend it in the second one)

for (i=0;i<=count;i++) has to be: for (i=0;i<count;i++) (otherwise you're overriding the array's bounds)

Now you've already the code to print out the whole array you've already completed 30% of your homework

#include<stdio.h>
#include<conio.h>
main()
{
printf("hello");
int arr[] = {10,20,30,40};
int count=sizeof(arr)/sizeof(int); 
int i,j;
  for (i=0;i<count;i++)
  {
      printf("%d",arr[i]);
      printf("\n");
      for(j=i+1;j<count;j++)
      {
            printf("%d + %d",arr[i],arr[j]);  printf("\n");              
      }
      // i was asking about this..
  }
getch(); 
}

how to write function or making dynamic program for any length array??

>I think you are talking about your this post: blablabla
No, I was talking about the 7th and 8th post of this thread :) (and of course also the post you thought about)

Edit:: If you combine these posts you'll be able to create such a program ...

but i was asking when my array
int arr[] = {10,20,30,40,50,60,70,80,90,100};
or

int arr[] = {10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100};

For this how to write the code so i got the output..

You don't read very well do you?? This line takes care of that int count=sizeof(arr)/sizeof(int);

hi,
i am interested in looping structure..

#include<stdio.h>
#include<conio.h>
main()
{
printf("hello");
int arr[] = {10,20,30,40};
//when i was use these two array
/*int arr[] = {10,20,30,40,50,60,70,80,90,100};
or

int arr[] = {10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100};
*/

int count=sizeof(arr)/sizeof(int); 
//here i got  4 or 10 or 20
int i,j;
  for (i=0;i<count;i++) // this loop is ok
  {
      printf("%d",arr[i]);
      printf("\n");
      for(j=i+1;j<count;j++)
      {
            printf("%d + %d",arr[i],arr[j]);  printf("\n");              
      }
      // i was asking about this..
  }
getch(); 
}

>i am interested in looping structure..
There are plenty of C tutorials on the internet, just use Google to find them :)

I give up. Read post #14 again.

commented: Hang in there :) +10

Hi,
i modified my code as you suggested to me.

#include<stdio.h>
#include<conio.h>
int main()
{
printf("hello");printf("\n");
int arr[] = {10,20,30,40,50,60,70,80,90,100};
int count=sizeof(arr)/sizeof(int); 
int i,j,k;
  for (i=0;i<count;i++)
  {
      printf("%d",arr[i]);
      printf("\n");
      for(j=i+1;j<count;j++)
      {
            printf("%d + %d",arr[i],arr[j]);  printf("\n");   
         for(k=j+1;k<count;k++)
         {
            printf("%d + %d + %d",arr[i],arr[j],arr[k]);  printf("\n");              
         }           
      }
      
  }
getch(); 
}

i got output

hello
10
10 + 20
10 + 20 + 30
10 + 20 + 40
10 + 20 + 50
10 + 20 + 60
10 + 20 + 70
10 + 20 + 80
10 + 20 + 90
10 + 20 + 100
10 + 30
10 + 30 + 40
10 + 30 + 50
10 + 30 + 60
10 + 30 + 70
10 + 30 + 80
10 + 30 + 90
10 + 30 + 100
10 + 40
10 + 40 + 50
10 + 40 + 60
10 + 40 + 70
10 + 40 + 80
10 + 40 + 90
10 + 40 + 100
10 + 50
10 + 50 + 60
10 + 50 + 70
10 + 50 + 80
10 + 50 + 90
10 + 50 + 100
10 + 60
10 + 60 + 70
10 + 60 + 80
10 + 60 + 90
10 + 60 + 100
10 + 70
10 + 70 + 80
10 + 70 + 90
10 + 70 + 100
10 + 80
10 + 80 + 90
10 + 80 + 100
10 + 90
10 + 90 + 100
10 + 100
20
20 + 30
20 + 30 + 40
20 + 30 + 50
20 + 30 + 60
20 + 30 + 70
20 + 30 + 80
20 + 30 + 90
20 + 30 + 100
20 + 40
20 + 40 + 50
20 + 40 + 60
20 + 40 + 70
20 + 40 + 80
20 + 40 + 90
20 + 40 + 100
20 + 50
20 + 50 + 60
20 + 50 + 70
20 + 50 + 80
20 + 50 + 90
20 + 50 + 100
20 + 60
20 + 60 + 70
20 + 60 + 80
20 + 60 + 90
20 + 60 + 100
20 + 70
20 + 70 + 80
20 + 70 + 90
20 + 70 + 100
20 + 80
20 + 80 + 90
20 + 80 + 100
20 + 90
20 + 90 + 100
20 + 100
30
30 + 40
30 + 40 + 50
30 + 40 + 60
30 + 40 + 70
30 + 40 + 80
30 + 40 + 90
30 + 40 + 100
30 + 50
30 + 50 + 60
30 + 50 + 70
30 + 50 + 80
30 + 50 + 90
30 + 50 + 100
30 + 60
30 + 60 + 70
30 + 60 + 80
30 + 60 + 90
30 + 60 + 100
30 + 70
30 + 70 + 80
30 + 70 + 90
30 + 70 + 100
30 + 80
30 + 80 + 90
30 + 80 + 100
30 + 90
30 + 90 + 100
30 + 100
40
40 + 50
40 + 50 + 60
40 + 50 + 70
40 + 50 + 80
40 + 50 + 90
40 + 50 + 100
40 + 60
40 + 60 + 70
40 + 60 + 80
40 + 60 + 90
40 + 60 + 100
40 + 70
40 + 70 + 80
40 + 70 + 90
40 + 70 + 100
40 + 80
40 + 80 + 90
40 + 80 + 100
40 + 90
40 + 90 + 100
40 + 100
50
50 + 60
50 + 60 + 70
50 + 60 + 80
50 + 60 + 90
50 + 60 + 100
50 + 70
50 + 70 + 80
50 + 70 + 90
50 + 70 + 100
50 + 80
50 + 80 + 90
50 + 80 + 100
50 + 90
50 + 90 + 100
50 + 100
60
60 + 70
60 + 70 + 80
60 + 70 + 90
60 + 70 + 100
60 + 80
60 + 80 + 90
60 + 80 + 100
60 + 90
60 + 90 + 100
60 + 100
70
70 + 80
70 + 80 + 90
70 + 80 + 100
70 + 90
70 + 90 + 100
70 + 100
80
80 + 90
80 + 90 + 100
80 + 100
90
90 + 100
100

But actual output is for this array is attached with this post in out.txt file
so i was asking for looping structure and dynamic solution..

We are giving you a dynamic solution, the only problem is that you aren't accepting it :(

Your problem is perhaps that you have not planned proper algorithm before writing this code.
Sit down with pencil and paper and plan out out what you want to accomplish. Then analyze it through the eye of an compiler. Try to go through the whole algorithm step by step as if a computer would.

Try-and-error is not correct methodology to learn how to program. Besides, you strongly need to learn C++. Pick up some book. If you don't want to spend money, there is a book "Thinking in C++ by Bruce Eckels" available free on the internet. It is one of the finniest book in this regard. Else, go for Accelerated C++.

Siddhant3s, we're in the C forum here :P

Not when I posted that post. Anyways, all my remarks remain the same except that you could strip off the Advices for books.

hi,all
i am modified my code

#include<stdio.h>
#include<conio.h>
int main()
{
printf("hello");printf("\n");
int arr[] = { 10, 20, 30, 40 } ;
  const int N = sizeof(arr) / sizeof( arr[0] ) ;

  for( int i = 0 ; i < N ; ++i ) 
   printf("\n%d",arr[i]);printf("\n");

  for( int i = 0 ; i < N-1 ; ++i )
      for( int j = i+1 ; j < N ; ++j )
            
             printf("\n%d + %d ",arr[i],arr[j]);printf("\n");

  for( int i = 0 ; i < N-2 ; ++i )
      for( int j = i+1 ; j < N-1 ; ++j )
          for( int k = j+1 ; k < N ; ++k )
                printf("\n%d + %d + %d ",arr[i],arr[j],arr[k]);printf("\n");

  for( int i = 0 ; i < N-3 ; ++i )
      for( int j = i+1 ; j < N-2 ; ++j )
          for( int k = j+1 ; k < N-1 ; ++k )
              for( int m = k+1 ; m < N ; ++m )
                  printf("\n%d + %d + %d + %d",arr[i],arr[j],arr[k],arr[m]);printf("\n");

getch(); 
}

but problem remains the same...
can you give me idea so that i can implement it as dynamic for any length of array??

Can you believe I am so stupid that I don't know the meaning of `dynamic solution', Can you please help me by telling me what exactly you mean by dynamic solution rather than using fancy words?

PS: The code you are using will work for any array length. The only problem is that you will have to initialize the array at( or before) the compile time.

For int arr[] = { 10, 20, 30, 40 } ;
This out put up to length 4 is correct
10
20
30
40

10 + 20
10 + 30
10 + 40
20 + 30
20 + 40
30 + 40

10 + 20 + 30
10 + 20 + 40
10 + 30 + 40
20 + 30 + 40

10 + 20 + 30 + 40

And

For int arr[] = { 10, 20, 30, 40,50,60,70 } ;

My program gives output

10
20
30
40
50
60
70

10 + 20
10 + 30
10 + 40
10 + 50
10 + 60
10 + 70
20 + 30
20 + 40
20 + 50
20 + 60
20 + 70
30 + 40
30 + 50
30 + 60
30 + 70
40 + 50
40 + 60
40 + 70
50 + 60
50 + 70
60 + 70

10 + 20 + 30
10 + 20 + 40
10 + 20 + 50
10 + 20 + 60
10 + 20 + 70
10 + 30 + 40
10 + 30 + 50
10 + 30 + 60
10 + 30 + 70
10 + 40 + 50
10 + 40 + 60
10 + 40 + 70
10 + 50 + 60
10 + 50 + 70
10 + 60 + 70
20 + 30 + 40
20 + 30 + 50
20 + 30 + 60
20 + 30 + 70
20 + 40 + 50
20 + 40 + 60
20 + 40 + 70
20 + 50 + 60
20 + 50 + 70
20 + 60 + 70
30 + 40 + 50
30 + 40 + 60
30 + 40 + 70
30 + 50 + 60
30 + 50 + 70
30 + 60 + 70
40 + 50 + 60
40 + 50 + 70
40 + 60 + 70
50 + 60 + 70

10 + 20 + 30 + 40
10 + 20 + 30 + 50
10 + 20 + 30 + 60
10 + 20 + 30 + 70
10 + 20 + 40 + 50
10 + 20 + 40 + 60
10 + 20 + 40 + 70
10 + 20 + 50 + 60
10 + 20 + 50 + 70
10 + 20 + 60 + 70
10 + 30 + 40 + 50
10 + 30 + 40 + 60
10 + 30 + 40 + 70
10 + 30 + 50 + 60
10 + 30 + 50 + 70
10 + 30 + 60 + 70
10 + 40 + 50 + 60
10 + 40 + 50 + 70
10 + 40 + 60 + 70
10 + 50 + 60 + 70
20 + 30 + 40 + 50
20 + 30 + 40 + 60
20 + 30 + 40 + 70
20 + 30 + 50 + 60
20 + 30 + 50 + 70
20 + 30 + 60 + 70
20 + 40 + 50 + 60
20 + 40 + 50 + 70
20 + 40 + 60 + 70
20 + 50 + 60 + 70
30 + 40 + 50 + 60
30 + 40 + 50 + 70
30 + 40 + 60 + 70
30 + 50 + 60 + 70
40 + 50 + 60 + 70

But correct OUTPUT is Required...
10
20
30
40
50
60
70
10+20
10+30
10+40
10+50
10+60
10+70
20+30
20+40
20+50
20+60
20+70
30+40
30+50
30+60
30+70
40+50
40+60
40+70
50+60
50+70
60+70
10+20+30
10+20+40
10+20+50
10+20+60
10+20+70
10+30+40
10+30+50
10+30+60
10+30+70
10+40+50
10+40+60
10+40+70
10+50+60
10+50+70
10+60+70
20+30+40
20+30+50
20+30+60
20+30+70
20+40+50
20+40+60
20+40+70
20+50+60
20+50+70
20+60+70
30+40+50
30+40+60
30+40+70
30+50+60
30+50+70
30+60+70
40+50+60
40+50+70
40+60+70
50+60+70
10+20+30+40
10+20+30+50
10+20+30+60
10+20+30+70
10+20+40+50
10+20+40+60
10+20+40+70
10+20+50+60
10+20+50+70
10+20+60+70
10+30+40+50
10+30+40+60
10+30+40+70
10+30+50+60
10+30+50+70
10+30+60+70
10+40+50+60
10+40+50+70
10+40+60+70
10+50+60+70
20+30+40+50
20+30+40+60
20+30+40+70
20+30+50+60
20+30+50+70
20+30+60+70
20+40+50+60
20+40+50+70
20+40+60+70
20+50+60+70
30+40+50+60
30+40+50+70
30+40+60+70
30+50+60+70
40+50+60+70
10+20+30+40+50
10+20+30+40+60
10+20+30+40+70
10+20+30+50+60
10+20+30+50+70
10+20+30+60+70
10+20+40+50+60
10+20+40+50+70
10+20+40+60+70
10+20+50+60+70
10+30+40+50+60
10+30+40+50+70
10+30+40+60+70
10+30+50+60+70
10+40+50+60+70
20+30+40+50+60
20+30+40+50+70
20+30+40+60+70
20+30+50+60+70
20+40+50+60+70
30+40+50+60+70
10+20+30+40+50+60
10+20+30+40+50+70
10+20+30+40+60+70
10+20+30+50+60+70
10+20+40+50+60+70
10+30+40+50+60+70
20+30+40+50+60+70
10+20+30+40+50+60+70


So every time i am unable to add more loops...
so i was asking for how to do it ??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.