1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Sedaia [141]
3 years ago
10

Create a method to search an un-ordered array of integers for a value, if the value is found return the index of its position in

the array, if not found, return -1.
Computers and Technology
1 answer:
Hatshy [7]3 years ago
6 0

Answer:

Following are the program in C++ language  

#include <iostream> // header file

using namespace std; // namespace

int search(int s[],int s1,int n) // function search  

{

   for (int k= 0; k<n; k++) // iterating over the loop

 {

   if (s[k] == s1)   // searching the element  

   {

   return(k); // return the index

     break;

   }

 

}

return(-1); // return(-1)

}

int main() // main function

{

int arr[100],n1,i,s; // variable declaration  

cout<<"Enter number you want in the array:";

cin>>n1;

cout<<"Enter the elements in the array\n ";

for (i = 0; i<n1; i++)

{

   cin>>arr[i]; // taking input into the array  

}

 cout<<"Enter a number you want to search: ";

cin>>s; // read the serach element by the user  

int res= search(arr,s,n1); // calling function search

cout<<res; // display result

 return 0;

}

Output:

Enter number you want in the array:3

Enter the elements in the array

1

2

34

Enter a number you want to search: 2

2

Explanation:

Following are the description of the program  

  • Read the number you want in array in the "n1" variable of int type.
  • Read the array by the user in the "arr ".
  • Read the searching element in the  "s" variable.
  • calling the function search by passing array arr, searching element "s" and "n1".
  • In the search function, it returns the index of its position in the array, if not found then it return -1.
  • Finally print the index.  

You might be interested in
Our Client, a renowned trading company, suffered a sudden, devastating power outage that caused their server to cease functionin
Norma-Jean [14]

Answer:

Explanation:

I will consult the following strategies:

Step 1: Being a discerning consultant I should be knowing what exactly has caused the drive to get corrupted.

Step 2: After examining the current state of the hard drive I will explain the actual problem to the client in a very lucid and eloquent manner whether it is feasible to retrieve the lost data or it is beyond repair.

Step 3: Identify a solution to the problem with things at my disposal(finding a right tool).

Step 4: apply the solution found to retrieve the data from the hard drive.

Step 5: submitting the retrieved data from the corrupt drive to the customer and advocating them the preventive measures for future reference.

The above five steps will work perfectly to resolve the issue faced if followed with great care and being heedful of the situation at hand.

6 0
3 years ago
We will use linear interpolation in a C program to estimate the population of NJ between the years of the census, which takes pl
nikklg [1K]

Answer:

See explaination for program code

Explanation:

program code below:

#include<stdio.h>

int main()

{

//file pointer to read from the file

FILE *fptr;

//array to store the years

int years[50];

//array to store the population

float population[50];

int n = 0, j;

//variables for the linear interpolation formula

float x0,y0,x1,y1,xp,yp;

//opening the file

fptr = fopen("njpopulation.dat", "r");

if (fptr != NULL)

{

//reading data for the file

while(fscanf(fptr, "%d %f", &years[n], &population[n]) == 2)

n++;

//prompting the user

int year = -1;

printf("Enter the years for which the population is to be estimated. Enter 0 to stop.\n\n");

while(year != 0)

{

printf("Enter the year (1790 - 2010) : ");

scanf("%d", &year);

if (year >= 1790 && year <= 2010)

{

//calculating the estimation

xp = year;

for (j = 0; j < n; j++)

{

if (year == years[j])

{

//if the year is already in the table no nedd for calculation

yp = population[j];

}

else if (j != n - 1)

{

//finding the surrounding census years

if (year > years[j] && year < years[j + 1])

{

//point one

x0 = years[j];

y0 = population[j];

//point two

x1 = years[j + 1];

y1 = population[j + 1];

//interpolation formula

yp = y0 + ((y1-y0)/(x1-x0)) * (xp - x0);

}

}

}

printf("\nEstimated population for year %d : %.2f\n\n", year, yp);

}

else if (year != 0)

{

printf("\nInvalid chioce!!\n\n");

}

}

printf("\nAborting!!");

}

else

{

printf("File cannot be opened!!");

}

close(fptr);

return 0;

}

OUTPUT :

Enter the years for which the population is to be estimated. Enter 0 to stop.

Enter the year (1790 - 2010) : 1790

Estimated population for year 1790 : 184139.00

Enter the year (1790 - 2010) : 1855

Estimated population for year 1855 : 580795.00

Enter the year (1790 - 2010) : 2010

Estimated population for year 2010 : 8791894.00

Enter the year (1790 - 2010) : 4545

Invalid chioce!!

Enter the year (1790 - 2010) : 1992

Estimated population for year 1992 : 7867020.50

Enter the year (1790 - 2010) : 0

Aborting!!

5 0
4 years ago
Why do TV shows/Movies from the 90's and early 2000s look so much different compared to today?
icang [17]
Back then, movies were black and white because they had cameras that could only see black and white. In the late 1900's and early 2000's, they developed color cameras. But the common resolution and screen size was 640x480 or 4:3. Nowadays, in the late 2000's and early 2010, the widescreen format was more commonly used for many reasons. Today, the common colors are RGB 1M colors, and the resolution is 16:9. Some movies like to go even farther, like me, and use 21:9,which is super widescreen. That is more immersive than 16:9, and gives the watcher a wider, better experience. 
7 0
3 years ago
What should you do if ads keep popping up on your computer when using Google Chrome?
romanna [79]
I think it’s B.. I’m pretty sure
8 0
3 years ago
Complete the sentence.
mamaluj [8]

Answer:

We need context, it could be several answers

Explanation:

4 0
4 years ago
Other questions:
  • You would like to arrange the records alphabetically. You should _____.
    7·2 answers
  • An authenticated user can add up to how many computer accounts to the domain, by default
    6·1 answer
  • The strFirstName and strLastName variables contain the strings "Jane" and "Jones," respectively. Which of the following statemen
    14·1 answer
  • This assignment is to read from an input file and process the data for a group of people. Your program calculates the interest a
    5·1 answer
  • Explain what occurs when you synchronize computer and mobile devices.
    5·1 answer
  • Interest assessments are always accurate. True False
    5·1 answer
  • Use the _______ to add a line or circle to your presentation.
    8·1 answer
  • The physical part or components of a computer system called​
    5·2 answers
  • Write a program that inputs a five-digit integer, spearates the integer into its digits and prints them seperated by three space
    6·1 answer
  • What will be displayed after this code segment is run?
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!