944,103 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2725
  • C RSS
Oct 29th, 2004
0

Array problem

Expand Post »
Can someone out there help me with this prog. ??
I have been trying to get this for the past 2 weeks !!

The question is:
Write a program to input an array having n integers. Display the array. Input a number x. Perform the following functions:
1)Delete all the occurunces of x;
2)Shift all the elements to the right side;
3)Fill the unused spaces by zero.

I have got an idea for deleting the program but the other 2 remain unsolved.
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Tejas is offline Offline
6 posts
since Jun 2004
Oct 29th, 2004
0

Re: Array problem

>I have been trying to get this for the past 2 weeks !!
Do you have any code after these two weeks?

>1)Delete all the occurunces of x;
Removing an item from an array is an involved procedure. You need to shift all items after it to fill in the hole . But in this case it makes more sense to shift all items before it because you're filling the unused items on the left with 0. The trick is to handle the shifting and zero fill at the same time:
  1. #include <algorithm>
  2. #include <cstddef>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. #define size(a) (sizeof a / sizeof *a)
  8.  
  9. int main()
  10. {
  11. int a[] = {1,2,3,1,2,3,1,2,3,1,2,3};
  12. int x = 2;
  13.  
  14. for ( size_t i = 0; i < size ( a ); i++ ) {
  15. if ( a[i] == x ) {
  16. // Remove and shift
  17. for ( size_t j = i; j > 0; j-- )
  18. a[j] = a[j - 1];
  19.  
  20. // Zero fill
  21. a[0] = 0;
  22. }
  23. }
  24.  
  25. copy ( a, a + size ( a ), ostream_iterator<int> ( cout, " " ) );
  26. cout<< endl;
  27. }
This solution can be optimized if the valid items cannot be 0. Then you can only shift until you find a 0 and fill in that hole, thus saving yourself part of an expensive operation:
  1. #include <algorithm>
  2. #include <cstddef>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. #define size(a) (sizeof a / sizeof *a)
  8.  
  9. int main()
  10. {
  11. int a[] = {1,2,3,1,2,3,1,2,3,1,2,3};
  12. int x = 2;
  13.  
  14. for ( size_t i = 0; i < size ( a ); i++ ) {
  15. if ( a[i] == x ) {
  16. // Remove and shift
  17. size_t j;
  18.  
  19. for ( j = i; j > 0 && a[j - 1] != 0; j-- )
  20. a[j] = a[j - 1];
  21.  
  22. // Zero fill
  23. a[j] = 0;
  24. }
  25. }
  26.  
  27. copy ( a, a + size ( a ), ostream_iterator<int> ( cout, " " ) );
  28. cout<< endl;
  29. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 29th, 2004
0

Re: Array problem

Cant any one give a simpler program. I am a beginner and just started to use functions !!
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Tejas is offline Offline
6 posts
since Jun 2004
Oct 29th, 2004
0

Re: Array problem

>Cant any one give a simpler program.
I'm not going to give you something that you can turn in and call your own, so don't expect that. The part of the program that matters to you is simple enough for anyone with more than a day's worth of experience with C++ to figure out. So stop being lazy and actually put some effort in. I don't usually take the time to write up a compilable program that solves the entire problem; you could be a little more grateful.

>I am a beginner and just started to use functions !!
Arrays, loops, and if statements are taught before functions, what's your problem?

Since you don't like it when someone gives you a full description of the best algorithm for the job along with the complete source, I'll be less helpful for your future questions.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 29th, 2004
0

Re: Array problem

#include<stdio.h>
# define MAX 10

main()
{


int a[MAX],b[MAX],i,j,k,key,count=0;


printf("\n Enter the integers");

for(i=0;i<MAX;i++)
scanf("%d",&a[i]);

printf("\n The elements are ");
for(i=0;i<MAX;i++)
printf(" %d",a[i]);

printf("\nEnter the key");
scanf("%d",&key);


for(i=0;i<MAX;i++)
if(a[i]==key)
{
a[i]='#';
count++;
}
for(j=0;j<count;j++)
for(i=0;i<MAX;i++)
if(a[i]=='#')
for(k=i;k>0;k--)
a[k]=a[k-1];

for(j=0;j<count;j++)
a[j]=0;

for(i=0;i<MAX;i++)
printf(" %d",a[i]);
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cuperman is offline Offline
3 posts
since Oct 2004

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: what does keybd_event() do????
Next Thread in C Forum Timeline: pls heeeeeeeeelp its urgent.





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


Follow us on Twitter


© 2011 DaniWeb® LLC