Having trouble with this c++ assignment, if you can provide comments for the dif
ID: 3825255 • Letter: H
Question
Having trouble with this c++ assignment, if you can provide comments for the different arrays it would benefit me a whole bunch. Emphasis on step C where the new array removes the smaller array and adds 4,2, and 8. Thank you very much!
a. Create a function called resize that can be used to increase the size of integer arrays dynamically. The function takes three parameters. The first parameter is the original array, the second parameter is the size of this array, and the third parameter is the size of the larger array to be created by this function. Make sure that you allocate memory from the heap inside this function. After allocating memory for the second array the function must copy the elements from the first array into the larger array. Finally, the function must return a pointer to the new array.
b. In main, allocate an array on the heap that is just large enough to store the integers 5, 7, 3, and 1.
c. Resize the array to store 10 integers by calling the resize function created in step a. Remove the old (smaller) array from the heap. Add the numbers 4, 2, and 8 to the end of the new array.
d. Write a sort function that sorts any integer array in increasing order.
e. Use the sort function to sort the array of numbers in c above. Display the sorted numbers.
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
void resize(int *a, int size1,int size2);
void sort(int a[], int size);
void print(int a[], int size);
int main()
{
int *intArray;
//allocate intArray to hold 4 integers 5 7 3 1
intArray = (int*)malloc(4 * sizeof(int));
//now assign each array element 5,7,3,1, I am assigning manually , u can ask user to enter input here
intArray[0] = 5;
intArray[1] = 7;
intArray[2] = 3;
intArray[3] = 1;
//print array after adding 4 elements
print(intArray, 4);
//now resize array and add 3 elements
resize(intArray, 4, 3);
intArray[4] = 4;
intArray[5] = 2;
intArray[6] = 8;
//now print array after resize and adding 3 elements
print(intArray,7);
//now sort the array and print
sort(intArray, 7);
printf("After sorting array: ");
print(intArray, 7);
}
void resize(int *a, int size1, int size2)
{
int *b;
int size,i;
b = (int*)malloc((size1 + size2)*sizeof(int));
//copy array elemets of a to b
for (i = 0; i < size1; i++)
b[i] = a[i];
a = b;
}
void sort(int a[], int size)
{
int i,j,tmp;
for (i = 0; i < size; i++)
{
for (j = 0; j < size-i-1; j++)
{
if (a[j] > a[j+1])
{
tmp = a[j];
a[j] = a[i];
a[i] = tmp;
}
}
}
}
void print(int a[], int size)
{
int i;
printf("Array elements are : ");
for (i = 0; i < size; i++)
{
printf("%d ", a[i]);
}
printf(" ");
}
----------------------------------------------
//output
Array elements are : 5 7 3 1
Array elements are : 5 7 3 1 4 2 8
After sorting array: Array elements are : 1 3 4 5 7 2 8
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.