Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone wirte code for me? C++ 2013 visual studio Pointers, Dynamic Memory A

ID: 3687102 • Letter: C

Question

Can someone wirte code for me? C++ 2013 visual studio

Pointers, Dynamic Memory Allocation

Test Array. Study Observing Variables feature of MSVS described here. As you work on the below project, demonstrate to the instructor displaying local variable values with "Autos" and "Locals" tab. Create a project titled Lab10_TestArray. In the project, you should use the functions prototyped in this header file. You should place the functions you implement in a file named varArray.cpp. The functions to be implemented are as follows:

output() takes the pointer to the array and its size and prints the arrays' contents

check() takes the pointer to the array and a number and checks if the number is in the array. If the number is in the array, returns the index of the element of the array holding the number. Otherwise, returns -1.

addNumber() takes a pointer to the array and adds a number to the array if the number is not already there. The pointer to the array is passed by reference. Note that the array cannot be enlarged to accommodate the number. Instead, this function needs to allocate a new array whose size is one more than the size of the old array, copy the contents of the old array to the new one, include the new number, deallocate the old array and then assign the address of the new array back to the original pointer.

Note that since the pointer is passed by reference, the call to this function results in effectively "enlarging" the array pointed to by the pointer.

removeNumber() takes a pointer to the array, a number, and removes the number if it is present in the array. If the number is removed, the array shrinks.

Note that the function should not change an array in any way if the number is not present in it. If the number is present, then the function should allocate a new array of smaller size and then copy all the numbers to it, except the one that needs to be erased. After copying is done, deallocate the old array and update the array pointer to point to the new array.

Copying and removing one element may be done in a single for-loop. Here is a pseudocode for the solution:

Your code should work with this test file

Hint: int *p=new int[0]; works correctly. This allows to avoid treating the array of size zero in the above functions specially.

Variable Size Array. Create a project titled Lab10_VarArray. Write a program that allows the user to add or remove an integer number to a collection of integers and then prints all of them. A user may enter the number twice in which case, the repeated number should not be entered.

Here is an example program dialog:

The size of the user input can be arbitrarily large. For this program, to accommodate user input, you need to implement an integer array whose size varies as necessary. The numbers in the array should not be sorted.

Make sure your programs adhere to proper programming style. Submit your projects to the subversion repository. Do not forget to verify your submission on the web.

this header file

this test file

Explanation / Answer

Solution for Question:

This below functions are updated with the definations present in given problem defination.

See the below definations

#ifndef VARARRAY_H
#define VARARRAY_H

// prints the values in "arrayPtr" of "size"
void output(int *arrayPtr, int size)
{
   int i;
   for(i=0; i <size; i++)
       cout<<arrayPtr[i]<<" ";      
}

// returns the index of the element in "arrayPtr" of "size"
// that corresponds to the element holding "number"
// if number is not in the array, returns -1
int check(int *arrayPtr, int number, int size)
{
   int index, temp;
   for(int i =0; i<size; i++)
   {
       if(array[i]==number)
       {
           index = i;
           temp = 1;
       }
       else
       {
           temp = 0;
       }
   }
       if(temp == 1)
           return index;
       else  
           return -1;
      
}  


// adds "number" to the array pointed to by "arrayPtr" of "size".
// if the number is not already there, if "number" is there - no action
// Note, the size of the array is thus increased.
void addNumber(int *& arrayPtr, int number, int &size)
{
   int val = check(arrayPtr, number, size)
   for(int i=0; i<size; i++)
   {
       if(number == arrayPtr[i])
       {  
           cout<<"Element is preset in the array"<<end;
           flag = 1;
       }
   }
       if(flag == 0)
       {
           size = size + 1;
           arrayPtr[size] = number;
       }
}

// removes a "number" from the "arrayPtr" of "size".
// if "number" is not there -- no action
// note, "size" changes
void removeNumber(int *& arrayPtr, int number, int &size)
{
   int val= check(arrayPtr, number, size);
   int flag = 0;
   for(int i=0; i<size; i++)
   {
       if(number == arrayPtr[i])
       {  
           cout<<"Element is present in the array"<<end;
           flag = 1;
           arrayPtr[i] = -1;
       }
   }
   if(flag == 0)
   {
       cout<<" Element is not present :"<<endl;      
   }
}

#endif // VARARRAY_H

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote