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...
Given two unsorted arrays of integers. a) Write a pseudocode algorithm which will output only the...
Given two unsorted arrays of integers. a) Write a pseudocode algorithm which will output only the integers not common to both arrays. When writing pseudocode, consider that no implementation for data structures or algorithms exist. b) Implement your algorithm in Modern C++
Given an unsorted array A of size N of integers, find a continuous sub-array which adds...
Given an unsorted array A of size N of integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax. (no [ ]'s or (ptr+1) stuff. input will be the number of input values to enter followed by the sum to compare with. print out the continuous sub-array of values that are equal to sum or the message 'no sum ofund.' there may be more than one sub-array to be found in...
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N...
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax (no [ ]’s or (ptr+i) stuff.     Input will be the number of input values to enter followed by the sum to compare with. Print out the continuous sub-array of values that are equal to sum or the message ‘No sum found’. There...
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...
Find the K'th smallest element in an unsorted array of integers. Find the K'th largest element...
Find the K'th smallest element in an unsorted array of integers. Find the K'th largest element in an unsorted array of integers. please make two separate methods in java
. As input you are given two arrays: an array of numbers ? and an array...
. As input you are given two arrays: an array of numbers ? and an array ? of queries where each query is a target number. The array ? is unsorted and may contain duplicates. Your goal is, for each query ? in the array ?, count the number of pairs in the array ? that sums up to ?; that is, the number of distinct pairs of indices [?, ?], with ? < ?, such that ?[?] + ?[?]...
Problem 1: [10 Marks] Given a generic array with ‘n’ items (not only integers). Give a...
Problem 1: [10 Marks] Given a generic array with ‘n’ items (not only integers). Give a solution for checking whether there are any duplicate elements in the array or not? You just need to return a boolean (true or false). State the complexity of your solution. Can you come up with another solution that has O(n logn) complexity? Explain. Problem 2: [10 Marks] Now consider the same problem as problem 1, but this time you only have positive integer numbers...
You are given an array of arrays a. Your task is to group the arrays a[i]...
You are given an array of arrays a. Your task is to group the arrays a[i] by their mean values, so that arrays with equal mean values are in the same group, and arrays with different mean values are in different groups. Each group should contain a set of indices (i, j, etc), such that the corresponding arrays (a[i], a[j], etc) all have the same mean. Return the set of groups as an array of arrays, where the indices within...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT