2-d array

Is there any way in which I can delete an element from an array without implementing linked-list? In my program, I need to find the minimum value from the array and delete the element with minimum value. Then I repeat the step of searching for minimum value from the remaining elements and delete the element with minimum value.

If there is no way of avoiding the implementation of linked-list, please send any sample codes, information and URLs related to linked-list implementation.



I tried to use dynamic allocation to declare a 2-dimensional array but there was an error during compilation. Message was "Cannot convert void* to double*".

typedef  double **matrix;
typedef  double *row;
typedef  double elem;

matrix get_space(int M, int N)
{  int i;
   elem  *p;
   matrix a;
   p = malloc(M*N*sizeof(elem));     //get space for all elements
   a = malloc(M*sizeof(row));
   --a;                                                      //offset pointer
   for (i=1;i<=M;i++)
      a[i]=p+((i-1)*N)-1;
   return a;
}

Please advise. Thanks in advance.
misumiAsked:
Who is Participating?
 
MotazCommented:
// This is the code of searching smallest number and deleting it

#include <stdio.h>
#include <stdlib.h>

void main(){
  int data[100];
  int i,j,MinPos;
  int Min=32000;  // Maximam expected value

  for (i=0;i<100;i++)        // Random numbers initialization
    data[i]=random(110);

  for (i=0;i<100;i++) {    // Search for smallest number and delete it
   for (j=0;j<100;j++)
     if (Min>data[j]) {
     Min=data[j];
     MinPos=j;
     }  // end if
   printf("\n%d",Min);
   Min=32000;
   data[MinPos]=32000;
  } // end i
  getchar();
}

Motaz,
0
 
MotazCommented:
// Hi  Misumi.
// This is a linked list in C++
// Just copy and paste then run

/*************************************************/
/*          Linked List using pointers               */
/*     (C) Copyright 17-Nov-1996 10:10 pm        */
/*     Last update : 12-Jun-1998 12:47 pm        */
/*  Motaz Abdel Azim Altahir. All right reserved */
/*************************************************/


#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <alloc.h>

typedef char datatype;
const maxlist=100;
typedef struct Tlink
{
  datatype data;
  Tlink *next;
};

Tlink *head;

unsigned long FirstMem;

void press()     /***** press any key to continue massege *****/
{
 cprintf("Press any key to continue ...\r\n");
 getch();
}

void add(datatype item,Tlink **head)   /**** add ****/
{
 Tlink *temp=*head;
 if (temp==NULL) {
      temp=new Tlink;
      temp->data=item;
      temp->next=NULL;
      *head=temp;
      }
  else add(item,&temp->next);
 }

void display()    /****** display *****/
{
 Tlink *temp=head;
 while (temp!=NULL)
 {
  cprintf("%c\r\n",temp->data);
 temp=temp->next;
 }
 printf("Available memory : %ld\n",farcoreleft());
 printf("Used memory      : %ld\n",FirstMem-farcoreleft());
 press();
}

Tlink *find(datatype item,Tlink *head,Tlink **pred)  /***** find *****/
{
 Tlink *temp=head;
 if (head->data==item){
    *pred=NULL;
    return (head);
    }
   else while (temp!=NULL){
    *pred=temp;
    temp=temp->next;
    if (temp->data==item) return temp;
   }
   *pred=NULL; /** if not found **/
   return NULL;
}

void del(datatype item)  /****** delete *******/
{
 Tlink *pred,*temp;
 temp=find(item,head,&pred);
 if (temp==NULL) return;
   if (temp==head) {
     head=head->next;
     delete(temp);
    }
    else {
      pred->next=temp->next;
      delete(temp);
     }
}

void ins()      /*******  Insertion  *******/
{
 datatype item;
 do
  {
   cprintf("Input any character : ");
   item=getch();
   cprintf("%c\r\n",item);
   if (item!=13) add(item,&head);
  }
  while (item!=13);
}

void search()      /*******  search  *******/
{
 datatype sear;
 Tlink *r,*pred;
 do
 {
 cprintf("input character to search for : ");
 sear=getch();
 cprintf("%c\r\n",sear);
 if (sear!=13)
 {
  r=find(sear,head,&pred);
  if (r==NULL) cprintf("%c not found\r\n",sear);
   else
    cprintf("%c is found\r\n",sear);
  }
 } while (sear!=13);
 press();
}

void delet()   /****** deletion *******/
{
 datatype d;
 Tlink *r,*pred;
 cprintf("input character to delete : ");
 d=getch();
 cprintf("%c\r\n",d);
 if (d!=13)
 {
  r=find(d,head,&pred);
  if (r==NULL) cprintf("%c not found\r\n",d);
   else
   {
    del(d);
    cprintf("%c successuly deleted\r\n",d);
   }
  }
 press();
}

main(void)     /****** main program *******/
{
 char choise;
 head=NULL;
 FirstMem=farcoreleft();
 do {
 textcolor(14);
 window(1,1,80,25);
 textbackground(1);
 window(20,6,60,14);
 textcolor(4);
 textbackground(7);
 clrscr();
 cprintf("\n Add\r\n View\r\n Search\r\n Delete\r\n Exit\r\n");
 choise=getch();
 if (choise>96) choise=choise-32;
 textcolor(14);
 textbackground(1);
 window(1,1,80,25);
 clrscr();
 switch (choise) {
  case 'A': ins(); break;
  case 'V': display(); break;
  case 'S': search(); break;
  case 'D': delet();
  }
 } while ((choise!='E') && (choise!='X') && (choise!=27));

 return 0;
}


// Motaz from Sudan
0
 
MotazCommented:
// This is the correction of your code
// Malloc return void then you must typecast it to the left type
// e.g.   ch=(char*)malloc(20)

#include <alloc.h>

typedef  double **matrix;
typedef  double *row;
typedef  double elem;

matrix get_space(int M, int N)
{  int i;
   elem  *p;
   matrix a;
   p =(double *) malloc(M*N*sizeof(elem));     //get space for all elements
   a =(double **) malloc(M*sizeof(row));
   --a;             //offset pointer
   for (i=1;i<=M;i++)
      a[i]=p+((i-1)*N)-1;
   return a;
}

void main(){}

// Motaz, from Sudan
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
snoeglerCommented:
Your code is a little bit confusing. I think this should do what you want:

typedef  double **matrix;
typedef  double *row;
typedef  double elem;
// i am assuming that 'M' is the number of rows ...
matrix get_space(int M, int N)
{
    int row,col;
   matrix a;
   a = (matrix)malloc( sizeof(row)*M );
   for(row=1;row<=M;row++) {
     a[row]=(row)malloc(sizeof(elem)*N);
     for(col=1;col<=N;col++)   // initialize the values to zero
       a[row][col]=0.0f;
   }
   return a;
}

A few tips:
- it is not very easy to handle when 'typedef'-ing pointers to 'non-pointer' types.
  what i mean is that no one trying to read your code will assume that a 'matrix'
  type is really a (double**) type. Either name it 'ppdMatrix' (pointer-pointer-double)
  (just a suggestion, there are many naming conventions), or avoid this at all.,
  Instead, you could use at least a 'struct' which contains a double** pointer -
  it is usually much easier to understand.
- try to imagine what you want to do:

matrix array; // double **array;
double *pRow0=array[0];
double *pRow1=array[1];
// ...
double pElem0_0=array[0][0]=pRow0[0]

I hope this helped a bit.
0
 
nietodCommented:
As an improvement to all of those suggestions.  Don't use malloc() and free().  You should never use them in C++ except when writing overloaded new and delete operators.  You should use the new and delete operators instead.
0
 
MotazCommented:
That is right nietod...
Moreover new and delete dose not requere any headers, they are keywords
0
 
alexoCommented:
Another improvement: Use the STL instead of reinventing the wheel.
The list<> and vector<> templates are quite efficient (some implementations provide slist<> which has even less overhead).
0
 
nietodCommented:
Except, alex, this is probably for an assignment.  I suspect that the teacher wants misumi to do the work, not use someone else's.
0
 
alexoCommented:
>> this is probably for an assignment.
If so, then my comment is totally irrelevant.  Carry on...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.