Question

Implement a funcion addNumber() in C that takes a pointer to the array and adds a...

Implement a funcion addNumber() in C that takes a pointer to the array and adds a number to the array if the number is not already there.

function protoype: void addNumber(int **arrayPtr, int number ,int *size);

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

function prototype: void removeNumber(int **arrayPtr, int number, int *size);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

This definition is not correct to for function that pass array to function as pointer

void addNumber(int **arrayPtr, int number ,int *size); - wrong

correct is

void addNumber(int *arrayPtr, int number ,int *size)


#include <stdio.h>
#include <stdbool.h>

void addNumber(int *arrayPtr, int number ,int *size){
int n=size;
int ar[n+1];
bool isPresent=false; //flag to see if element present
for(int i=0;i<n;i++){
if(arrayPtr[i]!=number){ //if element not present
isPresent=true; //set flag true
break;
}
}
n=n+1; //increse the size of array
for(int i=0;i<n-1;i++){
ar[i]=arrayPtr[i]; //copy element to temp array
}
ar[n-1]=number; //add element at last
arrayPtr=ar; //copy temp array to original array
for(int i=0;i<n;i++){
printf("%d,",arrayPtr[i]); //print array
}
printf("\n");
}
void removeNumber(int *arrayPtr, int number ,int *size){
int n=size;
int ind=-1;

for(int i=0;i<n;i++){
if(arrayPtr[i]==number){ //if number present
ind=i; //store index
break;
}
}

for(int i=ind;i<n-1;i++){ //from that index , shift all element backword by one
arrayPtr[i]=arrayPtr[i+1];
}

for(int i=0;i<n-1;i++){ //test record
printf("%d,",arrayPtr[i]);
}
}
//driver function to test
int main()
{
int ar[]={1,2,3,4,5};
addNumber(ar,9,5);
removeNumber(ar,1,5);

return 0;
}

output

1,2,3,4,5,9,
2,3,4,5,

Add a comment
Know the answer?
Add Answer to:
Implement a funcion addNumber() in C that takes a pointer to the array and adds a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • -Create a function output() in C that takes the pointer to the array and its size...

    -Create a function output() in C that takes the pointer to the array and its size and prints the arrays' contests. (malloc format) (function prototype : void output(int *arrayPtr, int size); -Create a function check() in C that 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.

  • Variable Size Array with Classes, Testing. Study Code and Object Definition Windows of Microsoft ...

    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...

  • must be done in C You will implement a function that matches one of the prototypes...

    must be done in C You will implement a function that matches one of the prototypes below. int maxArray(int size, int* arr); int maxArray(int size, int arr]); The first parameter is the number of elements in the array. The second parameter is an address to the beginning of an int array. In the body of the function, use a looping structure to identify and return the maximum (largest number) of the array. NOTE: The maximum of the same number, say...

  • In C An array ints of integers has already been declared and initialized. A function printint...

    In C An array ints of integers has already been declared and initialized. A function printint has been defined with the following prototype: void printint(int *); Write code that will call printint with a pointer to the second value in the array ints. Note: you will not need to declare an int variable.

  • Write a C++ function that takes a pointer to an array of integers as a parameter...

    Write a C++ function that takes a pointer to an array of integers as a parameter and return the number of prime integers in the array. Write main() program to test the function.

  • solve it in c+* Part II: Dynamic Arrays and Pointer Arithmetic Q5: Implement a subset function...

    solve it in c+* Part II: Dynamic Arrays and Pointer Arithmetic Q5: Implement a subset function for a dynamic array which returns a new dynamic array that is a subset of the original. (15pt) input parameters: array - (the array and any related parameters) start - index of the first element end - index of the last element This function returns a new dynamic array containing the elements from the start thru the end indices of the original array. All...

  • Write a C++ console application that adds the corresponding elements of two integer arrays of size...

    Write a C++ console application that adds the corresponding elements of two integer arrays of size five. Prompt the user for the ten values and store the first five in the first array and the second five in the second array. Then create and call function matrixAdd that adds the two arrays and prints the five sums. Here is the function prototype: void matrixAdd(int firstArr[], int secondArr[]); [your program code here]*

  • C++ problem to use dynamic memory allocation (of arrays) and pointer manipulation in order to implement...

    C++ problem to use dynamic memory allocation (of arrays) and pointer manipulation in order to implement the Inner and Outer classes (Circular Buffer of circular buffers using Queues). No need of any classes from the Standard Template Library (STL), not even vector. Add the member functions of those classes by following the codes of InnerCB.h and CBofCB.h below: // file: InnerCB.h // Header file for Inner Circular Buffer. // See project description for details. // #ifndef _INNERCB_H_ #define _INNERCB_H_ class...

  • Create a function that takes two parameters : pointer to the array and the size of...

    Create a function that takes two parameters : pointer to the array and the size of the array. Print out the array's values and their memory addresses inside the function. Then create another function that takes two parameters : pointer to the array. Inside the function, multiply each of the array's values by two ad print the modified values out in the main function. With C programming language

  • Assuming you have the following ordered-array class definition: class ordered_array { public: ordered_array(int c) { data...

    Assuming you have the following ordered-array class definition: class ordered_array { public: ordered_array(int c) { data = new int[c]; cp = c; sz = 0; } ... private: int sz, cp; // Current size, total capacity int* data = nullptr; }; Suppose that ordered_arrays can contain duplicates. Implement a method remove_duplicates(e)which takes an element e and removes it and any duplicates of it. (Note that e is the actual value to be removed, not its index in the array.) If...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT