Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inserting element at even indices of an array #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions IpAtEvenIndex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Name : Vishal Rathi
Roll Number : 303302219121
Date : 17-Aug-2020
Purpose : Code for algorithm of inserting items in even indices of an array
(From left to right - Assumed)
*/

#include <iostream>

void arrayTraverse(int, int, int[],int);
void evenInsertion(int, int, int[], int, int);

int main()
{
int n,lb,ub,max,num;

std::cout << "Enter number of elements in array:" ;
std::cin >> n;

int arr[n];
max = n-1;

std::cout << "enter elements of array:\n" ;
for(int i=0;i<n;i++)
{
std::cin >> arr[i];
}

std::cout << "\nEnter the value of lower bound: " ;
std::cin >> lb;

std::cout << "\nEnter the value of upper bound: " ;
std::cin >> ub;

std::cout << "\nEnter the value to be added in every even indices: " ;
std::cin >> num;
//calling the evenInput Function
evenInsertion(lb,ub,arr,num,max);

arrayTraverse(lb,ub,arr,max);

return 0;
}

void evenInsertion(int lb,int ub,int list[],int num,int max)
{
int i,j,item;
if(lb>ub)
{
std::cout << "\n Error" ;
return;
}

if(ub>max)
{
std::cout << "\n Overflow Condition";
return;
}

if(lb%2==0)
j = lb;
else
j = lb + 1;
while(j<=ub)
{
list[j] = list[j] + num;
j = j+2;
}

}

void arrayTraverse(int lb, int ub,int list[],int max)
{
for(int i=lb;i<=ub;i++)
{
std::cout << list[i] << "\t" ;
}
}
Binary file added IpAtEvenIndex.o
Binary file not shown.
13 changes: 7 additions & 6 deletions prog2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ void travarray(int[],int,int,int);
int main(){
int ip[10],lb,ub,pos,item;
cout<<"Enter Elements of array";
for(int i=0;i<=10;i++){
for(int i=0;i<10;i++){
cin>>ip[i];
}
cout<<"Enter the value of lower bound";cin>>lb;
cout<<"Enter the value of upper bound";cin>>ub;
cout<<"Enter the pos";cin>>pos;
cout<<"Enter the value of item";cin>>item;
//function call
arrayinsertion(lb,ub,ip,pos,item,3);
arrayinsertion(lb,ub,ip,pos,item,9);
return 0;
}

Expand All @@ -37,7 +37,7 @@ void travarray(int ip[],int lb,int ub, int max){
}

void arrayinsertion(int lb,int ub,int ip[],int pos, int item,int max){
cout<<"Input Array";
cout<<"Input Array :";
travarray(ip,lb,ub,10);
if(lb>ub){
cout<<"error";
Expand All @@ -50,12 +50,13 @@ void arrayinsertion(int lb,int ub,int ip[],int pos, int item,int max){
for(int i=ub; ;){
ip[i]=ip[i-1];
i=i-1;
if(i==pos-1){
if(i==pos+lb-1){
break;
}
}
ip[pos]=item;
ip[pos+lb-1]=item;
cout<<"\n";
travarray(ip,lb,ub,10);
cout<<"Output Array:";
travarray(ip,lb,ub,10);
}