Index of where the number is found or that it didnt

  • No School
  • AA 1
  • 8

This preview shows page 3 - 6 out of 8 pages.

index of where the number is found or that it didn't find the number. Uses Vectors and searching.*/ #include <iostream> #include <iomanip> #include <string> #include <cctype> #include <cstdlib> #include <ctime> #include <cmath> #include <fstream> #include <vector> using namespace std; void search(vector<int>a, int search); //Fixed spelling of search int main() { vector<int>input; int value; int num; cout << "how many numbers to input: "; cin >> num; cout << "Enter the values you want: "; cout << '\n'; for (int i = 0; i<num; i++) { cin >> value; input.push_back(value); } int search1; cout << "search"; //Fixed spelling of search cout << "\n"; cin >> search1; search(input, search1); return 0; } void search(vector<int>a, int search) //Fixed spelling of search {
Image of page 3

Subscribe to view the full document.

bool found = false; int i = 0; while ((i < a.size()) && !found) { if (a[i] == search) //Fixed spelling of search { found = true; } if (found) { cout << "Number " << search << " is at index: " << i << endl; //Fixed spelling of search, added endl } i++; } if (!found) { cout << "value was not found.\n"; } } //Q7 /*Write a program that will take ten words as input and sort them alphabetically using cstrings. Use either an input file or the keyboard to give the program the input. Uses cstrings and array sorting.*/ #include <iostream> #include <iomanip> #include <string> #include <cctype> #include <cstdlib> #include <ctime> #include <cmath> #include <fstream> #include <cstring> //Added cstring //using namespace std is bad practice l using namespace std; const int MAX = 10; void sort(char a[]); //Fixed parameters and added this int main() {
Image of page 4
Char input; Char words[MAX]; cout << "Please enter 10 words: "; cout << "\n"; //Filling Array for (int i = 0; i < 10; i++) { cin.getline(words, MAX); words[i] = input; } sort(words); cout << "Sorted alphabetically: \n"; //Ad for (int u = 0; u < 10; u++) {
Image of page 5

Subscribe to view the full document.

Image of page 6
  • Fall '19
  • Void type, Carriage return

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes