954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

simple display problem??

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??

Aamit
Posting Whiz
342 posts since Apr 2008
Reputation Points: 3
Solved Threads: 15
 

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 ...)

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

Can You please give me sample code??

Aamit
Posting Whiz
342 posts since Apr 2008
Reputation Points: 3
Solved Threads: 15
 
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!

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

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

Aamit
Posting Whiz
342 posts since Apr 2008
Reputation Points: 3
Solved Threads: 15
 

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<<strong>=</strong>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

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

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 time10+ '20,30,40', for 20 it's the same: 20+ '30,40', and so on :)

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

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

:)

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 
#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

Aamit
Posting Whiz
342 posts since Apr 2008
Reputation Points: 3
Solved Threads: 15
 

>>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.

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

>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

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

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 :)

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

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??

Aamit
Posting Whiz
342 posts since Apr 2008
Reputation Points: 3
Solved Threads: 15
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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..

Aamit
Posting Whiz
342 posts since Apr 2008
Reputation Points: 3
Solved Threads: 15
 

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'

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

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#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??

Aamit
Posting Whiz
342 posts since Apr 2008
Reputation Points: 3
Solved Threads: 15
 

>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 ...

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

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);

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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(); 
}
Aamit
Posting Whiz
342 posts since Apr 2008
Reputation Points: 3
Solved Threads: 15
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You