RSS

Category Archives: Deletion in 1D Arrays

PROGRAM TO DELETE A NAME FROM A SET OF NAMES

// THIS PROGRAM CREATES A FUNCTION TO DELETE A NAME FROM A LIST OF NAMES

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

using namespace std;

int deletename(char *, char [][30],int);

int main()
{
int size;
cout<<“\nEnter the number of names you want to enter \n”;
cin>>size;
char name[30][30];
cin.get();

cout<<“\nInput the names\n”;

for(int i =0; i<size; i++)
{
gets(name[i]);
}

cout<<“\nEnter the name to be deleted \n”;

char item[30];
gets(item);

int res = deletename(item, name, size);

if(res == 0)
cout<<“\nName could not be found\n”;
else
{
size-= res;
cout<<“\nThe array after deletion is \n”;

for(int i =0; i< size; i++)
{
puts(name[i]);
}
}

return 0;
}

int deletename(char name[], char arr[][30], int size)
{
int ctr = 0;

for(int i = 0; i<size; i++)
{
if(strcmp(name, arr[i]) == 0)
{
ctr++;
for(int j = i; j<size-1; j++)
{
strcpy(arr[j],arr[j+1]);
}
i–;
size-=1;
}
}

if(ctr > 0)
return ctr;
else
return -1;
}

 
Leave a comment

Posted by on November 21, 2015 in Deletion in 1D Arrays

 

PROGRAM TO PERFORM DELETION IN A STRING BY READING THE POSITION AND THE NUMBER OF ELEMENTS TO BE DELETED

/*
THIS PROGRAM READS POSITION AND THE NUMBER OF ELEMENTS TO BE DELETED AND
DELETES THEM IN A STRING
*/

#include <iostream.h>
#include <conio.h>
#include <stdio.h>

void main()
{
clrscr();
char arr[100];
int num,pos;
cout<<“\nEnter the string\n”;
gets(arr);
cout<<“\nEnter the element number from which you want deletion to be done\n”;
cin>>pos;
pos = pos-1;
cout<<“\nEnter the number of elements to be deleted\n”;
cin>>num;

for(int i =0; arr[i] != ‘\0’; i++)
{
if(i>=pos)
{
arr[i] = arr[i+num];
}
}
cout<<“\nThe new string is \n”;
puts(arr);
getch();
}

 

Program to delete the element at the position specified by user

/* THIS PROGRAM READS POSITION FROM USER AND DELETES THE ELEMENT AT THAT
POSITION IN AN ARRAY */

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int arr[100],size,pos = -1;
cout<<“\nEnter the size of the array \n”;
cin>>size;
cout<<“\nInput the array \n”;
for(int i =0; i<size; i++)
cin>>arr[i];
cout<<“\nEnter the element number to be deleted\n”;
cin>>pos;
pos = pos-1;
for(i =0; i<size; i++)
{
if(i>= pos)
{
arr[i] = arr[i+1];
}
}
size -= 1;
cout<<“\nThe new array is \n”;
for(i =0; i<size; i++)
cout<<arr[i]<<‘ ‘;
getch();
}

 
Leave a comment

Posted by on October 4, 2015 in Deletion in 1D Arrays

 

PROGRAM TO DELETE FIRST ELEMENT FROM AN ARRAY

// THIS PROGRAM DELETES FIRST ELEMENT FROM AN ARRAY

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int arr[100],size;
cout<<“Enter the size of the array \n”;
cin>>size;
cout<<“\nInput the array \n”;
for(int i =0; i<size; i++)
cin>>arr[i];
cout<<“\nDeleting the first element of the array \n”;
for(i =0; i<size; i++)
arr[i] = arr[i+1];
size -= 1;
cout<<“\nThe new array is \n”;
for(i =0; i<size; i++)
cout<<arr[i]<<‘ ‘;
getch();
}

 
Leave a comment

Posted by on October 4, 2015 in Deletion in 1D Arrays