Solved

2-d array

Posted on 1998-08-20
9
380 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

695 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