Question

In: Computer Science

Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions:...

Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions:

myAdd ( ): add an integer d to the array; return 0 if the operation is successful; return a negative number otherwise.

search ( ): given an integer d, if d is found in the array, return the index of the cell containing d. Return a negative number otherwise (e.g., d is not found in the array).

myRemove ( ): Step 1: remove integer d from the array; return 0 if the operation is successful; return a negative number otherwise (e.g., d is not found in the array).

Step 2: If element removed, shift the last element in the array into the position the removed element was in. (For example, if the Array is 10 -98 5 -67, and 10 is removed, the array should now look like this -67 -98 5).

******No points for deleting the element, but not shifting the array.

Problem 2: Submit a report with following information: error conditions and actions taken; brief algorithm; running time of the function (algorithm) (what was the complexity O(n), O(1), etc) and a brief explanation.

Augment the following code to produce your results:

#include <stdio.h>

/*********  DO NOT CHANGE THESE CONSTANTS IN THE FINAL SUBMISSION *********/

#define MAX_SIZE 20
#define SUCCESS 0

/******************  YOUR CODE STARTS HERE ******************/
/************************************************************/
/* 
   Input: array A with "size" elements and an integer d
   Output: d is added to the array.
   Return 0 if the addition is successful.
   Return a negative number if the addition is unsuccessful.
   Error condition(s): fill in the error condition(s).
 */
 
int myAdd( int A[], int size, int d )
{
   /* ADD YOUR CODE HERE */
  
   return 0;
}


/* 
   Input: array A with "size" elements and an integer d
   Output: If d is found in the array, return the index of the cell containing d.
   Otherwise return a negative number if d is not found.
   Error condition(s): fill in the error condition(s).
 */
 
int search( int A[], int size, int d )
{
   /* ADD YOUR CODE HERE */
   
   return 0;
}

/* 
   Input: array A with "size" elements and an integer d
   Output: Return a negative number if d is not found.
   Otherwise d is removed from the array and return 0.
   Error condition(s): fill in the error condition(s).
 */

int myRemove( int A[], int size, int d )
{
   /* ADD YOUR CODE HERE */
   
   return 0;
}


/*******************  YOUR CODE ENDS HERE *******************/
/************************************************************/

/*********  DO NOT CHANGE ANYTHING BELOW THIS LINE IN THE FINAL SUBMISSION *********/

/* 
   Input: array A with "size" elements
   Output: Display the array on the standard output with one space between every two numbers. 
   Print a new line after the last element.
   If the array is empty, print "Empty array" and a new line.
   Error condition(s): fill in the error condition(s).
 */

int printArray( int A[], int size )
{
  int i;
  if ( size == 0 )
    printf( "Empty array\n" );
  else {
  for ( i = 0; i < size; i++ )
     printf("%d ", A[ i ] );
  printf( "\n" );
  }
  return 0;
}

/* main() function 
 */
int main()
{
int myArray[MAX_SIZE];
int retCode, data, size;
char action;

size = 0;
do {
  scanf( "%d %c", &data, &action );
  switch( action )
  {
    case 'a': /* add */
    case  'A':
      retCode = myAdd( myArray, size, data );
      if ( retCode == SUCCESS ) {
        size++;         // increment size after successful addition
        printArray( myArray, size ); 
      }
      else
        printf( "Failed to add %d.\n", data );  
      break; 
        
    case 'r': /* remove */
    case 'R':
      retCode = myRemove( myArray, size, data );
      if ( retCode == SUCCESS ) {
        size--;         // decrement size after successful removal
        printArray( myArray, size ); 
      }
      else
        printf( "Failed to remove %d.\n", data );    
      break;  
                    
    case 's': /* search */
    case 'S':
      retCode = search( myArray, size, data );
      if( retCode >= 0 )
        printf( "Found %d at index %d.\n", data, retCode );
      else 
        printf( "Not found %d.\n", data );        
      break;
      
    case 'q':  /* quit */
    case 'Q':
      /* To quit, enter an arbitrary integer and character (action) 'q' or 'Q'.
         This is not elegant but makes the code simpler.  */  
      /* Do nothing but exit the switch statement */
      break; 
    
    default:
      printf( "Invalid operation %c\n", action );  
  }
} while ( action != 'q' && action != 'Q' );

return 0; 
} // end main

Check your code with the following sample output:

// Note: The following output assumes an array of maximum size of 5 elements.
// Your program requires an array of maximum size of 20, so it must be able  
// to accept and store up to 20 elements.


indigo 239 % ./a.out 
10 a
10 
-98 a
10 -98 
5 a
10 -98 5 
67 a
10 -98 5 67 
5 s
Found 5 at index 2.
10 s
Found 10 at index 0.
-67 a
10 -98 5 67 -67 
90 a
Failed to add 90.
67 r
10 -98 5 -67 
-1 r
Failed to remove -1.
-5 s
Not found -5.
10 r
-67 -98 5 
-98 r
-67 5 
5 r
-67 
-67 r
Empty array
10 r
Failed to remove 10.
6 s
Not found 6.
100 q
indigo 240 % 

Solutions

Expert Solution

#include<stdio.h>
#include<conio.h>
#define MAX_SIZE 20
#define SUCCESS 0
int printArray( int A[], int size )
{
int i;
if ( size == 0 )
printf( "Empty array\n" );
else {
for ( i = 0; i < size; i++ )
printf("%d ", A[ i ] );
printf( "\n" );
}
return 0;
}

int myAdd( int myArray[], int size, int data)
{
int i;
if(size!=20)
{
myArray[size]=data;
}
  
return 0;
}

int myRemove(int myArray[], int size, int data )
{ int i;
for (i=0; i< size; i++)
{
if(myArray[i]==data)
{
myArray[i] = myArray[i+1];
return 0;
break;
}  
}
return 1;
}


int search(int myArray[], int size, int data )
{ int i;
for (i=0; i< size; i++)
{
if(myArray[i]==data)
return i;
}
return -1;
}


/* main() function
*/
int main()
{
int myArray[MAX_SIZE];
int retCode, data, size;
char action;

size = 0;
do {
scanf( "%d %c", &data, &action );
switch( action )
{
case 'a': /* add */
case 'A':
retCode = myAdd( myArray, size, data );
if ( retCode == SUCCESS ) {
size++; // increment size after successful addition
printArray( myArray, size );

}
else
printf( "Failed to add %d.\n", data );
break;
  
case 'r': /* remove */
case 'R':
retCode = myRemove( myArray, size, data );
if ( retCode == SUCCESS ) {
size--; // decrement size after successful removal
printArray( myArray, size );
}
else
printf( "Failed to remove %d.\n", data );
break;
  
case 's': /* search */
case 'S':
retCode = search( myArray, size, data );
if( retCode >= 0 )
printf( "Found %d at index %d.\n", data, retCode );
else
printf( "Not found %d.\n", data );
break;
  
case 'q': /* quit */
case 'Q':
/* To quit, enter an arbitrary integer and character (action) 'q' or 'Q'.
This is not elegant but makes the code simpler. */
/* Do nothing but exit the switch statement */
break;
  
default:
printf( "Invalid operation %c\n", action );
}
} while ( action != 'q' && action != 'Q' );

return 0;
}

Document:

I implemented myAdd,myRemove and serch functions.

I corrected code in this case


Related Solutions

IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the kth smallest item in an array. Use the first item as the pivot. Compare sets of results using a static call counter. Reset counter before running another search. Create a Test drive to exhaustively test the program. // Assume all values in S are unique. kSmall(int [] S, int k): int (value of k-smallest element) pivot = arbitrary element from S:  let’s use the first...
Problem Definition: Problem: Given an array of integers print all pairs of integers a and b...
Problem Definition: Problem: Given an array of integers print all pairs of integers a and b where a + b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a + b = 16 A= [ 10, 4, 6, 15, 3, 5, 1, 13] The following are pairs that sum to 16: 13, 3 6, 10 15, 1 Your program should print these...
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
C++ Program 1–Implement a Priority Queue(PQ) using an UNSORTED LIST. Use an array size of 10...
C++ Program 1–Implement a Priority Queue(PQ) using an UNSORTED LIST. Use an array size of 10 elements. Use a circular array: Next index after last index is 0. Add the new node to next available index in the array. When you add an element, add 1 to index (hit max index, go to index 0). Test if array in full before you add. When you remove an element, from the list, move the following elements to the left to fill...
Problem 3 (4+2+2 marks). (a) Implement the following algorithm, which is given a duplicate-free array array...
Problem 3 (4+2+2 marks). (a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++. whatDoIDo (array): 1) Build a heap from array (using buildHeap as explained in class), where the heap starts at position array[0]. 2) Starting from j = size of array - 1, as long as j>0: i. Swap the entries array[0] and array[j]. ii. Percolate down array[0], but only within the subarray array[0..j-1]. iii. Decrement j by 1. Provide three input/output...
Implement a Priority Queue (PQ) using an UNSORTED LIST. Use an array size of 10 elements....
Implement a Priority Queue (PQ) using an UNSORTED LIST. Use an array size of 10 elements. Use a circular array: Next index after last index is 0. Add the new node to next available index in the array. When you add an element, add 1 to index (hit max index, go to index 0). Test if array in full before you add. When you remove an element, from the list, move the following elements to the left to fill in...
1. Implement a public method named initialize. It takes a twodimensional square array of integers...
1. Implement a public method named initialize. It takes a two dimensional square array of integers namedarray as a parameter. It initializes all of the elements of the array to the sum of their indices except for themajor diagonal (upper left to lower right) where each element is initialized to -1. (For testing use a 4X4 or5X5 array and have the application print out the array in 2 dimension format.2. Implement a method named totals that takes a two dimensional...
Given an unsorted integer array A of size n, develop an pseudocode with time complexity as...
Given an unsorted integer array A of size n, develop an pseudocode with time complexity as low as possible to find two indexes i and j such that A[i] + A[j] = 100. Indexes i and j may or may not be the same value.
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++....
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++. whatDoIDo (array): 1) Build a heap from array (using buildHeap as explained in class), where the heap starts at position array[0]. 2) Starting from j = size of array - 1, as long as j>0: i. Swap the entries array[0] and array[j]. ii. Percolate down array[0], but only within the subarray array[0..j-1]. iii. Decrement j by 1. Provide three input/output examples for duplicate-free arrays...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT