目前我正在尝试将元素插入到数组中 [英] Currently I am trying to insert an element into an array

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

问题描述

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







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  );
  } */
}

推荐答案

我已经编译了您的源代码并获得了解决方案。

你的代码中有2个错误。

1.不允许使用名为'delete'的函数,因为'delete'已经是C ++的关键字。

2.指针赋值不正确。

'插入'功能,

for(i = * npts-1; i> pos; i-- )

{

//(x + i)=(x + i-1); //原始代码,错误

*(x + i)= *(x + i-1);

}



我会给你修改的代码。

我希望我的话能帮到你。



I have compiled your source and got solutions.
There were 2 errors in your code.
1. Function named 'delete' is not allowed, because 'delete' is C++'s keyword already.
2. Value assign by pointer is not correct.
in 'insert' function,
for(i = *npts-1 ; i > pos ; i--)
{
// (x+i) = (x+i-1); // . original code, error
*(x+i) = *(x+i-1);
}

I will give you code I modified.
I wish my words would help you.

// 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;
}


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆