I'm new to c++ and I'm writing an array manipulator program. How would you write a function that removes an element from an array? What parameters would you pass to it? The function cannot have any cout or cin statements, they must be in main instead. How would you call the function? I wrote a function for adding an element to an array, and it looks like this:
int insertValue(int arr[], int value, int pos, int size)
if (size == 10)
cout << "Array full" << endl;
else
{
int i;
for (i = size - 1; i >= pos; --i) {
arr[i + 1] = arr[i];
}
arr[pos] = value;
++size;
}
cout << endl;
return size;
This function works when called. Would removing an element from an array and moving everything to the left follow the same layout? This is what I have so far for that function:
int removeValue(int arr[], int value, int pos, int size)
for (int i = pos; i < size; ++i) {
array[i] = array[i + 1];
}
arr[pos] = value;
return size;
I don't think I have the right idea for this code, which is why I am confused. Can someone explain the idea behind this function and how it would be written correctly?
Thanks for your help!
Use std::vector or std::array instead of c-array([]).
To remove elements from container, you should use std::vector + std::remove + std::vector::erase
This line is going to overflow the array, with all the attended horrors, as you are indexing one past the end.
array[i] = array[i + 1];
But other than that as long as you keep track of how many elements your array has it should be OK
Related
Trying to understand the insertion sort algorithm..
My algorithm looks like this currently:
void insertionSort(int *array, int N) {
int value;
int hole;
int *array2;
for (int i = 1; i < N - 1; i++) {
value = array[i]; //next item to be inserted in array 2
hole = i;
while (hole > 0 && array[hole - 1] > value) {
array[hole] = array[hole - 1];
hole = hole - 1;
}
array[hole] = value;
}
}
My algorithm works for sorting arrays, however I now need to change it so that I build up a new sorted array (array2) one element at a time, rather than just working with the original array.
Is there a simple way to implement this given my completed algorithm?
Thanks.
You can use the following method:
int *array2 = calloc(N, sizeof(int));
for(var index = 0; index < N; index++)
{
array2[index] = array[index];
}
and after that use array2 instead of array
then just change the prototype of your function to int *insertionSort
All remaining is to return array2 at the end of task
But be aware of memory leak: https://en.wikipedia.org/wiki/Memory_leak
I am trying to make a function, which deletes one line from a structure array, I give function the index of line I want to delete and the structure array. E.g. we have structure array :
Structure
{
string First;
string Second;
string Third;
string Fourth;
string Fifth;
}
Structure array :
Structure A[100];
int n;
So there are five string type elements in this structure array :
A[i].First A[i].Second A[i].Fourth A[i].Fifth // i is the index
Our function is like this :
void Delete(Structure A[], int index, int & n)
{
Structure t;
for (int i = index; i < n-1; i++)
{
t = A[i];
A[i] = A[i + 1];
A[i + 1] = t;
n--;
}
}
So I give function index and I want the function to delete all elements of my structure array with that index ( so how can I take like the whole "line" of those elements, instead of deleting them one by one ?)
A[index].First A[index].Second A[index].Third A[index].Fourth A[index].Fifth
The function however doesn't work. Can you give me some tips/advises/suggestions, please ? Thanks in advance.
At first level, your question is basically: how to remove a line from an array whose used size is stored in an external variable (passed here as n)
The signature of your function is correct, the implementation is not. It should be:
void Delete(Structure A[], int index, int & n)
{
// eventually control index >= 0 and index < n...
n -= 1;
for (int i = index; i < n; i++)
{
A[i] = A[i + 1];
}
}
If you have a recent version of C++ that support move semantics, you could speed up the operation by moving the strings instead of copying them:
n -= 1;
for (int i = index; i < n; i++)
{
A[i] = std::move(A[i + 1]);
}
I am trying to generate some lotto numbers and return the array that contain these numbers but I cant go any further; Help please
void getLotto(int rad[7]) {
int numbers[7];
for (int i = 0; i < 7; i++) {
numbers[i] = rand() % 35 + 1;
}
for (int j = 0; j < 7; j++) {
int n = rand() % 35 + 1;
if (n == numbers[j]) {
numbers[j] = rand() % 35 + 1;
return;
}
}
}
Arrays can't be returned by functions. A common thing to do is to dynamically allocate the array and return a pointer to its first element. This will work in your case but will generate a requirement for the caller to manage the memory (delete[] the new[]'ed memory). That's why C++ provides us with standard array classes: Use and return a std::vector. If you have C++11 support, return std::array.
Following may help, using Fisher–Yates_shuffle:
// Fisher–Yates_shuffle
// http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
std::vector<int> FisherYatesShuffle(std::size_t size, std::size_t max_size, std::mt19937& gen)
{
assert(size < max_size);
std::vector<int> res(size);
for(std::size_t i = 0; i != max_size; ++i) {
std::uniform_int_distribution<> dis(0, i);
std::size_t j = dis(gen);
if (j < res.size()) {
if (i != j) {
res[i] = res[j];
}
res[j] = 1 + i;
}
}
return res;
}
Live example
std::vector and std::array are better than regular arrays, but if you want to use regular arrays you can modify your function as follows:
// Arguments: renamed the array, added N (# of array elements)
void getLotto(int numbers[], size_t N) {
//int numbers[7]; // commented out local variable
for (int i = 0; i < N; i++) {
numbers[i] = rand() % 35 + 1;
}
for (int j = 0; j < N; j++) {
int n = rand() % 35 + 1;
if (n == numbers[j]) {
numbers[j] = rand() % 35 + 1;
return;
}
}
}
The brackets in int numbers[] indicates that the argument is an array, and what is actually passed is a pointer to the first element of the array. Modifying numbers in getLotto() modifies the array passed to the function.
The second argument is of type size_t because it is the platform-dependent alias for the unsigned integral type used by your system to represent the size of objects (like arrays).
This isn't as safe in that the function has to trust that numbers actually has N elements, but this is how you have a function modify a regular array instead of a container like std::vector.
You would call the function like this:
size_t N;
int numbers[N];
getLotto(numbers, N);
C++ does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index.
If you want to return a single-dimension array from a function, you would have to declare a function returning a pointer as in the following example:
int * myFunction()
{
.
.
.
}
Second point to remember is that C++ does not advocate to return the address of a local variable to outside of the function so you would have to define the local variable as static variable.
Now, consider the following function, which will generate 10 random numbers and return them using an array and call this function as follows:
#include <iostream>
#include <ctime>
using namespace std;
// function to generate and retrun random numbers.
int * getRandom( )
{
static int r[10];
// set the seed
srand( (unsigned)time( NULL ) );
for (int i = 0; i < 10; ++i)
{
r[i] = rand();
cout << r[i] << endl;
}
return r;
}
// main function to call above defined function.
int main ()
{
// a pointer to an int.
int *p;
p = getRandom();
for ( int i = 0; i < 10; i++ )
{
cout << "*(p + " << i << ") : ";
cout << *(p + i) << endl;
}
return 0;
}
When the above code is compiled together and executed, it produces result something as follows
624723190
1468735695
807113585
976495677
613357504
1377296355
1530315259
1778906708
1820354158
667126415
*(p + 0) : 624723190
*(p + 1) : 1468735695
*(p + 2) : 807113585
*(p + 3) : 976495677
*(p + 4) : 613357504
*(p + 5) : 1377296355
*(p + 6) : 1530315259
*(p + 7) : 1778906708
*(p + 8) : 1820354158
*(p + 9) : 667126415
There are two main ways of accomplishing this.
note: I'm not sure what your second for loop is doing. I guess the intention was to ensure that the numbers are all unique? You might want to take a look at it as that is not what it is doing.
For the purposes of this question, I've cut it down to just generating the random numbers to populate the array.
The first is to take your code and fix it to put the generated numbers into the array that was passed in:
#include <iostream>
void getLotto(int numbers[7]) {
for (int i = 0; i < 7; i++)
{numbers[i] = rand() % 35 + 1;}
return;
}
int main()
{
srand(time(0));
int lotto_numbers[7];
getLotto(lotto_numbers);
for (int i = 0; i < 7; i++)
{std::cout<<lotto_numbers[i]<<std::endl;}
}
numbers isn't actually passed in as an int[] but instead as an int* pointing to the array. This means that any changes you make to it in the function are changed in the original data.
Bear in mind that you need to keep track of your array bounds though, as the array could be defined as
int lotto_numbers[6]
which means that
numbers[7]
would be out of bounds.
The second method is to create the array on the heap. This means that you don't need to pass in an array but you can instantiate it in the function
I'm not actually going to provide the code for this here. Mainly because for something simple like this, the memory management is more trouble than it is worth. (you need to remember to call delete[] for everything created on the heap etc).
Instead, lets use something with memory management built in:
#include <iostream>
#include <vector>
std::vector<int> getLotto() {
std::vector<int> numbers;
numbers.reserve(7);
for (int i = 0; i < 7; i++) {
//numbers[i] = rand() % 35 + 1;
//would work, but is unsafe as you could potentially reference somthing out of range
//this is safer:
numbers.push_back(rand() % 35 + 1);
}
return numbers;
}
int main()
{
srand(time(0));
std::vector<int> lotto_numbers = getLotto();
for (auto i = lotto_numbers.begin(); i != lotto_numbers.end(); i++)
{
std::cout<<*i<<std::endl;
}
}
The vector handles the memory management for you. The vector can be returned, and the returned vector will still point at the allocated memory on the heap we have just populated. We don't need to free it as this will be done automatically when the vector goes out of scope.
I'm trying to write a class that contains an array of numbers that you can sort with a function myArray.quicksort().
When creating a new Objekt of Array I pass the constructor the length and then fill it:
public: int Items[];
/*-- Constructor --*/
Array(int n){
length = n;
for(int i=0; i<length; i++){
int zahl;
Items[length];
cout << "Item" << i << ": ";
cin >> number;
Items[i] = number;
}
...
Right after creating the Array this function to print it out works just fine:
void Array::show(){
for(int i=0; i<this->length; i++){
cout << this->Items[i] << " ";
}
}
However after I try to sort it it prints out nonsense:
void Array::quickSort(int left, int right){
int i=left, j=right;
int tmp;
int pivot = this->Items[(left + right) / 2];
while(i <= j){
while (this->Items[i] > pivot)
i++;
while (this->Items[j] < pivot)
j--;
if (i <= j) {
tmp = this->Items[i];
this->Items[i] = this->Items[j];
this->Items[j] = tmp;
i++;
j--;
}
};
if (left < j)
quickSort(left, j);
if (i < right)
quickSort(i, right);
}
I'm sure I totally mix up something with the array pointers..
But I can't seem to find a solution for it.
Where is the big flaw here?
Standard C++ does not have flexible array members (like C99 has).
You don't need your Array class, use std::vector<int> instead (or perhaps std::array with C++11 if the array length is a compile time constant)
If you want to declare your Array class containing Items, follow the hints in the comment of Joachim Pileborg, learn about the rule of three (in C++11, it became the rule of five), so declare:
int *Items;
in your class, then initialize it with
Items = new int[n];
in its constructor[s], and destroy it with
delete [] Items;
in your destructor.
So I'm trying to use a sorting function (similar to bubble) and pass into it an object. If that object is bigger (alphabetically) then switch then return true and switch that with the before it. I keep getting an error though inside the if statement inside mySort() which says "no match for operator[] in arr[j]" but from my understanding I'm passing an object array right? Why is this happening and how can I solve it?
Here's the driver
#include <iostream>
#include <fstream>
#include <string>
#include "phoneEntry.h"
using namespace std;
void mySort(PhoneEntry &arr, int size)
{
bool inOrder = false;
string temp;
for (int i = size - 1; i > 0 && !inOrder; i--)
{
inOrder = true;
for (int j = 0; j < i; j++)
{
if(arr.alphaGreater(arr[j]))
{
inOrder = false;
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
};
int main()
{
const int MAXNUM = 500;
PhoneEntry entry[MAXNUM];
ifstream filezilla;
filezilla.open("phone.txt");
int count = 0;
if(filezilla)
{
while(count < MAXNUM && entry[count].readEntry(filezilla))
{
count++;
mySort(entry[count], count);
}
for(int i = 0; i < count; i++)
{
entry[i].writeEntry(cout) << endl;
}
}
else
{
cout << "404" << endl;
}
return 0;
}
Phone Entry Header
Phone Number Header
Sorting Text (http://pastebin.com/HE8Rsmbg)
arr should be an array, not a reference, like this PhoneEntry arr[]
You should be passing an entire array to the sort, not a single element, like this: mySort(entry, count);
Other than this, your code appears OK.
I should add that this is not a C++ - ish solution: the preferred way of managing arrays in C++ is through using the std::vector<T> container from the standard library. The nice thing about vectors is that you do not need to pass their size "on the side".
You can use pointer notation - mySort(PhoneEntry * arr, int size) or array notation - mySort(PhoneEntry arr[], int size).
If you want to pass the whole array when you call the function, just do mySort(entry, count).
arr is not an array in your method.
change your method signature to
void mySort(PhoneEntry *arr, int size)
and call your method with
mySort(entry[count], count);
from my understanding I'm passing an object array right?
No, you are not passing an object array. You are passing a reference (indicated by the & in the function header) to the PhoneEntry element that is at the count-th position in the entry array. You probably meant PhoneEntry* arr in the header of mySort -- that would require a pointer to a PhoneEntry instance, and since the name of an array can be interpreted as a pointer to the first element of that array, you could simply pass entry as the first argument to mySort.
Substitute this:
void mySort(PhoneEntry * arr, int size)
Instead of this:
// Wrong
mySort(entry[count], count);
... do one of these (as appropriate):
// Always passes the start of the array, "entry[0]":
mySort(entry, count);
// Passes a pointer to a particular entry, onwards:
mySort(&entry[count], count);