Question

In: Computer Science

Please Note: Only do Problem 2 and please read Problem 2 carefully the requirements thanks /***********************************...

Please Note: Only do Problem 2 and please read Problem 2 carefully the requirements


thanks



/***********************************
* Filename: arraySorted.c 
* Author: Last name, first name
* Email: Your preferred email address
* Login ID: Your EECS login ID
************************************/

#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

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. • myRemove ( ): 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). • 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). Notes: • The functions (algorithms) must be the most efficient in terms of running time and memory usage. • Submit file arrayUnsorted.c. Complete the header with your student and contact information. Include sufficient comments in your code to facilitate code inspection. • Submit a report in PDF with following information: references (sources); error conditions and actions taken; brief algorithm; running time of the function (algorithm) and a brief explanation. See the template a1report.docx for an example. • See file a1output.txt for sample output. • Your code will be graded automatically by a script, so make sure to strictly follow the specifications. • Do not use any output statements (for example, printf) in your code, or they will mess up the grading scripts. • Use the main( ) function provided to test your code. Understanding the code in the main( ) function is part of the assignment. Do not change the code in the main( ) function in the final submission, or your program will mess up the grading script.

Problem 2: Sorted arrays This problem is the same as the above problem, except that the array of interreges is sorted and must always be maintained in non-decreasing order at any point in time. Note: Do not use any sorting algorithm in the program

Solutions

Expert Solution

Program :

/*************
* Filename: arraySorted.c 
* Author: Last name, first name
* Email: Your preferred email address
* Login ID: Your EECS login ID
************/

#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): Cannot insert more elements if size is already
                       more than or equal to capcity.
 */
 
int myAdd( int A[], int size, int d )
{
    if (size >= MAX_SIZE)
        return -1;
    else{
    int i;
    for (i = size - 1; (i >= 0 && A[i] > d); i--)
        A[i + 1] = A[i];
 
    A[i + 1] = d;
 
    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 )
{
   int i; 
    for (i = 0; i < size; i++) 
        if (A[i] == d) 
            return i; 
    return -1; 
}

/* 
   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 )
{
   int pos = search(A,size, d);
 
    if (pos == -1)
        return -1;
    else{  
    int i;
    for (i = pos; i < size - 1; i++)
        A[i] = A[i + 1];
 
    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

Screen shot for reference (wherever changes are made):

Output of the above program:


Related Solutions

Read the case below and respond to the requirements that follow. Please note that this is...
Read the case below and respond to the requirements that follow. Please note that this is a hypothetical case and does not represent the real issues as may pertain to the case study area. The Case of Nasha Body clinic Nana Ashorkor Kyei is the Managing Director of Nasha Body clinic, a leading “body massage butik” in Accra. She has established standards in her organisation that to the best of her knowledge must satisfy customers. Although some of her customers...
Please read the following program requirements carefully:  You are working as an eBay Seller, where...
Please read the following program requirements carefully:  You are working as an eBay Seller, where your customers are only in New York (n), New Jersey (j) and Pennsylvania (p).  Your C++ program is supposed ask the name of the state where the purchase is made. (n or j or p)  Next your program asks the amount of purchase.  Then, you calculate the Total amount by adding the proper State sales tax calculation to the Purchase Amount...
Please read the question carefully and then provide the answer thanks. Discussion Topic Choose a TCP/IP...
Please read the question carefully and then provide the answer thanks. Discussion Topic Choose a TCP/IP service, such as a web browser, email, file transfer, remote shell, DHCP, DNS, network time protocol, network address translation, etc. Important! Everyone should pick a different service, if possible, to keep the discussion fresh. In your original post, answer the following about the service you have chosen: How would you determine the IP addresses of your devices on your network? Success Hints You can...
[The problem was slightly modified. Please read a problem carefully.] A 1970s case-control study on cerebrovascular...
[The problem was slightly modified. Please read a problem carefully.] A 1970s case-control study on cerebrovascular disease (thrombotic stroke) and oral contraceptive use in young women matched cases to controls according to neighborhood, age, sex, and race. The table below displays data from this study for thrombotic stroke. Perform an appropriate test at alpha level 1%. Please use McNemar’s test to test the association between cerebrovascular disease (thrombotic stroke) and oral contraceptive use Case exposed Case Not exposed Control exposed...
Instructions – PLEASE READ THEM CAREFULLY The Assignment must be submitted on Blackboard (WORD format only)...
Instructions – PLEASE READ THEM CAREFULLY The Assignment must be submitted on Blackboard (WORD format only) via allocated folder. Assignments submitted through email will not be accepted. Students are advised to make their work clear and well presented, marks may be reduced for poor presentation. This includes filling your information on the cover page. Students must mention question number clearly in their answer. Late submission will NOT be accepted. Avoid plagiarism, the work should be in your own words, copying...
Instructions – PLEASE READ THEM CAREFULLY The Assignment must be submitted on Blackboard (WORD format only)...
Instructions – PLEASE READ THEM CAREFULLY The Assignment must be submitted on Blackboard (WORD format only) via allocated folder. Assignments submitted through email will not be accepted. Students are advised to make their work clear and well presented, marks may be reduced for poor presentation. This includes filling your information on the cover page. Students must mention question number clearly in their answer. Late submission will NOT be accepted. Avoid plagiarism, the work should be in your own words, copying...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? *IMPORTANT* PLEASE READ CAREFULLY....
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? *IMPORTANT* PLEASE READ CAREFULLY. WE HAVE TO DO WHAT THIS ASSIGNMENT DOES OR WE WILL MARKED OFF POINTS. IT DOES NOT HELP WHEN YOU CHANGE THE SKELETON TO YOU'RE PREFERENCE. THIS IS FOR A BASIC C++ LEVEL CLASS SO WE HAVE TO STICK TO BASIC C++ CODE. HOWEVER IT COULD BE WRONG IN TERMS OF WORKING CONDITIONS SO PLEASE HELP FIX THESE. *IMPORTANT* void readFile(Candidate candidates[]) – reads...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? ****IMPORTANT**** PLEASE READ CAREFULLY...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? ****IMPORTANT**** PLEASE READ CAREFULLY ****IMPORTANT**** ***GOALS*** HOW TO CHECK FOR COMMAS, TILL THE END OF FILE. IT WILL CHECK THE LINE FOR THE APPRORIATE FORMAT IN THE TEXT FILE. IF THERE IS MISSING A COMMA, IT WILL IGNORE, IF THERE IS A WHITE SPACE, IT WILL CORRECT AND READ LINE, IF IT IS MISSING 1 OF THE 3 INFORMATION, IT WILL IGNORE. Display candidates’ names using displayList() function...
PLEASE READ CAREFULLY AND EXPLAIN YOUR ANSWER IF POSSIBLE (JAVASCRIPT ONLY) Write out the call stack...
PLEASE READ CAREFULLY AND EXPLAIN YOUR ANSWER IF POSSIBLE (JAVASCRIPT ONLY) Write out the call stack for this program if x is 5. function factorial(x) { if (x === 0) { return 1; } return x * factorial(x-1); } console.log(factorial(x)); Use "not in function" if not in a function, and put the function name along with the arguments if in a function. Not in function in function_name(arg, ...) ...
Please note that problem solving often requires you to consider additional requirements that are not specifically...
Please note that problem solving often requires you to consider additional requirements that are not specifically listed. You are to write a Java program to represent cars. The program has a Car class (Car.java) with constructors, accessors, mutators, and print methods. This class should not have static data members or static methods. The main method will be in another class. The data members include the car model year, make, model, retail price, and current value; for example, a 2020 Ford...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT