Question

How to make the last element of an array null?(c++)So for my data structures class, we...

How to make the last element of an array null?(c++)So for my data structures class, we are working in c++. We are supposed to create a function that takes in an array, shifts everything to the left and leaves the original final element as null. Now I haven't found any simple way to convert them to null so I was just converting them to "0".I was seeking help on how to modify my code such to make the final element a null value.

We are also testing our code with catch 2,so I will include my test case that isn't running. Any help would be greatly appreciated. I was also seeking help on how to get the function to be called and properly function

Thank you! :)

edit:Sorry if the question was ambiguous

here is the code I have so far.

```
int * left_shift(int arr[],int n)
{
for(int i = 0 ; i {
       if(i==n-1){ arr[i] = 0; } // make last element null
       arr[i] = arr[i+1]; //shift element to the right
   }
   return arr; //function then returns modifed array
}

```

and here are the failing tests,that are saying I am converting the file incorrectly. I don't quite understand the error message to be honest

```
TEST_CASE("left_shift","[Lab1]"){

       SECTION("ex1")//test one
       {
           int arr[] = {1,2,3,4};//initial array
           int n = sizeof(arr)/sizeof(int);
          
           int r[]= {left_shift(&arr,n)};
           CHECK(r[n-1] == 0);
   }

}
```


Thanks again !

Homework Answers

Answer #1

Hi,

Please find the solution below:
------------------------------


There is a small bug in the code. After checking for the final last element and assigning it to zero,
We need to break the code.

Note: On assumption that the last element is set to zero.

if(i==n-1){
           arr[i] = 0;
           break;
       } // make last element null


Otherwise, the code shifts some junk content to the last element in the next line.

arr[i] = arr[i+1]; //shift element to the right


The above would execute and overrides the assignment of zero in the last element.

-----


The function returns the base address of the array. Use of pointers would be helpful in the test case.

Sample code:


// Example program
#include <iostream>

int* left_shift(int arr[],int n)
{
for(int i = 0 ; i<n;i++) {
       if(i==n-1){
           arr[i] = 0;
           break;
       } // make last element null
       arr[i] = arr[i+1]; //shift element to the right
   }
   return arr; //function then returns modifed array
}

int main()
{
int arr[] = {1,2,3,4};//initial array
int n = sizeof(arr)/sizeof(int);
        
int* p;
p=left_shift(arr,n);

for ( int i = 0; i < n; i++ ) {
      std::cout << *(p + i) << "\n" ;
   }

}

------------------------------

Hope this helps.

Know the answer?
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
Similar Questions
Write a MIPS assembly program that sorts an array using bubble sort translating the C code...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code int main(void) { int array[] = {10, 2, 7, 5, 15, 30, 8, 6}; // input array int arraySize = sizeof(array)/sizeof(array[0]); bool swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; //Note : "j" , "arraySize - j" are optimizations to the bubble sort algorithm j++; // j= sorted elements int i=0; /* "arraySize - j" is used...
What is wrong with my recursive Binary Search code? For some reason my 'middle' value is...
What is wrong with my recursive Binary Search code? For some reason my 'middle' value is always zero? Please fix and explain! #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int BinarySearch(const int A[],const int L,const int R,const int key) {              if (R >= 1){           int middle = L + (R-1)/2;                              if (A[middle] == key)        return A[middle];               if (A[middle] > key){...
Restricted structures such as stack and queue are fast, but they do not support access in...
Restricted structures such as stack and queue are fast, but they do not support access in the key field mode. Group of answer choices True False Big O analysis evaluates an algorithm based on its _________ performance. Group of answer choices A. average-case B. best-case C. worst-case Which of the following algorithms is the fastest in speed? Group of answer choices A. Polynomial time algorithm B. Linear time algorithm C. Exponential time algorithm The following code gives an implementation of...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...
The AssemblyLine class has a potential problem. Since the only way you can remove an object...
The AssemblyLine class has a potential problem. Since the only way you can remove an object from the AssemblyLine array is when the insert method returns an object from the last element of the AssemblyLine's encapsulated array, what about those ManufacturedProduct objects that are "left hanging" in the array because they never got to the last element position? How do we get them out? So I need to edit my project. Here is my AssemblyLine class: import java.util.Random; public class...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void search_node(struct linked_list* list, int find_node_ value) (The function to make) This function finds the node from the list that value is same with find_node_value and count the order of the node. This function should print message “The order of (node_value) is (order).” and error message “Function search_node : There is no such node to search.”....
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an executable with gcc -Wall -DUNIT_TESTS=1 array-utils5A.c The definitions for is_reverse_sorted and all_different are both defective. Rewrite the definitions so that they are correct. The definition for is_alternating is missing. Write a correct definition for that function, and add unit tests for it, using the unit tests for is_reverse_sorted and all_different as models. Please explain the logic errors present in in the definition of is_reverse_sorted...
Hello, in my programming class we have to create a function that will receive a time...
Hello, in my programming class we have to create a function that will receive a time in units of seconds and must return the equivalent time in units of hours-minutes-seconds. The function must also be called from main. I have attached what I have so far if someone could help me finish it that would be great.   int convert(void); int main() {    int time;    time = convert();    printf("Hour:Minute:Seconds is %d\n", time);       return 0; } int...
Hi there, I've been asked to write a program in C which can read values from...
Hi there, I've been asked to write a program in C which can read values from a file then sort them, and then write to a binary file. I'm getting stuck when I write my binary file as the output is just spitting out garbage values and not the values that are being read in. When I print my input file reader everything is perfect but after sorting and then writing, the output is completely wrong. I have checked that...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels (integer values) that broadcast the show. For this problem you can ONLY use the following string library functions: strcpy, strlen, strcmp. You MAY not use memcpy, memset, memmove. You can assume memory allocations are successful (you do not need to check values returned by malloc nor calloc). typedef struct tv_show { char *name; int num_channels, *channels; } Tv_show; a. Implement the init_tv_show function that...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT