Need a project done?

C++ Programming Developer

Search This Blog

Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

Recursive function to search through array

Recursive function to search through array.

Write a program that asks the user to enter 10 integers, stores these numbers in an array. The program should ask if the user would like to search an element in the array, in which case the program should then prompt the user to enter the number to look for. Use a recursive function find( int index, int target, int array[] ) that does sequential search for a target in this array. Display an appropriate message to the user after searching the element.

Solution:

C++ Program to Search a keyword (in character type array or) through the user entered text.

C++ Program to search through character type array
/*
C++ Program to Search a keyword through the user entered text.
*/









//#include <iostream>

#include <iostream>
using namespace std;

int main()
{
cout << "Write somethig:\n";
char *string = new char [100];
cin.getline (string,100,'\n');

cout << endl << "Search: ";
char *keyword = new char [10];
cin.getline (keyword,10,'\n');

for (int i = 0, j = 0, k = 0, m = 0; string[j] != '\0'; i++,j++)
{
if (keyword[i] == '\0')
{
i = 0;
m = 0;
j = ++k;
}

if ( keyword[i] == string[j] )
{
m++;
}
if (m == strlen(keyword))
{
cout << "FOUND" << endl;
exit (0);
}
}
cout << "Not Found" << endl;
return 0;
}

C++: Write a function that finds the element closest in value from 2-d array to the number entered by user. The row and column indices of the element closest in value to the element should be the output.

/*
Write a function that finds the element closest in value from 2-d array to the number entered by user. The row and column indices of the element closest in value to the element should be the output.
*/



Code:

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

void closest_number_finder(int [][5], int);

int main()
{
cout << "Array:" << endl << endl;
int array[5][5];
for (int r = 0; r<5; r++)
{
for (int c = 0; c<5; c++)
{
array[r][c] = rand()%101 - 50; //Storing random numbers from -50 to 50.
cout << char(179) << setw(3) << array[r][c] << setw(3) << char(179);
}
cout << endl;
}

cout << "\nEnter a Number, I'll tell nearest of this number from my array: ";
int user;
cin >> user;
closest_number_finder(array, user);

return 0;
}

void closest_number_finder(int array[][5], int user)
{
int difference[5][5];

for (int r = 0; r<5; r++)
{
for (int c = 0; c<5; c++)
{
difference[r][c] = fabs(array[r][c] - user);
}
}
int R, C;
//Now we've to find the smallest difference.
int smallest = difference[0][0];
for (r = 0; r<5; r++)
{
for (int c = 0; c<5; c++)
{
if (smallest > difference[r][c])
{
smallest = difference[r][c];
R = r; C = c;
}

}
}

cout << "Closest to your entered number is " << array[R][C] << " at table[" << R << "][" << C << "]" << " with a difference of " << smallest << endl;
}
"Don't let anyone ever make you feel like you don't deserve what you want."