Question

In C language Write a program that includes a function search() that finds the index of...

In C language

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. 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. Name program part1.c

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

Example 1:

Enter length of array: 4

Enter elements of array: 8 2 3 9

Enter the value for searching: 2

Output: 1

Example 2:

Enter the length of the array: 6

Enter the elements of the array: 4 6 1 0 2 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 part2.c.

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

Example 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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

Part1.c code Screenshot:

#include<stdio.h> int main() { int n, i; printf(Enter the length of array : ); scanf(%d,&n); int arr[n]; printf(Enter th

Part1.c - Output Screenshot1

C:\Program Files (x86)\Dev-Cpp ConsolePauser.exe Enter the length of array : 4 Enter the elements of array : 4 2 8 9 Enter th

Part1.c - Output Screenshot2

- 0 x C:\Program Files (x86)\Dev-Cpp ConsolePauser.exe Enter the length of array : 6 Enter the elements of array : 4 6 1 0 2

Part2.c code Screenshot:

#include<stdio.h> int main() { int n, i; printf(Enter the length of array: ); scanf(%d,&n); int arr[n]; printf(Enter the

Part2.c Output Screenshot:

- O X C:\Program Files (x86)\Dev-Cpp\ConsolePauser.exe Enter the length of array : 6 Enter the elements of array : 4 3 10 39

Part1.c Code in Text Format:

#include<stdio.h>
int main(){
        int n, i;
        printf("Enter the length of array : ");
        scanf("%d",&n);
        
        int arr[n];
        printf("Enter the elements of array : ");
        for(i = 0;i < n;i++){
                scanf("%d",&arr[i]);
        }
        
        
        int value;
        printf("Enter the value for searching : ");
        scanf("%d",&value);
        
        
        int result = search(arr,n,value);
        
        // if element was not found
        if(result == -1){
                printf("Output: %d", result);
        }
        // if Element was found
        else{
                printf("Output: %d",result);
        }
        return 0;
}

int search(int arr[],int n, int x){
        int i, count = 0, index;
        for(i = 0;i<n;i++){
                if(arr[i]==x){
                        index = i;
                        count++;
                        break;
                }
        }
        if(count == 0){
                return -1;
        }
        else{
                return 1;
        }
}

Part2.c Code in Text format:

#include<stdio.h>
int main(){
        int n, i;
        printf("Enter the length of array : ");
        scanf("%d",&n);
        
        int arr[n];
        printf("Enter the elements of array : ");
        for(i = 0;i < n;i++){
                scanf("%d",&arr[i]);
        }
        
        int value;
        printf("Enter the value for deleting : ");
        scanf("%d",&value);
        
        int result = delete(arr,n,value);
        if(result == 0){
                printf("Element not found");
        }
        else{
                printf("Output array: ");
                for(i=0;i<n-result;i++){
                        printf("%d ",arr[i]);
                }
        }
        return 0;
}
int delete(int arr[],int n, int x){
        
        int i, j, count = 0;
        for(i=0;i<n;i++){
                if(arr[i]==x){
                        count++;
                }
        }
        
        for(i=0;i<n;i++){
                if(arr[i]==x){
                        for(j = i;j<n;j++){
                                arr[j] = arr[j+1];
                        }
                        delete(arr, n, x);      
                }
        }
        return count;
}
Add a comment
Know the answer?
Add Answer to:
In C language Write a program that includes a function search() that finds the index of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C program convert.c that converts each number in an array by the sum of...

    Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...

  • The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an ar...

    The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an array in ascending order. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that...

  • (a) Write a recursive function int find(const int A[], int n, int x); which returns the...

    (a) Write a recursive function int find(const int A[], int n, int x); which returns the first index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found. (b) Write a recursive function int rfind(const int A[], int n, int x); which returns the last index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found....

  • Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...

    Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. To test your function, write a main that prompts a user for a list of 15 integers and outputs the index and value of the first occurrence of the smallest value. The program should print out Enter 15 integers: The position of the first occurrence of the smallest element in...

  • Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int...

    Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int *a2) Write a program addition.c that reads in an array (a1) of numbers, and creates a new array (a2) of numbers such that the first and last numbers of a1 are added and stored as the first number, the second and second-to-last numbers are added and stored as the second number, and so on. You need to check for even and odd length of...

  • Program must be wriiten in c++ Write a function, removeAt, that takes three parameters: an array...

    Program must be wriiten in c++ Write a function, removeAt, that takes three parameters: an array of integers, the number of elements in the array, and an integer (say, index). The function should delete the array element indicated by index. If index is out of range or the array is empty, output an appropriate message. (Note that after deleting the element, the number of elements in the array is reduced by 1.) Assume that the array is unsorted.

  • Write a program in C++ language that finds the largest number in an array of positive...

    Write a program in C++ language that finds the largest number in an array of positive numbers using recursion. Your program should have a function called array_max that should pass in an int array, and an int for the size of the array, respectively, and should return an int value. As an example, given an array of size 10 with contents: {10, 9, 6, 1, 2, 4, 16, 8, 7, 5} The output should be: The largest number in the...

  • Write and test a function in C++ that uses the binary search algorithm to search an...

    Write and test a function in C++ that uses the binary search algorithm to search an array of sorted strings – use a do..while loop to allow user to perform multiple searches w/o terminating the program – see sample output below. Use this name array: string names[SIZE] = { "Collins, Bill", "Smith, Bart", "Allen, Jim",         "Griffin, Jim", "Stamey, Marty", "Rose, Geri", "Taylor, Terri",         "Johnson, Jill", "Allison, Jeff", "Looney, Joe", "Wolfe, Bill",         "James, Jean", "Weaver, Jim", "Pore, Bob",...

  • C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below...

    C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below you will find a linear search function with early stop. A linear search is just a naive search - you go through each of the elements of a list one by one. Early stop works only on sorted list. Early stop means, instead of going through whole list, we will stop when your number to search can no longer be possibly found in the...

  • Language C Code Write a program that takes two integer arrays (A and B) and sums...

    Language C Code Write a program that takes two integer arrays (A and B) and sums them together (element wise). A third array to accept the result should be passed in as the output argument. Assume the arrays are all the same size. The argument N is the size of the arrays. Your code should provide a function with the following signature: void array Addition (int A[], int B[], int N, int output[]) { } Your code must also provide...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT