In: Computer Science
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 %
#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