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.

Recommended Answers

All 4 Replies

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

#include <algorithm>
#include <cstddef>
#include <iostream>

using namespace std;

#define size(a) (sizeof a / sizeof *a)

int main()
{
  int a[] = {1,2,3,1,2,3,1,2,3,1,2,3};
  int x = 2;

  for ( size_t i = 0; i < size ( a ); i++ ) {
    if ( a[i] == x ) {
      // Remove and shift
      for ( size_t j = i; j > 0; j-- )
        a[j] = a[j - 1];

      // Zero fill
      a[0] = 0;
    }
  }

  copy ( a, a + size ( a ), ostream_iterator<int> ( cout, " " ) );
  cout<< endl;
}

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:

#include <algorithm>
#include <cstddef>
#include <iostream>

using namespace std;

#define size(a) (sizeof a / sizeof *a)

int main()
{
  int a[] = {1,2,3,1,2,3,1,2,3,1,2,3};
  int x = 2;

  for ( size_t i = 0; i < size ( a ); i++ ) {
    if ( a[i] == x ) {
      // Remove and shift
      size_t j;

      for ( j = i; j > 0 && a[j - 1] != 0; j-- )
        a[j] = a[j - 1];

      // Zero fill
      a[j] = 0;
    }
  }

  copy ( a, a + size ( a ), ostream_iterator<int> ( cout, " " ) );
  cout<< endl;
}

Cant any one give a simpler program. I am a beginner and just started to use functions !!

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

#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]);
 }
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.