Array problem

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2004
Posts: 6
Reputation: Tejas is an unknown quantity at this point 
Solved Threads: 0
Tejas Tejas is offline Offline
Newbie Poster

Array problem

 
0
  #1
Oct 29th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,730
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Array problem

 
0
  #2
Oct 29th, 2004
>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. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 6
Reputation: Tejas is an unknown quantity at this point 
Solved Threads: 0
Tejas Tejas is offline Offline
Newbie Poster

Re: Array problem

 
0
  #3
Oct 29th, 2004
Cant any one give a simpler program. I am a beginner and just started to use functions !!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,730
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Array problem

 
0
  #4
Oct 29th, 2004
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3
Reputation: cuperman is an unknown quantity at this point 
Solved Threads: 0
cuperman cuperman is offline Offline
Newbie Poster

Re: Array problem

 
0
  #5
Oct 29th, 2004
#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]);
}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC