| | |
Array problem
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2004
Posts: 6
Reputation:
Solved Threads: 0
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.
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.
>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:
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:
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:
C Syntax (Toggle Plain Text)
#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; }
C Syntax (Toggle Plain Text)
#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; }
I'm here to prove you wrong.
>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 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.
•
•
Join Date: Oct 2004
Posts: 3
Reputation:
Solved Threads: 0
#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]);
}
# 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]);
}
![]() |
Similar Threads
- Strange array problem (PHP)
- simple array problem not so simple (Java)
- Array problem (C++)
- Array problem (C#)
- Is there a simplest way to work this array problem? (C++)
- class array problem! (C++)
- C++ help with student array problem (C++)
- i have problem with array plz help (Java)
- Large Array Problem (PHP)
Other Threads in the C Forum
- Previous Thread: what does keybd_event() do????
- Next Thread: pls heeeeeeeeelp its urgent.
| Thread Tools | Search this Thread |
* ansi api append array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlogicaldrivestrin givemetehcodez grade graphics gtkwinlinux histogram homework i/o ide inches include infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft multi mysql oddnumber open opendocumentformat openwebfoundation overwrite pdf pointer pointers posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string strings suggestions test testautomation threads unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






