Sie sind auf Seite 1von 2

//

11.1 delete value according to row & coloumn


void main ( void )
{
const int row = 3, coloumn = 3;
int arr[row][coloumn] =
{
{1,2,3},
{4,5,6},
{7,8,9},
};
for( int i=0;i <row; ++i )
{
for( int j=0; j<coloumn; ++j )
cout<<setw(5)<<arr[i][j];
cout<<endl;
}
short del = 0, row_i = -1, coloumn_i = -1;
cout<<"\n\nEnter Value to Delete:";
cin>>del;
for( int i=0; (i < row) && (row_i == -1); ++i )
{
for( int j=0; (j < coloumn) && (coloumn_i == -1); ++j)
{
if( del == arr[i][j] )
{
row_i = i;
coloumn_i = j;
}
}
}
if( row_i != -1 && coloumn_i != -1 )
{
while( row_i < row )
{
while( coloumn_i < coloumn )
{
arr[row_i][coloumn_i] = arr[row_i][coloumn_i+1];
coloumn_i++;
}
if( row_i+1 < row )
{
arr[row_i][coloumn_i] = arr[row_i+1][0];
coloumn_i = 0;
}
row_i++;
}
arr[row-1][coloumn-1] = 0;
cout<<"\nValue Deleted.\n";
}
else
cerr<<"\nValue not Deleted.\n";
for( int i=0;i <row; ++i )
{
for( int j=0; j<coloumn; ++j )
cout<<setw(5)<<arr[i][j];

cout<<endl;
}
_getch();
}
//
11.2 delete value using pointer
void main ( void )
{
const int row = 3, coloumn = 3;
int arr[row][coloumn] =
{
{1,2,3},
{4,5,6},
{7,8,9},
};
for( int i=0;i <row; ++i )
{
for( int j=0; j<coloumn; ++j )
cout<<setw(5)<<arr[i][j];
cout<<endl;
}
short index = -1, del = 0;
cout<<"\n\nEnter Value to Delete:";

cin>>del;

int *temp = &arr[0][0];


for( int i=0; (i < row*coloumn) && (index == -1); ++i )
{
if( del == temp[i] )
index = i;
}
if( index != -1 )
{
while( index < row*coloumn )
{
temp[index] = temp[index+1];
index++;
}
temp[index-1] = 0;
cout<<"\nValue Deleted.\n";
}
else
cerr<<"\nValue not Deleted.\n";
for( int i=0;i <row; ++i )
{
for( int j=0; j<coloumn; ++j )
cout<<setw(5)<<arr[i][j];
cout<<endl;
}
temp = NULL;
_getch();
}

Das könnte Ihnen auch gefallen