0% found this document useful (0 votes)
5 views

Array methods and function

The document contains several code snippets demonstrating various array methods and functions in programming. It includes functions to calculate the angle between hour and minute hands of a clock, sort an array, reverse a number and an array, insert an element into an array, convert lowercase to uppercase, and check for prime numbers. Each function is accompanied by its implementation in C++.

Uploaded by

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

Array methods and function

The document contains several code snippets demonstrating various array methods and functions in programming. It includes functions to calculate the angle between hour and minute hands of a clock, sort an array, reverse a number and an array, insert an element into an array, convert lowercase to uppercase, and check for prime numbers. Each function is accompanied by its implementation in C++.

Uploaded by

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

Array methods and function

‫ايجاد الزاوية بين الدقيقة والساعة‬


double clAngle(int h, int m)
{
if (h == 12)
h = 0;
if (m == 60)
m = 0;
double hAngle = 0.5 * ((h * 60) + m);
double mAngle = 6 * m;
double ret = abs(hAngle - mAngle);
return min(360 - ret , ret);
}

function to sort
void sorting(int arr[] , int n)
{
int idx;
for (int i = 0 i < n+1; i ++)
idx = i;
for (int j = i + 1; j < n ; j ++)
if (arr[j] < arr[idx]) // (Ascending) and if it >
(descending)
{
idx = j;
swap(arr[idx], arr[i])
}
}
function to reverse.
int reverse(int num)
{
int rn = 0;
while(num > 0)
rn = rn * 10 + num % 10;
num = num / 10;
return rn;
}
insert to array without delate
int *insert(int arr[], int n, int num , int pos)
{
n++;
for (i = n ; i >= pos ; i--)
arr[i] = arr[i-1];
arr[pos - 1] = num;
return arr;
}

lower case to upper case


string str = "ahmed";
str[0] = toupper(str[0]);
cout << str;

toupper()
tolower()
islower()
isupper()

Prime numbers
bool isPrime(int n)
{
if(n == 1 || n == 0) return false;
// Run a loop from 2 to n/2.
for(int i = 2; i <= n / 2; i++)
{
// If the number is divisible by i,
// then n is not a prime number.
if(n % i == 0) return false;
}
// Otherwise n is a prime number.
return true;
}
int main ()
{
int arr[100] ;
for (int i = 0; i < 100 ; i++)
{
arr[i] = i+1;
if (isPrime(arr[i]) )
cout <<"\t"<<arr[i];
}
}

function to reverse Array.


int reverseArr(int arr[], int n)
{
int rn[n];
for (int i = 0; i < n ;i++)
{
rn[i] = arr[n - i - 1]
}
return arr;
}

You might also like