Cpp Language :
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int *insert(int arr[],int (&size),int value,int
position){
int *numbers = (int
*)malloc((size-1)*sizeof(int));
int i,j=0;
for(i=0;i<size;i++){
*(numbers+i) = arr[i];
}
for(i=size;i>=position;i--){
*(numbers+i) =
*(numbers+(i-1));
}
numbers[position] = value;
size++;
return numbers;
}
int *remove(int arr[],int (&size),int position){
int *numbers = (int
*)malloc((size-1)*sizeof(int));
int i,j=0;
for(i=0;i<size;i++){
if(i != position){
*(numbers+j) =
arr[i];
j++;
}
}
delete[] arr;
size--;
return numbers;
}
int Count(int arr[],int size,int target){
int count = 0,i=0;
for(i=0;i<size;i++){
if(target == arr[i]){
count++;
}
}
return count;
}
void print(int arr[],int size){
int i=0;
cout << "Elements in array: " <<
endl;
for(i=0;i<size;i++){
cout << arr[i] << "
";
}
cout << "\n" << endl;
}
int main(int argc,char *argv[]){
//checking sufficient command lines arguments entered
or not
if (argc == 3){
int length = atoi(argv[2]);
int *numbers = new
int[length];
int x,i=0;
//object to read file data
ifstream read;
//opening the file
read.open(argv[1]);
if(!read){
//if file not
found this messaege prints
cout <<
"entered file is not exists"<< endl;
return 0;
}
while(read >> x){
//storing the
numbers from the file to the array
numbers[i++] =
x;
}
//menu
int
choice,index,count=0,value=0,position=0;
//repeat until user exists
do{
cout <<
"Selection" << endl;
cout <<
"1) Insert" << endl;
cout <<
"2) Remove" << endl;
cout <<
"3) Count" << endl;
cout <<
"4) print" << endl;
cout <<
"5) Exit" << endl;
cout <<
"Choice: ";
//taking the
choice from user
cin >>
choice;
cout <<
endl;
switch(choice){
//inserting the value in the array
case 1:
cout << "Enter a
element to insert: ";
cin >> value;
cout << "Enter a
position to insert: ";
cin >> position;
if(position <
length){
numbers =
insert(numbers,length,value,position);
cout
<< "Inserted\n" << endl;
}else{
cout
<< "Invalid position";
}
break;
//deleting a nubmer entered
case 2:
cout << "Enter a
position to delete: ";
cin >> position;
if(position <
length){
numbers =
remove(numbers,length,position);
}else{
cout
<< "Invalid position";
}
break;
//counting no.of times entered element is
occured in array
case 3:
cout << "Enter a
element to count: ";
cin >> x;
count =
Count(numbers,length,x);
if(count != 0){
cout
<< x << " finds " << count << " times\n"
<< endl;
}else{
cout
<< x << " is not found\n" << endl;
}
break;
//displaying the array
case 4:
print(numbers,length);
break;
//if user wants to exit this message
prints
case 5:
cout << "Thank You for
using our application\n" << endl;
break;
default:
cout << "Invalid
option.Try again\n" << endl;
break;
}
}while(choice != 5);
}else{
cout << "Enter command line
arguments : filename and no.of elements to read" <<
endl;
}
}
nums.txt :
Output :
please up vote.If any doubt comment below.I will answer.Please up vote.Thank You.
using c++ 13 do pretty much whatever they want Exercise: Array Resizing You will create an...
IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...
(C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....
Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...
Hi! I am struggling trying to figure out how to do this. For this lab, you must have two implementations of calculating the Fibonacci sequence. You are required to calculate it utilizing an array and then with a pointer. You must use the array provided and you must malloc space for your pointer. Your algorithms must be in-place, meaning they do not use a temporary variable to calculate the next sequence number. Lastly, you must insert a comment above each...
IN C# The fun for today is to build your own circular array to support a queue. The challenge here is in adjusting your queueFront and queueRear markers (they are just integer array indices), and dealing with what happens when they wrap around the First/last element of the array. The tricky part here is worrying about the edge cases. There are only two methods you need to fix up addBack – this should correctly add an item at the rear...
In C++, develop a class that supports array rotation. Rotating an array is an operation where you shift all elements of the array some number of positions left or right, and elements that are shifted off of the left or right end of the array "wrap around" to the right or left end, respectively. For example, if we rotate the array [1, 2, 3, 4, 5] to the right by 1, we get the array [5, 1, 2, 3, 4]....
Add a method called median() to the ArrayIns class in the insertSort.java program (Listing 3.3). This method should return the median value in the array. (Recall that in a group of numbers half are larger than the median and half are smaller.) Do it the easy way. LISTING 3.3 The insertSort.java Program // insertSort.java // demonstrates insertion sort // to run this program: C>java InsertSortApp //-------------------------------------------------------------- class ArrayIns { private long[] a; // ref to array a private int nElems;...
PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores integers and and implements a minimum priority queue. (Your program can be "hard coded" for integers - it does not need to use templates, generics, or polymorphism.) Your data structure must always store its internal data as a heap. Your toString function should return a string with the heap values as a comma separated list, also including the size of the heap as well....
Variable Size Array with Classes, Testing. Study Code and Object Definition Windows of Microsoft Visual Studio described here. As you work on the below project, demonstrate to the instructor the usage of this feature. Create a project titled Lab11_VarArrayTest. Implement the dynamically expanding and contracting array of doubles described in the previous lab as a class. You should use this class definition. The class attributes are a pointer to the dynamically allocated array dAarray and the array size size This...