本文介绍了目前我正在尝试将元素插入到数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我目前的代码,我哪里出错了?







below is my current code, where did I went wrong ?



#include <stdio.h>  /* printf, scanf*/
#include <stdlib.h> /* malloc, free */

void readArray(int **, int *);
void insert(int **, int *, int, int, const int *);
void delete(int **, int *, int, int);
void printArray(const int *, int);

int main(void)
{
 int *Arr, N, operation, pos, n;
 readArray(&Arr,&N); 
 scanf("%d%d%d",&operation, &pos, &n);
 if(operation==1) /* deletion */ 
  delete(&Arr,&N,pos,n);  
 else /*insertion*/
 {
  int *insert_array = (int *)malloc(n*sizeof(int));
  int i;  
  for(i=0;i<=n-1;++i)
   scanf("%d",&insert_array[i]);
  insert(&Arr,&N,pos,n,insert_array);
  free(insert_array);
 }
 printArray(Arr,N);
 free(Arr);
 return 0;
}




void readArray(int **array, int *size)
{ 
  /* counter */
  int i;
  
  /* taking in the size */
  scanf("%d", size);
  

  /* allocate memory for array */
  for(i=0;i < *size;i++)
  {
    /* memory allocate for the array */
    (*array) = (int*)malloc (*size * sizeof(int));
  
    /* special case */
    if(*array == NULL)
    {
      return;
    }
  
  }
  /* printf("%d", *array); */
  
}

void insert(int **x, int *npts, int pos, int num_inserted, 
const int *element_inserted)
{   
  /* counter */
   int i, num;
   
   
   /* printf("it goes here"); */
   /* num = *npts + num_inserted; */
   
   
   printf("the value of pos: %d\n", pos);
   printf("the value of element_inserted: %d\n", *element_inserted);
   printf("the value of x: %d\n", *x);
   printf("the value of num_insert: %d\n", num_inserted);
   printf("the value of npts: %d\n", *npts);
 
 
 /* create space at the specified location */
 /*                                        */
 /*                                        */
 
 for(i = *npts-1 ; i > pos ; i--)
 {
   (x+i) = (x+i-1);
 }
 
  
  *npts ++; 
  *x[pos - 1] = num_inserted + *(npts);
  
  /*                                   */
  /* print out the result of insertion */
  /*                                   */
  
  for(i=0; i < *npts ; i++)
  {
    printf("%d", *(x+i));
  }
  
}

void delete(int **x, int *npts, int pos, int num_deleted)
{ 
}

void printArray(const int *array, int size)
{
  /* counter */
 /*    int i;
  
  for(i=0;i<size;i++)
  {
    printf("%d",*array  );
  } */
}

推荐答案

// codeproject_help.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <stdio.h>  /* printf, scanf*/
#include <stdlib.h> /* malloc, free */

void readArray(int **, int *);
void insert(int **, int *, int, int, const int *);
void delete_(int **, int *, int, int);
void printArray(const int *, int);
 
int main(void)
{
 int *Arr, N, operation, pos, n;
 readArray(&Arr,&N); 
 scanf("%d%d%d",&operation, &pos, &n);
 if(operation==1) /* deletion */ 
  delete_(&Arr,&N,pos,n);  
 else /*insertion*/
 {
  int *insert_array = (int *)malloc(n*sizeof(int));
  int i;  
  for(i=0;i<=n-1;++i)
   scanf("%d",&insert_array[i]);
  insert(&Arr,&N,pos,n,insert_array);
  free(insert_array);
 }
 printArray(Arr,N);
 free(Arr);
 return 0;
}
 

 

void readArray(int **array, int *size)
{ 
  /* counter */
  int i;
  
  /* taking in the size */
  scanf("%d", size);
  
 
  /* allocate memory for array */
  for(i=0;i < *size;i++)
  {
    /* memory allocate for the array */
    (*array) = (int*)malloc (*size * sizeof(int));
  
    /* special case */
    if(*array == NULL)
    {
      return;
    }
  
  }
  /* printf("%d", *array); */
  
}
 
void insert(int **x, int *npts, int pos, int num_inserted, 
const int *element_inserted)
{   
  /* counter */
   int i, num;
   
   
   /* printf("it goes here"); */
   /* num = *npts + num_inserted; */
   
   
   printf("the value of pos: %d\n", pos);
   printf("the value of element_inserted: %d\n", *element_inserted);
   printf("the value of x: %d\n", *x);
   printf("the value of num_insert: %d\n", num_inserted);
   printf("the value of npts: %d\n", *npts);
 
 
 /* create space at the specified location */
 /*                                        */
 /*                                        */
 
 for(i = *npts-1 ; i > pos ; i--)
 {
//    (x+i) = (x+i-1);
   *(x+i) = *(x+i-1);
 }
 
  
  *npts ++; 
  *x[pos - 1] = num_inserted + *(npts);
  
  /*                                   */
  /* print out the result of insertion */
  /*                                   */
  
  for(i=0; i < *npts ; i++)
  {
    printf("%d", *(x+i));
  }
  
}
 
void delete_(int **x, int *npts, int pos, int num_deleted)
{ 
}
 
void printArray(const int *array, int size)
{
  /* counter */
 /*    int i;
  
  for(i=0;i<size;i++)
  {
    printf("%d",*array  );
  } */
}

int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}


这篇关于目前我正在尝试将元素插入到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 05:21