Write a function that takes a vector of integers, an integer n representing the number of elements in that vector, and an integer b. Implement the sequential search algorithm to look for the integer b in the vector. The sequential search works as follows: you must look at all the elements of the vector until you find the b, from the first to the last. The function must return an integer representing how many elements the function tested until it found b. If it does not find the function, it must return 0. The name of the function must be called "busca_seq"

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Code in C. Solve the code below. 

 

Write a function that takes a vector of integers, an integer n representing the number of elements in that vector, and an integer b. Implement the sequential search algorithm to look for the integer b in the vector. The sequential search works as follows: you must look at all the elements of the vector until you find the b, from the first to the last. The function must return an integer representing how many elements the function tested until it found b. If it does not find the function, it must return 0. The name of the function must be called "busca_seq".

 

int busca_seq(int vetor[], int n, int b)

{

   //your code

}

 

My Code

 

#include<stdio.h>

int busca_seq(int vetor[], int n, int b)

{

 int i;

 for(i = 0; i < n; i++){

  if(vetor[i] == b)

   return (i+1);

 }

 

 {

 int vet[5],n = 5,b,x;

 

 vet[0] = 1;

 vet[1] = 2;

 vet[2] = 3;

 vet[3] = 4;

 vet[4] = 5;

 

 scanf("%d",&b);

 x = busca_seq(vet,n,b);

 printf("%d",x);

     return 0;

 }

 }

 

Test
Input Expected Got
int vet[5], n=5, b, x;
1
1
1
vet[0]=1;
vet[1]=2;
vet[2]=3;
vet[3]=4;
vet[4]=5;
scanf("%d",&b);
x=busca_seq(vet, n, b);
printf("%d",x);
int vet[5], n=5, b, x;
7
***Error***
vet[0]=1;
Segmentation fault
vet[1]=2;
vet[2]=3;
vet[3]=4;
vet[4]=5;
scanf("%d",&b);
x=busca_seq(vet, n, b);
printf("%d",x);
Transcribed Image Text:Test Input Expected Got int vet[5], n=5, b, x; 1 1 1 vet[0]=1; vet[1]=2; vet[2]=3; vet[3]=4; vet[4]=5; scanf("%d",&b); x=busca_seq(vet, n, b); printf("%d",x); int vet[5], n=5, b, x; 7 ***Error*** vet[0]=1; Segmentation fault vet[1]=2; vet[2]=3; vet[3]=4; vet[4]=5; scanf("%d",&b); x=busca_seq(vet, n, b); printf("%d",x);
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education