0% found this document useful (0 votes)
22 views11 pages

JJ

Uploaded by

fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views11 pages

JJ

Uploaded by

fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

1.

Tesadufi secilmis ededlerden sade ededleri diger massivde cixaran


#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;

void set_arr(int[], int);


void print_arr(int[], int);
int find_primes(int[], int, int[]);

int main(){
int n, p;
cin >> n;
int A[n], P[n];
srand(time(NULL));
set_arr(A, n);

cout << "All numbers: ";


print_arr(A, n);

p = find_primes(A, n, P);
cout << "Prime numbers: ";
print_arr(P, p);

return 0;
}

void print_arr(int arr[], int size){


for(int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}

void set_arr(int arr[], int size){


for(int i = 0; i < size; i++)
arr[i] = rand() % 100;
}

bool is_prime(int num){


if(num<2) return false;
for(int i = 2; i*i <=num; i++){
if (num % i == 0)
return false;
}

int find_primes(int A[], int size, int P[]){


int p = 0;
for(int i = 0; i < size; i++)
if(is_prime(A[i]))
P[p++] = A[i];
return p;
}

2. Ededin extremum reqemleri


#include<iostream>
#include<ctime>
using namespace std;
void extremum_digits(long long, int &, int &);
int main()
{ long long n;
int M=999, m=2024;
cin >> n;
extremum_digits(n, M, m);
cout <<M << " " << m << endl;
return 0;
}
void extremum_digits(long long n, int &M, int &m)
{ M = n % 10; n = n /10; m = M;
do
{ if(n % 10 > M) M = n % 10;
if(n % 10 < m) m = n % 10;
n /= 10;
}while (n > 0);
cout <<M << " " << m << endl;
}

3. BUBBLE SORT

#include<iostream>
using namespace std;
void print_arr(int[], int);
void bubble_sort(int[], int);
int main()
{ int A[] = {48, 23, 31, 18, 15, 39, 34, 40};
int n = sizeof(A) / sizeof(int);
cout <<"Unsorted array : ";
print_arr(A, n);
bubble_sort(A, n);
cout <<"Sorted array : ";
print_arr(A, n);
return 0;
}
void bubble_sort(int arr[], int size)
{ for(int i = 0; i < size; i++)
{ bool unswapped = true;
for(int j = 0; j < size - 1 - i; j++)
if(arr[j] > arr[j+1])
{
swap(arr[j], arr[j+1]);
unswapped = false;
}
if(unswapped) break;
print_arr(arr, size);
}
}

void print_arr(int arr[], int size)


{for(int i = 0; i < size; i++) cout << arr[i] <<" "; cout << endl;}

4. SELECTION SORT
#include<iostream>
using namespace std;
void print_arr(int[], int);
void selection_sort(int[], int);
int main()
{ int A[] = {48, 23, 31, 15, 39, 34, 40, 18};
int n = sizeof(A) / sizeof(int);
cout <<"Unsorted array : ";
print_arr(A, n);
selection_sort(A, n);
cout <<"Sorted array : ";
print_arr(A, n);
return 0;
}
void selection_sort(int arr[], int size)
{
for(int i = 0; i < size; i++)
{
int min_id = i;
for(int j = i + 1; j < size; j++)
if (arr[j] < arr[min_id]) min_id = j;
if(min_id != i) swap(arr[i], arr[min_id]);
}
}

void print_arr(int arr[], int size)


{for(int i = 0; i < size; i++) cout << arr[i] <<" "; cout << endl;}

5. INSERTION SORT
#include<iostream>
using namespace std;
void print_arr(int[], int);
void insertion_sort(int[], int);
int main()
{ int A[] = {48, 23, 31, 18, 15, 39, 34, 40};
int n = sizeof(A) / sizeof(int);
cout <<"Unsorted array : ";
print_arr(A, n);
insertion_sort(A, n);
cout <<"Sorted array : ";
print_arr(A, n);
return 0;
}
void insertion_sort(int arr[], int size)
{
for(int i = 1; i < size; i++)
{
int key = arr[i], j = i - 1;
while(j >= 0 && key < arr[j])
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = key;
}
}

void print_arr(int arr[], int size)


{for(int i = 0; i < size; i++) cout << arr[i] <<" "; cout << endl;}
6. COUNTING SORT
#include<iostream>
using namespace std;
void print_arr(int[], int);
void counting_sort(int[], int);
int get_max(int[], int);
int main()
{ int A[] = {5, 9, 8, 5, 1, 0, 9, 0};
int n = sizeof(A) / sizeof(int);
cout <<"Unsorted array : ";
print_arr(A, n);
counting_sort(A, n);
cout <<"Sorted array : ";
print_arr(A, n);
return 0;
}
int get_max(int arr[], int size)
{
int M = arr[0];
for(int i =1;i < size; i++) if(arr[i] > M) M = arr[i];
return M;
}
void counting_sort(int arr[], int size)
{
int M = get_max(arr, size), a = 0;
int T[M+1]={};
print_arr(T, M+1);
for(int i = 0; i < size; i++) T[arr[i]] += 1;
print_arr(T, M+1);
for(int i = 0; i < M + 1; i++)
for(int j = 0; j <T[i]; j++)
arr[a++] = i;
}

void print_arr(int arr[], int size)


{for(int i = 0; i < size; i++) cout << arr[i] <<" "; cout << endl;}

7. CUMLEDEKI REQEMLERIN CEMI

//input : a8b12 c3 df5 => output: 19


#include <iostream>
using namespace std;
int main()
{
string s;
int w = 0;
getline(cin, s);
for(int i = 0; i < s.length(); i++)

if(isdigit(s[i]) )w = w + s[i] - '0';

cout << w << endl;


}

8. SOZUN CEKISI

//input : ABC, AZ
//output: 6, 27
#include <iostream>
using namespace std;
int main()
{
string s;
int w = 0;
cin >> s;
for(int i = 0; i < s.length(); i++)

w = w + toupper(s[i])- 'A' + 1;

cout << w << endl;


}

9. SOZDEKI HERFLERIN,SIMVOLLARIN VE REQEMLERIN SAYI

#include<iostream>
#include<algorithm>
using namespace std;
int main(){
string s = "30.09 : c++ Programming Language 2024";
int h = 0, b = 0, k = 0, r = 0, x = 0;
for(int i = 0; i < s.length(); i++)
if(isalpha(s[i]))
{ h++;
if(isupper(s[i])) b++;
if(islower(s[i])) k++;
}
else
if(isdigit(s[i])) r++;
else
x++;
cout <<h <<" " << b <<" " << k << " " << r << " "<< x<< endl;
cout << s << endl;
for(int i = 0; i < s.length(); i++)
if(isupper(s[i])) s[i] = tolower(s[i]);
else
if(islower(s[i])) s[i] = toupper(s[i]);
cout << s << endl;
return 0;
}

10.

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main(){
string s1 = "Ali hasanov bmu programming"; //

programming bmu hasanov Ali


s1+=" ";
int k=0,q=0;
for(int i=0; i<s1.length(); i++){
if(s1[i]==' '){
k++;
}
}
k++;
string h[k],d=" ";
q=k;
for(int i=0; i<s1.length(); i++){
d=d+s1[i];
if(s1[i]==' '){
h[q-1]=d;
d="";
q--;
}
}
for(int i=0; i<k; i++){
cout<<h[i];
}

return 0;
}

11.

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main(){
string s1 = "Ali hasanov bmu programming"; //ilA vonasah umb gnimmargorp
int k=0,a=0;
s1=s1+" ";
a=s1.length();
string h;
for(int i=0; i<a; i++){
if(s1[i]!=' '){
h=h+s1[i];
}
else{
reverse(h.begin(),h.end());
cout << h<<" ";
h=" ";
}
}
return 0;
}
12.EDEDLERIN SAYI VE CEMI
#include <iostream>
using namespace std;
int main()
{ string s = "Ali20 BMU 2024 5C++9";
//getline(cin, s);
// s.push_back(' ');
bool found = false;
int say = 0, cem = 0, eded = 0;
for(int i = 0; i < s.length(); i++)
{ if(isdigit(s[i]))
{eded = eded * 10 + s[i] - '0';
found = true;
}
else
{ if(found)
{ cem = cem + eded;
say = say + 1;
eded = 0;
}
found = false;
}
}
if(found) {say = say + 1; cem = cem + eded;}
cout <<"Ededlerin sayi: "<< say << endl;
cout <<"Ededlerin cemi: " << cem << endl;
}
13. EKSTRA BOSLUQLARIN SILINMESI

#include <iostream>
using namespace std;
int main()
{ string s = " Ali hasanov bmu programming ";
//getline(cin, s);

while(s.find(" ")!= -1)


{
int pos = s.find(" ");
s.erase(pos, 1);
}

if(s[0] == ' ') s.erase(0, 1);


if(s[s.length()-1] == ' ') s.erase(s.length()-1, 1);
cout << s << endl;
return 0;
}
14. BAS HERFLER BOYUK
#include<iostream>
#include<ctime>
using namespace std;
int main(){
string s;
getline(cin,s);
string result=" ";
bool cap=true;
for(int i=0;i<s.length();i++){
if(cap && isalpha(s[i])){
result+=toupper(s[i]);
cap=false;
}
if(s[i]==' '){
cap=true;
}
}
cout<<result;
return 0;
}
15. ILK VE SON HERF BOYUK

#include<iostream>
#include<ctime>
using namespace std;
int main(){
string s;
getline(cin,s);
string result=s;
for(int i=0;i<s.length();i++){
if(i==0||s[i-1]==' '||ispunct(s[i-1])){
result[i]=toupper(s[i]);
}
if(i==s.length()-1||s[i+1]==' '||ispunct(s[i+1])){
result[i]=toupper(s[i]);
}
}
cout<<result;
return 0;
}

16. STACK ILE IKILIK SAY SISTEMI


#include <iostream>
#include <stack>
using namespace std;
int main(){
stack <int> s;
int a;
cin>>a;
while(a>0){
s.push(a%2);
a/=2;}

while(!s.empty()){
cout<< s.top();
s.pop();
}

}
17. MOTERIZELER DOGRUDUR YA YOX
#include <iostream>
#include <stack>
using namespace std;
int main(){
stack <char> s;
string moterize;
bool flag = true;
getline(cin,moterize);
for(int i = 0; i < moterize.length();i++)
if(moterize[i] =='(') s.push('(');
else
if(moterize[i] ==')')
{ if(s.empty())
{ flag = false;
break;
}
else
s.pop();
}
if(flag && s.empty()) cout << "Yes\n";
else cout <<"No\n";
return 0;
}
18. MOTERIZELER DOGRUDUR YA YOX 2

// (5+(4-7)+6)
#include <iostream>
#include <stack>
using namespace std;
bool isOpened(char);
bool isClosed(char);
bool correctCloesed(char , char);
int main(){
stack <char> s;
string moterize;
bool flag = true;
getline(cin,moterize);
for(int i = 0; i < moterize.length();i++)
{
if(isOpened(moterize[i])) s.push(moterize[i]);
else
if(isClosed(moterize[i]))
{
if(s.empty())
{
flag = false;
break;"""""""
}
else
if(!correctCloesed(moterize[i], s.top()))
{
flag = false;
break;
}
else
s.pop();
}
}
if(flag && s.empty()) cout << "Yes\n";
else cout <<"No\n";
return 0;
}
bool correctCloesed(char b, char a)
{
if(b==')' && a == '(') return true;
if(b==']' && a == '[') return true;
if(b=='}' && a == '{') return true;
return false;
}
bool isOpened(char ch)"""
{
if(ch =='(' || ch == '[' || ch == '{') return true;
else return false;
}
bool isClosed(char ch)
{
if(ch ==')' || ch == ']' || ch == '}') return true;
else return false;
}

19. POSTFIX I HESABLA (TEK REQEMLILER)


#include<iostream>
#include<stack>
#include<cmath>
// 4+7*6/2 => 4 7 6 * 2 / + = 25
// 8+7-5*2+9/3 => 8 7 + 5 2 * - 9 3 / + = 8
// 4-2*3^4-7*6 => 4 2 3 4 ^ * - 7 6 * - = -200
// 6*(4-2*1^3+5)-3*(8/4+2) => 6 4 2 1 3 ^ * - 5 + * 3 8 4 / 2 + * - = 30
// 8-(9/3^2+1)/6-4 => 8 9 3 2 ^ / 1 + 6 / - 4 -
using namespace std;
bool isOperator(char);
double calc(double, double, char);
int main()
{ string postfix;
stack <double> s;
getline(cin, postfix);
for(int i = 0; i < postfix.length(); i++)
{
if(isdigit(postfix[i])) s.push(postfix[i]-'0');
else if(isOperator(postfix[i]))
{
double b = s.top(); s.pop();
double a = s.top(); s.pop();
s.push(calc(a, b,postfix[i]));
}
}
cout << s.top()<< endl;
}
double calc(double a, double b, char oper)
{
if(oper == '+') return a + b;
if(oper == '-') return a - b;
if(oper == '*') return a * b;
if(oper == '/') return a / b;
if(oper == '^') return pow(a , b);
return 2024;
}
bool isOperator(char ch)
{
if(ch =='+'||ch=='*'||ch=='/' || ch=='-'|| ch =='^')
return true;
else
return false;

20. POSTFIXI HESABLA 1,2,3 REQEMLILER

#include <iostream>
#include <stack>
#include <string>
#include <cmath>
#include <cctype>

using namespace std;

bool isOperator(char);
double calc(double, double, char);

int main()
{
string postfix;
stack <double> s;
getline(cin, postfix);
for(int i = 0; i < postfix.length(); i++)
{
if(isdigit(postfix[i]))
{
double num = 0;
while (i < postfix.length() && isdigit(postfix[i]))
{
num = num * 10 + (postfix[i] - '0');
i++;
}
i--;
s.push(num);
}
else if(isOperator(postfix[i]))
{
double b = s.top(); s.pop();
double a = s.top(); s.pop();
s.push(calc(a, b, postfix[i]));
}
}

cout << s.top() << endl;


}

double calc(double a, double b, char oper)


{
if(oper == '+') return a + b;
if(oper == '-') return a - b;
if(oper == '*') return a * b;
if(oper == '/') return a / b;
if(oper == '^') return pow(a , b);
return 2024;
}

bool isOperator(char ch)


{
if(ch =='+'||ch=='*'||ch=='/' || ch=='-'|| ch =='^')
return true;
else
return false;
}

You might also like