Click here to Skip to main content
15,920,956 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
below is my current code, where did I went wrong ?



C++
#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  );
  } */
}
Posted
Comments
PIEBALDconsult 29-Nov-15 0:00am    
You went wrong when you tried to insert into an array with C.
You probably want a linked list instead. With a little work I'm sure you can do it.

1 solution

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.

C++
// 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;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900