删除数组/交换值中的元素

时间:2015-11-29 17:36:33

标签: c arrays

我正在动态分配和重新分配2d数组。我在数组中搜索匹配,如果我找到匹配项,我想删除找到匹配项的索引,我该如何实现呢?为了说明(动态分配),我假设我有一个数组,我正在搜索匹配。

MessageBox.Show("Please Select a college")  
Query = String.Format("SELECT * FROM logininfo WHERE Username = '{0}' AND
Password = '{1}' and College = '{2}'", Me.TextBox1UN.Text.Trim(),
Me.TextBox2Pass.Text.Trim, Me.ComboBox1.SelectedItem.Trim())  
ElseIf

我怎样才能实现这样的目标? 另一个说明如何交换2d数组的值(动态分配) 例子>数组看起来像

int arr [2][2]={{12,2},{1,2}}
for ( int i =0; i < 2 ; i++){
  if ( arr[i][0]=1 && arr[i][1]=2){
  // remove item from array;
}
}

我希望它看起来像

{
{5,5},
{6,6},
{7,7}
}

如何用2d数组实现它?我试过了

{
    {7,7},
    {6,6},
    {5,5}
    }

但它没有用 完整的例子

                            int ag=arr[bestDeal]; //bestdeal is matched index
                            arr[bestDeal]=ponuka[0];
                            arr[0]=ag;

3 个答案:

答案 0 :(得分:1)

参考这个例子....你会得到一个想法.......

int main()
{
   int array[100], position, c, n;

   printf("Enter number of elements in array\n");
   scanf("%d", &n);

   printf("Enter %d elements\n", n);

   for ( c = 0 ; c < n ; c++ )
      scanf("%d", &array[c]);

   printf("Enter the location where you wish to delete element\n");
   scanf("%d", &position);

   if ( position >= n+1 )
      printf("Deletion not possible.\n");
   else
   {
      for ( c = position - 1 ; c < n - 1 ; c++ )
         array[c] = array[c+1];

      printf("Resultant array is\n");

      for( c = 0 ; c < n - 1 ; c++ )
         printf("%d\n", array[c]);
   }

   return 0;
}

答案 1 :(得分:0)

以下代码可能会执行您想要的操作:

#include "stdlib.h"
int main()
{
    // Current size of array.
    int size = 4;
    int size2D = 2;
    // Number that we want to find.
    int numToFind[2] = {1, 1};
    int **a = (int**)malloc(sizeof(int*) * size);

    for (int i = 0; i < size; i++)
    {
        a[i] = (int*)malloc(sizeof(int)*size2D);
        a[i][0] = i;
        a[i][1] = i;
    }

    for (int i = 0; i < size; i++)
    {
        if (a[i][0] == numToFind[0] && a[i][1] == numToFind[1])
        {
            // Found number, move all the elements to the right of this element 
            // one step left.
            memmove(&a[i], &a[i + 1], sizeof(int*)*(size-i));

            // Reduce array size by 1.
            a = (int**)realloc(a, sizeof(int*)*(size - 1));
            size--;
        }
    }

    for (int i = 0; i < size; i++)
    {
        printf("%d, %d\n", a[i][0], a[i][1]);
    }
    free(a);
    return 0;
}

答案 2 :(得分:0)

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

#define COLS 2

void print(int **arr, int rows);
void swap_row(int **r1, int **r2);

int main(void){
    //{{5,5}, {6,6}, {1,2}, {7,7}}
    int rows = 4;
    int **arr = malloc( rows * sizeof(*arr));
    for(int r = 0; r < rows; ++r){
        arr[r] = malloc( COLS * sizeof(**arr));
    }
    memcpy(arr[0], (int[COLS]){5,5}, sizeof(int[COLS]));
    memcpy(arr[1], (int[COLS]){6,6}, sizeof(int[COLS]));
    memcpy(arr[2], (int[COLS]){1,2}, sizeof(int[COLS]));
    memcpy(arr[3], (int[COLS]){7,7}, sizeof(int[COLS]));

    print(arr, rows);

    for(int r = 0; r < rows; ++r){
        if(!memcmp(arr[r], (int[COLS]){1,2}, sizeof(int[COLS]))){
            free(arr[r]);
            memmove(&arr[r], &arr[r+1], (rows - (r+1)) * sizeof(*arr));
            --rows;
            break;
        }
    }
    print(arr, rows);//{{5,5}, {6,6}, {7,7}}

    swap_row(&arr[0], &arr[2]);
    print(arr, rows);//{{7,7}, {6,6}, {5,5}}

    for(int r = 0; r < rows; ++r)
        free(arr[r]);
    free(arr);

    return 0;
}

void print(int **arr, int rows){
    puts("{");
    for(int r = 0; r < rows; ++r){
        printf("\t{");
        for(int c = 0; c < COLS; ++c){
            if(c)
                putchar(',');
            printf("%d", arr[r][c]);
        }
        printf("}%s", (r != rows -1) ? ",\n" : "\n");
    }
    printf("}\n");
}

void swap_row(int **r1, int **r2){
    int *temp = *r1;
    *r1 = *r2;
    *r2 = temp;
}