943,635 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2527
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
May 16th, 2009
0

simple display problem??

Expand Post »
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??
Similar Threads
Reputation Points: 3
Solved Threads: 15
Posting Whiz
Aamit is online now Online
341 posts
since Apr 2008
May 16th, 2009
0

Re: simple display problem??

Are you sure that you're a software engineer (as described in your profile) ?
  1. 10
  2. 20
  3. 30
  4. 40
(Just display the array)

  1. 10+20
  2. 10+30
  3. 10+40
  4. 20+30
  5. 20+40
  6. 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 ...)
Last edited by tux4life; May 16th, 2009 at 4:37 am.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 16th, 2009
-1

Re: simple display problem??

Can You please give me sample code??
Reputation Points: 3
Solved Threads: 15
Posting Whiz
Aamit is online now Online
341 posts
since Apr 2008
May 16th, 2009
1

Re: simple display problem??

Click to Expand / Collapse  Quote originally posted by Aamit ...
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!
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 16th, 2009
0

Re: simple display problem??

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

  1. for (i=0;i<=count;i++)
  2. {
  3. printf("%d",arr[i]);
  4. }
how to move on next step
Reputation Points: 3
Solved Threads: 15
Posting Whiz
Aamit is online now Online
341 posts
since Apr 2008
May 16th, 2009
0

Re: simple display problem??

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
Last edited by tux4life; May 16th, 2009 at 6:23 am.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 16th, 2009
0

Re: simple display problem??

To achieve the next milestone in your program () the following might help you:
  1. 10+20
  2. 10+30
  3. 10+40
  4. 20+30
  5. 20+40
  6. 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
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 16th, 2009
0

Re: simple display problem??

For the third part:
  1. 10+20+30 => all elements except the last one
  2. 10+20+40 => all elements except the one before the last one
  3. 20+30+40 => all elements except the first one
  4. 10+20+30+40 => all elements
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 16th, 2009
0

Re: simple display problem??

  1. #include<stdio.h>
  2. #include<conio.h>
  3. main()
  4. {
  5. printf("hello");
  6. int arr[] = {10,20,30,40};
  7. int count=sizeof(arr)/sizeof(int);
  8. int i,j;
  9. for (i=0;i<count;i++)
  10. {
  11. printf("%d",arr[i]);
  12. printf("\n");
  13. for(j=i+1;j<count;j++)
  14. {
  15. printf("%d + %d",arr[i],arr[j]); printf("\n");
  16. }
  17.  
  18. }
  19. getch();
  20. }
but problem is how to make this dynamic??
or display all numbers
Last edited by Ancient Dragon; May 16th, 2009 at 8:20 am. Reason: fix code tags
Reputation Points: 3
Solved Threads: 15
Posting Whiz
Aamit is online now Online
341 posts
since Apr 2008
May 16th, 2009
0

Re: simple display problem??

>>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.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Compare int value : do not use relational operator.
Next Thread in C Forum Timeline: Array of Stucts - Printing Strings





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC