GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-May-2005, 17:20
vaha vaha is offline
New Member
 
Join Date: May 2005
Posts: 8
vaha is on a distinguished road

array problem


Hi all, im new in C world and I need your's help for one simple prog.
Program: enter array like this A=(1,2,3,4,5,6) (number of elements n=6) you need to delete one element (z=4) from that A array and print A array like A=(1,2,3,5).

((sorry for bad english))
  #2  
Old 14-May-2005, 21:47
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Welcome to GID vaha....

So what's the problem you're having? Understanding the problem? Designing the solution? Coding the solution? Compiler arrors? Runtime errors? Logic errors?

Read the Guidelines for posting requests for help so you understand what we need to help you solve your task.
__________________

Age is unimportant -- except in cheese
  #3  
Old 15-May-2005, 03:21
ajitham's Avatar
ajitham ajitham is offline
New Member
 
Join Date: May 2005
Location: Kuala Lumpur
Posts: 17
ajitham is on a distinguished road
Hello vaha,

As a new programmer you may have this sort of problems.Even i'm new to programming even i'm having this sort of problems.

OK the problem you've got is very simple actually.Insert the following in your program to get u the result.

CPP / C++ / C Code:

//first input the array.

int position;    //input the position where you would like the deletion
int k;             //some value to represent the array element
int i;               //to loop through

k=position;

for(i=6;i>=position;i--)         //since your array length is 6
{
         a[k]=a[k+1];                   //move your array elements
         k++;
}



this is very simple is it not.Now you try yourself to insert an element at the desired position. Somewhat similar to this, but different.
  #4  
Old 15-May-2005, 07:50
vaha vaha is offline
New Member
 
Join Date: May 2005
Posts: 8
vaha is on a distinguished road
i had problem when user enters all same numbers for array elemenets and want one to remove, and when element that user wants out of array is last one. no new array is alowed to use.
here is my raw code sample:
CPP / C++ / C Code:
#include<stdio.h>
#include<conio.h>
#define MAX_NIZ 30

int i,n,z,a[MAX_NIZ];
char answer;

void main()
{
 for(i=0;i<MAX_NIZ;i++) a[i]=0;
 textcolor(12);
do
{

 do
   {
clrscr();
printf("\n\t\t Number of elements for array X:");
scanf("%d",&n);
printf("\n\t\t Enter elementsX:\n");
   }
   while(n<1 || n>MAX_NIZ );
   for(i=0;i<n;i++){
    printf("\t\t");
    scanf("%d",&a[i]);}

    printf("\n\t\t Enter elemnt for removing");

    scanf("%d",&z);

 clrscr();
 printf("\n\t\t Number of elements for array X n= %d.",n);
 printf("\n\t\t Enter elements X:");
 printf("\n\t\t");
for(i=0;i<n;i++)
 {
  textcolor(15);
  cprintf(" %d ",a[i]);}
  textbackground(7);
  textcolor(16);
  printf("\n");

for(i=0;i<n;i++)
 {
 if(a[i]==z) a[i]=a[i++];   // there is problem ---- works when i remove one elemet (not last one) 
 printf("\n\t\t %d",a[i]);  // when i enter last element to remove i get 0, when i enter to remove duplicate elements program  dont work
 }



  printf("\n\n\n\t\t\t Do you want to continue (Y/N)");
	  flushall();
	  scanf("%c",&answer);
}
	 while (answer == 'y' || answer=='Y');


 gotoxy(1,25);

 }


and one more qest. , how can i prevent users to enter letters for elements?
when i enter program crash, is ther any way to do that with standard lib -stdio.h-?
Last edited by vaha : 15-May-2005 at 08:02. Reason: forgoth something to enter
  #5  
Old 15-May-2005, 11:03
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Your problems seems to be here:
CPP / C++ / C Code:
for(i=0;i<n;i++)
{
    if(a[i]==z) a[i]=a[i++];   // there is problem 
    printf("\n\t\t %d",a[i]);  // when i enter last 
}
All this loop does is find each value z and replace it with the next value in the array. So if you have
1 4 5 8 9 and z is 5 you end up with
1 4 8 8 9 in the array.

What you need to do to remove z is
Code:
n = number of values loop thru array from 0 to n-1 if a[index] = z then if index < n-1 then loop thru array from index to n-2 moving a[index2+1] int a[index2] endif decrement n endif endloop
You can't use a for loop for the first loop because your endpoint keeps moving.

Quote:
Originally Posted by vaha
and one more qest. , how can i prevent users to enter letters for elements?
when i enter program crash, is ther any way to do that with standard lib -stdio.h-?
Stand over the user with a baseball bat ready to swing.....

Not easily. This takes a completely different form of input than you probably can do at the moment. After a few more weeks of class this will be easier.

Also read this tutorial on how to format your code. Your formatting make the code difficult to read because it is inconsistant.
__________________

Age is unimportant -- except in cheese
  #6  
Old 15-May-2005, 17:02
vaha vaha is offline
New Member
 
Join Date: May 2005
Posts: 8
vaha is on a distinguished road
Thanks WaltP,
Quote:
Originally Posted by WaltP
Your problems seems to be here:
CPP / C++ / C Code:
for(i=0;i<n;i++)
{
    if(a[i]==z) a[i]=a[i++];   // there is problem 
    printf("\n\t\t %d",a[i]);  // when i enter last 
}
All this loop does is find each value z and replace it with the next value in the array. So if you have
1 4 5 8 9 and z is 5 you end up with
1 4 8 8 9 in the array.

no, that loop do this:
if i have 1.4.5.8.9 and z= 4
i get 1.5.8.9
But when i set z=9 result= 1.4.5.8.0
and if i have 2.4.4.6.7 i can't use this loop for z=4.
Quote:
Code:
n = number of values loop thru array from 0 to n-1 if a[index] = z then if index < n-1 then loop thru array from index to n-2 moving a[index2+1] int a[index2] endif decrement n endif endloop

this is :
CPP / C++ / C Code:
for (i=0 ; i<n-1; i++)
 {
   if (a[i] == z)
     {
       if (i < (n-1) )
            {
               for (i=0; i < (n-2) ; i++)
                   {
                     a[j+1] == a[j];
                     j++;

                   }
             }

        n=j;
     }


  }
corect me if im wrong.
  #7  
Old 15-May-2005, 17:17
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough
the only problems are in your for loop conditions (I don't know if that will work, by "problem" i mean "different than the psuedocode")

"from 0 to n-1" is i < n. Therefore, the loop will run until i=n-1, and that will be its last run. Same for
"from 0 to n-2" is i < (n-1).
  #8  
Old 15-May-2005, 17:55
vaha vaha is offline
New Member
 
Join Date: May 2005
Posts: 8
vaha is on a distinguished road
problem is solved
CPP / C++ / C Code:
int k=0;

  for (int i=0; i<n; i++){
         if (a[i] != z)
                   a[k++] = a[i];
                     }
                       n = k;
   for(k=0;k<n;k++) printf("%d",a[k]);

thaks all for help.
  #9  
Old 15-May-2005, 23:58
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by vaha
problem is solved
Much better. But I had to reformat your code so it could be read:
CPP / C++ / C Code:
int k=0;

for (int i=0; i<n; i++)
{
    if (a[i] != z)  a[k++] = a[i];
}
n = k;
for(k=0;k<n;k++) printf("%d",a[k]);
Read that tutorial.... It'll help.
__________________

Age is unimportant -- except in cheese
  #10  
Old 18-May-2005, 12:23
vaha vaha is offline
New Member
 
Join Date: May 2005
Posts: 8
vaha is on a distinguished road
Quote:
by WaltP

Stand over the user with a baseball bat ready to swing.....

Not easily. This takes a completely different form of input than you probably can do at the moment. After a few more weeks of class this will be easier.


nice.
To prevent entering letters and sings.
I have something like this:

CPP / C++ / C Code:
do {
      printf(" Enter only numbers ");
      scanf(" %d ", &element);
     }
while(element >= "ASCii code (0)" && element <= "ASCii code (9)");
if this work do i need to #include some library?
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem modifying an array of char in a function ronin C Programming Language 10 28-Mar-2005 18:15
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 21:26
problem reading to a dynamic array noamfrie C Programming Language 9 02-Jan-2005 18:35
How to search an array problem jenmaz C Programming Language 4 17-Nov-2004 01:58
Problem in array Kay Chan C Programming Language 2 05-Oct-2004 21:16

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:50.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.