Hello can someone help me figure this out for my revision using C Programming. thanks!
Given the following function prototype: void trimPositive(int* *arr, int *size) ;
write the corresponding function definition such that: given any (dynamically allocated) array of ints and its size via the arr and size parameters, the result of your function is that:
a) the int* array argument points to a new (dynamically allocated) array that is completely full, and whose values are exactly the positive values of the array that it pointed to before the call.
b) the int size argument has been set to the number of elements in this newly created array.
Note: Your function must be free of memory leaks.
// C program to create and test function trimPositive
#include <stdio.h>
#include <stdlib.h>
// function that will remove all negative elements from dynamic
array arr so that arr contains only positive values
// of the array that it pointed.
void trimPositive(int* *arr, int *size)
{
int i,j, curr_size = *(size); // set curr_size to
value in size
// loop over the array
for(i=0;i<curr_size;)
{
// if ith element is negative
if(*(arr[i]) < 0)
{
// shift the
contents of the array to left by 1 position
for(j=i;j<curr_size-1;j++)
*(arr[j]) = *(arr[j+1]);
curr_size--; //
decrement current size
}else // if positive, then go to
the next element
i++;
}
// create and allocate a temporary array to store
the updated array of updated size curr_size
int **temp =
(int**)(malloc)(sizeof(int*)*(curr_size));
// loop to copy the elements
for(i=0;i<curr_size;i++)
{
temp[i] =
(int*)(malloc)(sizeof(int));
*(temp[i]) = *(arr[i]);
}
// loop to free the memory held by arr
for(i=0;i<*(size);i++)
free(arr[i]);
free(arr);
// update the size
*(size) = curr_size;
// allocate memory for new arr
arr = (int**)(malloc)(sizeof(int*)*(*size));
// copy the elements to arr
for(i=0;i<*(size);i++)
{
arr[i] =
(int*)malloc(sizeof(int));
arr[i] = temp[i];
}
}
int main(void) {
// test the function
int size = 10, i;
// create an array fo size 10
int **arr = (int**)(malloc)(sizeof(int*)*(size));
// allocate memory for each element
for(i=0;i<size;i++)
arr[i] =
(int*)malloc(sizeof(int));
// set elements of arr
*(arr[0]) = 5;
*(arr[1]) = 10;
*(arr[2]) = -5;
*(arr[3]) = 12;
*(arr[4]) = -2;
*(arr[5]) = -7;
*(arr[6]) = 5;
*(arr[7]) = 6;
*(arr[8]) = 14;
*(arr[9]) = 5;
//display the original array
printf("\nOriginal array : \n");
for(i=0;i<size;i++)
{
printf("%d ",*(arr[i]));
}
printf("\nAfter Trim Positive Array:\n");
// call the function to remove the negative
values
trimPositive(arr, &size);
// display the array after function call
for(i=0;i<size;i++)
{
printf("%d ",*(arr[i]));
}
return 0;
}
//end of program
Output:

Hello can someone help me figure this out for my revision using C Programming. thanks! Given...
Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...
Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...
Homework Question Write a void function called transformArray that takes two parameters - a reference to a pointer to a dynamically allocated array of ints, and the size of that array. The pointer is passed by referencebecause you want to change the value of the pointer. The function should dynamically allocate an array that is twice as long, filled with the values from the original array followed by each of those values times 2. For example, if the array that was...
Programming Assignment 7 Implement a function named fibo that will get a 20 element initialized an array of zeros from the main function. It will set the array to the Fibonacci sequence. This sequence starts with 1 and 2 as the first 2 elements and each element thereafter is the sum of the previous two elements. (1, 2, 3, 5, 8, 13…). Add another function named findNum that will get the newly created array by fibo from the main function....
can someone help me fix this. The function that finds the minimum and maximum values of the array and outputs the value and index doesnt find the maximum value of the array. #include <iostream> #include <fstream> #include<iomanip> using namespace std; void readArray(ifstream& inF, int arr[], int&ArrSize); void writeArray(ofstream & outF, int arr[], int & ArrSize); void MaxAndMin(ofstream & outF, int arr[], int & ArrSize, int &high, int &low); int main() { int arr[100]; int i = 0; ...
Hey everyone, I need help making a function with this directions
with C++ Language.
Can you guys use code like printf and fscanf without iostream or
fstream because i havent study that yet.
Thanks.
Directions: Write a function declaration and definition for the char* function allocCat. This function should take in as a parameter a const Words pointer (Words is a defined struct) The function should allocate exactly enough memory for the concatenation of all of the strings in the...
(C++ programming) Need help with homework. Write a program that can be used to gather statistical data about the number of hours per week college students play video games. The program should perform the following steps: 1). Read data from the input file into a dynamically allocated array. The first number in the input file, n, represents the number of students that were surveyed. First read this number then use it to dynamically allocate an array of n integers. On...
D
and e only.
Can someone help with this. C++ only. Show work if possible.
Thanks
Name: 3, (30%) Consider the following class definition: class SomeClass public: SomeClass (): SomeClass (); void set1 (int val); void set2 (int val); void add (SomeClass m); int getl ) int get2 0 private: static int numobjs; int meml; int mem2 SomeClass x1, x2; (a) Draw a box around every mutator function in this class. (b) Circle every accessor function in this class. (c)...
Need helps on C++ attach. Thanks
Pointers with classes a) A user-defined class named Timer has a constructor that takes two integer parameters to initialize hour and minute data members. Write a single C++ statement to create an object of the Timer class using dynamic memory allocation and assign it to a pointer variable named timePt r. It should call the constructor with two parameters. Use values of 10 and 20 for hour and minute respectively. b) Write the definition...
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...