Solved

2-d array

Posted on 1998-08-20
9
354 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
ID: 1170751
// 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
ID: 1170752
// 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
ID: 1170753
// 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
ID: 1170754
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:nietod
ID: 1170755
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
ID: 1170756
That is right nietod...
Moreover new and delete dose not requere any headers, they are keywords
0
 
LVL 11

Expert Comment

by:alexo
ID: 1170757
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
ID: 1170758
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
ID: 1170759
>> this is probably for an assignment.
If so, then my comment is totally irrelevant.  Carry on...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

914 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

19 Experts available now in Live!

Get 1:1 Help Now