Question

In: Computer Science

1. Write a program that includes a function search() that finds the index of the first...

1. Write a program that includes a function search() that finds the index of the first

element of an input array that contains the value specified. n is the size of the array. If

no element of the array contains the value, then the function should return -1. Name your

program .C The program takes an int array, the number of elements in the array,

and the value that it searches for. The main function takes input, calls the

search()function, and displays the output.

int search(int a[], int n, int value);

Example input/output #1:

Enter the length of the array: 4

Enter the elements of the array: 8 2 3 9

Enter the value for searching: 3

Output: 2

Example input/output #2:

Enter the length of the array: 6

Enter the elements of the array: 4 7 1 0 3 9

Enter the value for searching: 5

Output: -1

2. Modify the part 1 program so that it deletes all instances of the value from the array. As

part of the solution, write and call the function delete() with the following

prototype. n is the size of the array. The function returns the new size of the array after

deletion (The array after deletion will be the same size as before but the actual elements

are the elements from index 0 to new_size-1). In writing function delete(), you may

include calls to function search(). The main function takes input, calls the

delete()function, and displays the output. Name your program .C

int delete(int a[], int n, int value);

Example input/output #1:

Enter the length of the array: 6

Enter the elements of the array: 4 3 1 0 3 9

Enter the value for deleting: 3

Output array:

4 1 0 9

Solutions

Expert Solution

#include<stdio.h>
int search(int a[],int n,int value){
   int i;
   for(i = 0; i < n; i++){
       if(a[i] == value){
           return i;
       }  
   }
   return -1;
}
int delete(int a[],int n,int value){
   int shift = 0;;
int removed = 0;
   int i;
for ( i = 0; i < n; i++)
{
if (search(a,n,value)!=-1)
{
removed++;
}
else
{
a[shift++] = a[i];
}
}
return (n - removed);
}
int main(){
   int arr[100],i,size;
   int value;
   printf("Enter the length of the array: ");
   scanf("%d",&size);
   printf("Enter the elements of the array: ");
   for(i = 0; i < size; i++)
       scanf("%d",&arr[i]);
   printf("Enter the value for searching: ");
   scanf("%d",&value);
   printf("Output: %d",search(arr,size,value));
  
}

/* OUTPUT */

Ans 2)

#include<stdio.h>

int delete(int a[],int n,int value){
   int shift = 0;;
int removed = 0;
   int i;
for ( i = 0; i < n; i++)
{
if (a[i] == value)
{
removed++;
}
else
{
a[shift++] = a[i];
}
}
return (n - removed);
}
int main(){
   int arr[100],i,size;
   int value;
   printf("Enter the length of the array: ");
   scanf("%d",&size);
   printf("Enter the elements of the array: ");
   for(i = 0; i < size; i++)
       scanf("%d",&arr[i]);
   printf("Enter the value for deleting: ");
   scanf("%d",&value);
   size = delete(arr,size,value);
   printf("Output array: \n");
   for(i = 0 ; i < size; i++){
       printf("%d ",arr[i]);
   }
  
}


Related Solutions

In Java, write a program that finds the first character to occur 3 times in a...
In Java, write a program that finds the first character to occur 3 times in a given string? EX. Find the character that repeats 3 times in "COOOMMPUTERRRR"
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Write a program of Binary Search in C++ by using function and arrays with the explanation.
C++ for Mac (Xcode) For this exercise, you will write a program that includes four function...
C++ for Mac (Xcode) For this exercise, you will write a program that includes four function definitions. You will also write a main() function that calls these four functions to demonstrate that they work as expected. The four functions are: 1. printExitMessage() : This function prints a message to the screen saying something like "Thanks for using this software. Goodbye." 2. getMin(): This function takes two float inputs and returns the lesser of the two float values. For example, if...
Part 1: Write a program that finds the sum and average of a series of numbers...
Part 1: Write a program that finds the sum and average of a series of numbers entered by the user. The program will first ask the user how many numbers there are. It then prompts the user for each of the numbers in turn and prints out the sum, average, the list of number from the user. Note: the average should always be a float, even if the user inputs are all ints. Part 2: Same as part 1 except...
Write a C program in Unix which uses a function called search to find the location...
Write a C program in Unix which uses a function called search to find the location of a value in THREE arrays of floats. The function should take three parameters :          the value to be found          the array to be searched          the size of the array N.B.!!!! The main program should read in three arrays of varying size          example : array a has twelve elements                    array b has six elements                    array c has nine...
For C++ Function 1: Write a recursive function to perform a sequential search on a set...
For C++ Function 1: Write a recursive function to perform a sequential search on a set of integers The function will require an array parameter and the number to look for. These are the minimal parameter requirements The function should take an array of any size Function 2: Write a recursive function that will convert an integer (base 10) to binary The function should only have an integer parameter Have the function write the binary number to the console You...
Write a Java program that implements the Depth-First Search (DFS) algorithm. Input format: This is a...
Write a Java program that implements the Depth-First Search (DFS) algorithm. Input format: This is a sample input from a user. 3 2 0 1 1 2 The first line (= 3 in the example) indicates that there are three vertices in the graph. You can assume that the first vertex starts from the number 0. The second line (= 2 in the example) represents the number of edges, and following two lines are the edge information. This is the...
You are given a function median(A,p,r) that finds the index corresponding to the median of an...
You are given a function median(A,p,r) that finds the index corresponding to the median of an array ? with starting index p and ending index r, in worst-case complexity Θ(?) where ? is the length of ?. Making use of the given median function, write an algorithm with complexity Θ(?) to partition the array ? using its median as the pivot. You may call the functions discussed in class. Using your algorithm in Qn 1, write an algorithm that selects...
a) Write an function that finds the independent variable value at which a mathematical function is...
a) Write an function that finds the independent variable value at which a mathematical function is maximized over a specified interval. The function must accept a handle to a function, and evaluate that function at values ranging from x1 to x2 with an increment of dx. The value returned is the value of x at which the maximum value of f(x) occurs. Function syntax: xm = xmax(f,x1,x2,dx); As was the case in the previous problem, this function does not find...
please write in c++ 2. Write a function sumOfArray that recursively finds the sum of a...
please write in c++ 2. Write a function sumOfArray that recursively finds the sum of a one-dimensional array. A sample run is below. The elements of the array are: 0 8 -4 6 7 The sum of the array is: 17 Press any key to continue . . .
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT