Question

In: Computer Science

Part 1- Delete an element from an Array. See the instructions below: -Use the previous Array...

Part 1- Delete an element from an Array. See the instructions below: -Use the previous Array code provided to you (to demonstrate how to insert elements in an Array). It is an Array with the following details: Size 10 Length 5. Your task is to Delete element on index 4

please use the code below and write in C++ how to delete the 4th element.

#include struct Array { int A[10]; int size; int length; }; void Display(struct Array arr) { int i; printf("\nElements are\n"); for(i=0;ilengthsize) arr->A[arr->length++]=x; } void Insert(struct Array *arr,int index,int x) { int i; if(index>=0 && index length) { for(i=arr->length;i>index;i--) arr->A[i]=arr->A[i-1]; arr->A[index]=x; arr->length++; } } int main() { struct Array arr1={{2,3,4,5,6},10,5}; Append(&arr1,10); Insert(&arr1,0,12); Display(arr1); return 0; }

Part 2 - - Create and display a singly Linked List with 5 elements (see below)

Node* head

Node*second

Node*third

Node*forth

Node*fifth

2-Assign the data 5, 6, 8, 10, 12 to each node

3-Display the output

Solutions

Expert Solution

Part 1:

Code in C++:

#include <iostream>
using namespace std;

int deleteElement(int arr[], int n, int x)
{
// Search x in array
int i;
for (i=0; i<n; i++)
if (arr[i] == arr[x])
break;
  
// If x found in array
if (i < n)
{
// reduce size of array and move all
// elements on space ahead
n = n - 1;
for (int j=i; j<n; j++)
arr[j] = arr[j+1];
}
  
return n;
}
  
/* Driver program to test above function */
int main()
{
int arr[] = {2,3,4,5,6};
int n = sizeof(arr)/sizeof(arr[0]);
int x = 4; //index number 4
cout << "Given array is \n";
for (int i=0; i<n; i++)
cout << arr[i] << " ";
// Delete x from arr[]
n = deleteElement(arr, n, x-1);
  
cout << "\n\nModified array is \n";
for (int i=0; i<n; i++)
cout << arr[i] << " ";
  
return 0;
}

Output:

This program has been compiled using online C++ conpiler.

Part 2:

Code in C++:

#include <iostream>
using namespace std;
  
class Node {
public:
int data;
Node* next;
};
  
// This function prints contents of linked list
// starting from the given node
void printList(Node* n)
{
cout<<"The linked List is "<< endl;
while (n != NULL) {
cout << n->data << " ";
n = n->next;
}
}
  
// Driver code
int main()
{
Node* head = NULL;
Node* second = NULL;
Node* third = NULL;
Node* forth = NULL;
Node* fifth = NULL;
// allocate 5 nodes in the heap
head = new Node();
second = new Node();
third = new Node();
forth = new Node();
fifth = new Node();
  
head->data = 5; // assign data in first node
head->next = second; // Link first node with second
  
second->data = 6; // assign data to second node
second->next = third;
  
third->data = 8; // assign data to third node
third->next = forth;
  
forth->data = 10; // assign data to forth node
forth->next = fifth;
  
fifth->data = 12; // assign data to fifth node
fifth->next = NULL;
printList(head);
  
return 0;
}
  

Output:

This program has been compiled using online C++ conpiler.


Related Solutions

Given an array of integers, delete each element from the array which is a multiple of 5, and display the rest of the array.
Given an array of integers, delete each element from the array which is a multiple of 5, and display the rest of the array.Input:    6    2 3 4 11 22 320    where:First line represents the number of elements in the array.Second line represents the elements in the array.Output:    2 3 4 11 22Explanation: Element of the array 320 is the only one in the array which is a multiple of 5, so it is removed from the array.Assumptions:Array can be of size...
Write an algorithm to delete an element from a hash table that uses linear probing as...
Write an algorithm to delete an element from a hash table that uses linear probing as its clash resolution strategy. Analyze your algorithm and show the results using order notation?
Part A Instructions: Use the information provided below for Plant A of Big Noizz Corporation to...
Part A Instructions: Use the information provided below for Plant A of Big Noizz Corporation to prepare the Statement of Cost of Goods Manufactured, Cost of Goods Sold and Income Statement for 2017. Sales $20,000 Raw Materials Used $5,000 Direct Labor Costs $2,000 Selling and Administrative Expenses $5,000 Beginning Raw Material Inventory $600 Ending Raw Material Inventory $2,000 Net Income $400 Beginning Work-in-Process Inventory zero Ending Work-in-Process Inventory $600 Beginning Finished Goods Inventory $1,400 Ending Finished Goods Inventory $800
Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array.
C++ Programming using iostream and namespace stdGiven an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1. void findDuplicate (int [ ], int)Example 1: Given array: {2,3,5,6,11,20,4,8,4,9} Output: 4 Example 2: Given array: {1,3,5,6,7,8,2,9} Output: -1
Instructions: 1) Below you will see the Nutrition Facts Panel and the Ingredients list for two...
Instructions: 1) Below you will see the Nutrition Facts Panel and the Ingredients list for two very similar products. Then you will see the ingredients list for another unrelated product. Read the information on the NFP and Ingredients list and answer the questions on the question form to turn in. Then, find an alternative to these products that is a more whole food and talk about it in discussions. See the discussion forum for the specific details/requirements. LC Thai Chicken...
(Data structure) Show the contents of the array below, once the “pivot” element is placed at...
(Data structure) Show the contents of the array below, once the “pivot” element is placed at its appropriate location after each call of the “Partition” algorithm, in the process of running Quick-Sort on said array. Arrange the data in descending order (from largest to smallest value). Always select the first element of the partition as “pivot” Apply sorting on the following data set s,       f,       p,      a,      g,      e,       v,      q,      i,        c
Write a program to remove an element from an array at the given position k and...
Write a program to remove an element from an array at the given position k and push the rest of the array elements one position back. Then insert the removed element at the beginning. Position k is entered through keyboard. For example, if the original array x is {'r', 'c', 'm', '7', 'w', '3', 'q'} and k = 3, the array will be changed to {'7', 'r', 'c', 'm', 'w', '3', 'q'}. Hint: Sequence of moving the element is important....
3. See the table below that was assigned to you as homework in a previous week.  Suppose...
3. See the table below that was assigned to you as homework in a previous week.  Suppose that the market price is $13 per unit. Quantity Total Cost Fixed Cost Variable Cost ATC AVC MC 0 $18 $18 0 18 0 0 1 $27 $18 $9 $27 $9 $9 2 $32 $18 $14 $16 $7 $5 3 $33 $18 $15 $11 $5 $1 4 $40 $18 $22 $10 $5.50 $7 5 $60 $18 $42 $12 $8.40 $20 a. What quantity will...
Instructions (In C++ Please) Verification This programming lab makes use of an array of characters. The...
Instructions (In C++ Please) Verification This programming lab makes use of an array of characters. The program will validate the characters in the array to see if they meets a set of requirements. Requirements: Must be at least 6 characters long (but not limited to 6, can be greater) Contains one upper case letter Contains one lower case letter Contains one digit Complete the code attached. Must use pointer while checking the characters. Download Source Lab 3 File: #include using...
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list....
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list. So if the item is 7 and the list is [1,3,7,4,7,3,7,2], the result is [1,3,7,4,7,3,2] 2- Use LinkList. Write removeAll(int n). Deletes all occurrences of an item n from a linked list. So if the item is 7 and the list1 is [1,3,7,4,7,3,2] , then list1.removeAll(7) then list1 becomes [1,3,4,3,2]. Demonstrate by displaying the list contents before and after calling the above methods. Eg:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT