Solved

2-d array

Posted on 1998-08-20
9
344 Views
Last Modified: 2010-04-01
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.
0
Comment
Question by:misumi
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 7

Expert Comment

by:Motaz
Comment Utility
// 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
 
LVL 7

Accepted Solution

by:
Motaz earned 50 total points
Comment Utility
// 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
 
LVL 7

Expert Comment

by:Motaz
Comment Utility
// 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
 
LVL 6

Expert Comment

by:snoegler
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
 
LVL 7

Expert Comment

by:Motaz
Comment Utility
That is right nietod...
Moreover new and delete dose not requere any headers, they are keywords
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
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
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
 
LVL 11

Expert Comment

by:alexo
Comment Utility
>> this is probably for an assignment.
If so, then my comment is totally irrelevant.  Carry on...
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now