Question
This code outputs garbage values and i'm not sure why. If someone could help fix it and make it look like the correct output, that
This code outputs garbage values and i'm not sure why. If someone could help fix it and make it look like the correct output, that would be greatly appreciated. (Will like helpful responses)
Main code for output: (don't edit this)
using namespace std;
void fill_array(int* src, int size){
for (int i = 0; i src[i] = i*10; } } bool pointer_array_functions_basic_test(bool debug = true) { int size = 7; int capacity = 10; //-- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- cout //-- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- int *a = allocate fill_array(a, size); print_array(a, size, capacity); //-- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- cout //-- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- int key; int *found; key = 30; found = search_entry(a, size, key); if (found){ cout } else{ cout } key = 35; found = search_entry(a, size, key); if (found){ cout } else{ cout } //-- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- cout //-- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- int found_index; key = 30; found_index = search(a, size, key); if (found_index>=0){ found = a + found_index; cout } else{ cout } key = 35; found_index = search(a, size, key); if (found_index>=0){ found = a + found_index; cout } else{ cout } //-- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- cout //-- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- int copy_1_static[20]; cout copy_array(copy_1_static, a, size); //would also work with dynamic print_array(copy_1_static, size); //default capacity cout int *copy_2_dynamic = copy_array(copy_1_static, size); print_array(copy_2_dynamic, size, size); cout //-- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- cout //-- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- int *mark; key = 30; cout mark = search_entry(a, size, key); shift_right(a, size, mark); print_array(a, size, capacity); key = 60; cout mark = search_entry(a, size, key); shift_right(a, size, mark); print_array(a, size, capacity); key = 0; cout mark = search_entry(a, size, key); shift_right(a, size, mark); print_array(a, size, capacity); //-- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- cout //-- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- capacity *= 2; a = reallocate(a, size, capacity); cout print_array(a, size, capacity); key = 20; cout mark = search_entry(a, size, key); shift_right(a, size, mark); print_array(a, size, capacity); //-- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- cout //-- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- cout print_array(a, size, capacity); key = 60; cout mark = search_entry(a, size, key); shift_left(a, size, mark); print_array(a, size, capacity); key = 60; cout mark = search_entry(a, size, key); shift_left(a, size, mark); print_array(a, size, capacity); key = 20; cout mark = search_entry(a, size, key); shift_left(a, size, mark); print_array(a, size, capacity); key = 20; cout mark = search_entry(a, size, key); shift_left(a, size, mark); print_array(a, size, capacity); //-- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- cout //-- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- -- --- cout print_array(a, size, capacity); array_string(a, size); cout cout cout cout return true; } TEST(POINTER_ARRAY_FUNCS, PointerArrayFuncsTest) { bool success = pointer_array_functions_basic_test(true); EXPECT_EQ(success, true); } int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); std::cout return RUN_ALL_TESTS(); } Functions: (Only edit this) const int MINIMUM_CAPACITY = 3; template T* allocate(int capacity){ return new T[capacity]; } template T* reallocate(T* a, int size, int capacity){ T*walker = new T[capacity*2]; for (int i = 0; i walker[i] = a[i]; } size = capacity*2; return walker; } template void print_array(T* a, int size, int capacity, ostream& outs){ for(int i = 0; i outs } } template void print(T* a, unsigned int how_many, ostream& outs){ for (int i = 0; i outs } outs } template T* search_entry(T* a, int size, const T& find_me){ for(int i = 0; i if(find_me == a[i]){ return &a[i]; } } return nullptr; } template int search(T* a, int size, const T& find_me){ for(int i = 0; i if (find_me == a[i]) { return i; } } return -1; } template void shift_left(T* a, int& size, int shift_here){ for (int i = shift_here; i a[i-shift_here] = a[i]; } size -= shift_here; } template void shift_left(T* a, int& size, T* shift_here){ int b; T*walker = a; for (int i = 0 ; i if(walker[i]==*shift_here) { b = i; break; } } for (int i = b; i walker[i-b] = walker[i]; } size -= b; } template void shift_right(T *a, int &size, int shift_here){ if(shift_here > size) shift_here = size; for (int i = size - 1; i >= shift_here; i--) { a[i] = a[i-shift_here]; } size += shift_here; } template void shift_right(T *a, int &size, T* shift_here){ int b; for (int i = 0; i if(a[i]==*shift_here) { b = i; break; } } for (int i = size - 1; i >= b; i--) { a[i+b] = a[i]; } size += b; } template void copy_array(T *dest, const T* src, int many_to_copy){ for (int i = 0; i dest[i] = src[i]; } } template T* copy_array(const T *src, int size){ T*walker = new T[size]; for (int i = 0; i walker[i] = src[i]; } return walker; } template string array_string(const T* a, int size){ stringstream ss; for (int i = 0; i ss } return ss.str(); } Correct Output: My Output: 0102030405060 - search_entry() 30 was found: 30 35 was not found - search() 30 was found: 30 35 was not found - copy function() (@@) -..- void copy_array(dest, src, size): 0102030405060T copy_array(src, siae): 0102030405060 - shift_right() 030405060 - reallocate() - shift_left() 060 - shift left at 20:2030405060 - shift left at 20:2030405060 - - array_string() array now: 2030405060 array to string: 2030405060 Feel free to change the way the string is constructed: commas, better spacing, etc. end basic test OK ] POINTER_ARRAY_FUNCS.PointerArrayFuncsTest (140 ms) ] 1 test from POINTER_ARRAY_FUNCS (142 ms total) ] Global test environment tear-down ] 1 test from 1 test suite ran. (145 ms total)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started