Question

Document2 Tell me ign Layout References Mailings Review View 1. Write a program called Lab18C that searches for a number in a

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

C++ Program:

#include <iostream>
#include <fstream>

using namespace std;

//Search function
bool search(int arr[], int len, int searchElement)
{
int i;

//Iterating over array
for(i=0; i<len; i++)
{
//Comparing elements
if(arr[i] == searchElement)
{
//Element found
return true;
}
}

//Element not found
return false;
}

//Fill array function
void fillArray(int arr[])
{
int i;

//Opening file for reading
fstream fin("Lab18c.txt", ios::in);

//Reading data from file
for(i=0; i<20; i++)
{
//Reading data in to array
fin >> arr[i];
}
}

//Main function
int main()
{
int arr[20]; //Declaring an array of 20 integers
int searchElement;
bool result;

//Reading search element
cout << "\n Enter element to search: ";
cin >> searchElement;

//Fill array
fillArray(arr);

//Calling search function
result = search(arr, 20, searchElement);

//Printing result
cout << "\n Searching for element " << searchElement << ", Value returned from function: " << result << "\n";

//Checking result
if(result)
{
cout << "\n\nElement Found...\n";
}
else
{
cout << "\n\nElement Not Found...\n";
}

return 0;
}

_____________________________________________________________________________________________________________________

Sample Run:

C:\TC\LAB18C\bin\Debug\LAB18C.exe Enter element to search: 23 Searching for element 23, Value returned from function: 1 Eleme

_____________________________________________________________________________________________________________________

Please let me know if you need any further assistance....

Add a comment
Know the answer?
Add Answer to:
Document2 Tell me ign Layout References Mailings Review View 1. Write a program called Lab18C that...
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
  • Document2 Tell me Layout References Mailings Review View 1. Write a program named Lab17B that will...

    Document2 Tell me Layout References Mailings Review View 1. Write a program named Lab17B that will read 2 strings and compare them. a. Create a bool function named compareLetters that will accept 2 string variables as parameters and will return true if the strings have the same first letters and the same last letters. It will return a false otherwise. b. Create a bool function named inside that will accept the 2 string variables in the order they were entered)...

  • | ome Insert Design Layout References Mailings View Help Tell me what you want to do from the l U...

    | ome Insert Design Layout References Mailings View Help Tell me what you want to do from the l Unless you need to edit, it's safer to stay in 11. Write C++ statements that do the following: a. Declare an array alpha of 10 rows and 20 columns of type int. b. Initialize the array alpha to 0. c. Store 1 in the first row and 2 in the remaining rows d. Store 5 in the first column, and make...

  • sert Format Tools Table Window Help Document2 v Search in Docu ign Layout References Mailings Review...

    sert Format Tools Table Window Help Document2 v Search in Docu ign Layout References Mailings Review View Styles Styles Pane The employee credit union at State University is planning the allocation of funds for the coming year. The credit union makes four types of loans to its members. In addition, the credit union invests in risk-free securities to stabilize income. The various revenue producing investments together with annual rates of return are as follows: Type of Loan/Investment Annual Rate of...

  • C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the...

    C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the user for ten (10) grades, and store the data in an array.  Compute the average of all the grades.  Print the original ten grades and the average. a.       Declare an integer array with the name of “grades” of size 10 in the main function. b.      Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array.                                                                i.      The function will receive...

  • **IN C*** * In this lab, you will write a program with three recursive functions you...

    **IN C*** * In this lab, you will write a program with three recursive functions you will call in your main. For the purposes of this lab, keep all functions in a single source file: main.c Here are the three functions you will write. For each function, the output example is for this array: int array[ ] = { 35, 25, 20, 15, 10 }; • Function 1: This function is named printReverse(). It takes in an array of integers...

  • Test 1-PHYS 1402 -AC summer 1 2020 (1) View Tell me ign Layout References Mailings Review...

    Test 1-PHYS 1402 -AC summer 1 2020 (1) View Tell me ign Layout References Mailings Review 12 + A A Аа As 21 AaBbccde Albedde AaB Normal No Spacing Heading 1 Problem 4 for the following circuit. The electric current is 2.00A. The value of each resistor is given on the figure 4 -2.00 A w R - 10.00 Ry - 10.000 V- Ry - 10.000 R - 10.000 This Photo by Unknown Author is licensed under CC.SANC a) What...

  • Grades for Katie Snyder: DHM.3213.30638 Document2 aver Draw Design Layout References Mailings Review View SH alibri...

    Grades for Katie Snyder: DHM.3213.30638 Document2 aver Draw Design Layout References Mailings Review View SH alibri (Bo... 12 A A Aa A EEEE I Ueb X, X APA CE AL Styles Sherri's Tan-O-Rama is a local tanning salon. The following information reflects its number of appointments and total costs for the first half of the year: Month Number of Appointments Total Cost January 475 $ 6,600 February 150 5,300 March 375 6,800 April 300 6,050 May 350 5,950 June 250...

  • Code written in NASM Function called findLargest Write a function called findLargest that receives two parameters:...

    Code written in NASM Function called findLargest Write a function called findLargest that receives two parameters: an unsigned doubleword array and the length of the array. The function must return the value of the largest array member in eax. Preserve all registers (except eax) that are modified by the function. Write a test program in main that calls findLargest three times, each call using a different array with different lengths. Function called countHits Write a function called named countHits that...

  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

  • Write a function template named summarray, with two parameters: and array called are and an int...

    Write a function template named summarray, with two parameters: and array called are and an int holding the number of elements in arr. The function should return the sum of the elements in arr. this template must work for double or int arrays. Show a sample function call to the template function you wrote. What would you have to do to use the templates sumArray function with an array of objects of a custom class?   

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