You are on page 1of 6

#include

#include
#include
#include
#include
#include

<iostream>
<stdio.h>
<conio.h>
<string>
<math.h>
<ctype.h>

// Declarations
int main(void);

//Execut

able
void ques1(void);
Search
void ques2_3_18_19(void);
osition
void ques4(void);
Palindrome
void ques5_6(void);
//Max-Min in Array
void ques7_8(void);
//No. of words, digits, alphabets
void ques9(void);
//Binary Search WIP
int bsearch(void);
//I messed up.
void ques10(void);
//2s + (2s+4s) + (2s+4s+6s) + ...
void ques11_12_15(void);
s and (WIP)GCD
void ques(void);
//

//Linear
//Even and Odd P
//String

//Factor

using namespace std;


int main(void) {
//Execut
able
unsigned int ques=0;
std::cout << "01. Array Linear Search. \n";
std::cout << "02. Find sum and Product of alternate elements. \n
";
std::cout << "03. String Reverse and Palindrome Checker. \n";
std::cout << "04. Maximum and Minimum Values in an Array. \n";
std::cout << "05. String Analysis. \n";
std::cout << "06. String Binary Search. WIP\n";
std::cout << "07. Sum to n terms: 2s + (2s+4s) + (2s+4s+6s) + ..
.\n";
std::cout << "08. Find Factors and GCD of 2 numbers. \n";
std::cout << "09. To Reverse a String. \n";
std::cout << " \n";
std::cout << " \n";
std::cout << " \n";
std::cout << "
String functions don't accept input. \n"
;
std::cout << "
Deal with it. \n";
std::cout << " \n";
std::cout << " \n";
std::cout << "\nSelect an option: ";
std::cin >> ques;
switch (ques)

case 1: ques1();
break;
case 2: ques2_3_18_19();
break;
case 3: ques4();
break;
case 4: ques5_6();
break;
case 5: ques7_8();
break;
case 6: ques9();
break;
case 7: ques10();
break;
case 8: ques11_12_15();
break;
/*

case 9: ques14();
break;
case 10:
ques16();
break;
*/
default: std::cout << "Bad choice. Try again. ";

}
getchar();
return 0;
}
void ques1(void)
{
//Linear Search
// clrscr();
int arr[30], h=0, i=0, k=0, f=0;
std::cout << "Enter the number of elements: ";
std::cin >> h;
for (i=0; i<h; ++i)
{
std::cout << "\nEnter value: " << i+1 << ": ";
std::cin >> arr[i];
}
// clrscr();
std::cout << "\n\nEnter the value you'd like to locate: ";
std::cin >> k;
std::cout << "\n";
for (i=0; i<=h; ++i)
{
if (arr[i]==k) {
std::cout << "\nValue found at " << i+1;
++f;
}
}

if (f == 0) {
std::cout << "Value is not in the array. ";
}
}
void ques2_3_18_19(void)
{
d Even Positions
// clrscr();
int arr[30], h=0, e=0, o=0, i=0;
std::cout << "Enter the number of elements: ";
std::cin >> h;
for (i=0; i<h; ++i)
{
std::cout << "\nEnter value " << i+1 << ": ";
std::cin >> arr[i];
}

//Odd an

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


{
if (i%2==0)
{
o += arr[i];
}
else
{
e += arr[i];
}
}
std::cout << "The sum of elements in Even positions is " << e << endl;
std::cout << "The sum of elements in Odd positions is " << o << endl;
}
void ques4(void)
{
//String Palindrome
char s[35], r[35];
int i=0, j=0, f=0, l=0;
std::cout << "Enter a string: ";
getline(s, 35);
for (l=0; s[l] != '\0'; ++l);
for (i=0, j=l; s[i] != '\0'; ++i, --j) {
r[i] = s[j];
}
r[i] = '\0';
std::cout << "The reversed string is: ";
puts(r);
for (i=0; i<l/2; ++i) {
if (s[i] != r[i])
{
++f;
break;
}
}
(f==0)?std::cout << "\n\nThe string is a Palindrome. \n" : std::cout <<
"\n\nThe string is not a Palindrome. \n";
}
void ques5_6(void)
{
//Max-Min in Array
int arr[35], i=0, g=-32764, s=32764, h=0;
std::cout << "Enter the number of elements: ";
std::cin >> h;
for (i=0; i<h; ++i)
{
std::cout << "\nEnter value " << i+1 << ": ";
std::cin >> arr[i];

if (arr[i] < g) {
g = arr[i];
}
if (arr[i] > s) {
s = arr[i];
}
}
// clrscr();
std::cout << "\nThe greatest value is " << g;
std::cout << "\nThe smallest value is " << s;
}
void ques7_8(void)
{
//No. of words, digits, alphabets
char s[55];
int w=0, c=0, v=0, d=0, i=0, l=0, j=0;
std::cout << "Enter a string: ";
std::cin.getline(s, 35);
for (l=0; s[l] != '\0'; ++l);
for (i=0, j=l; i<l/2; ++i, --j) {
if (isalpha(s[i]) == 1) {
switch (s[i]) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U': ++v;
break;
default: ++c;
}
}
else if (isdigit(s[i]) == 1)
{
++d;
}
if (s[i] == ' ' || s[i] == '.') {
++w;
}
}
std::cout << "\n\nWords = " << w << "\nDigits = " << d;
std::cout << "\nVowels = " << v << "\nConsonants = " << c;
}
void ques9(void)
{
//Binary Search WIP
//WIP
}
int bsearch(int a[], int n, int l)
int mid, beg=0, last=n-1;
while (beg <= last)
{
mid = (beg+last)/2;
if (a[mid] == l)
return mid+1;
else if (a[mid] > l)
last = mid-1;

else if (a[mid] < l)


beg = mid+1;
}
return -1;
}
void ques10(void)
{
//2s + (2s+4s) + (2s+4s+6s) + ...
int n=0, i=0, j=0, s=0, t=0;
std::cout << "Enter the number of terms: ";
std::cin >> n;
for (i=0; i <= n; ++i) {
for (j=1; j <= i; ++j) {
t += pow(2*j, 2);
}
s += t;
t=0;
}
std::cout << "\n\nThe value to " << n << " terms is: " << s;
}
void ques11_12_15(void) {
s and GCD
int a=0, b=0, i=0, f=0, aj=0, bj=0, af[45], bf[45], gcd[44];
std::cout << "Enter a number: ";
std::cin >> a;
std::cout << "\nEnter a number: ";
std::cin >> b;
af[45] = bf[45] = 0;
for (i=a/2; i > 0; --i) {
if (a%i == 0) {
af[aj++] = i;
af[45] += i;
++f;
}
if (f==0)
{
std::cout << "\na is a
std::cout << "\nGCD is
goto skip;
}
}
f = 0;
for (i=b/2; i > 0; --i) {
if (b%i == 0) {
bf[bj++] = i;
bf[45] += i;
++f;
}
if (f==0)
{
std::cout << "\nb is a
std::cout << "\nGCD is
goto skip;
}
}
skip: return;
}

Prime number. ";


1. ";

Prime number. ";


1. ";

//Factor

void ques(void) {}

You might also like