-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Questions of Functions and arrays module (#259) [HF]
* create hello.cpp created hello.cpp * Week 1st programs * Getting started module * Patterns * 5 questions of Functions And Arrays * Problems of Function and Array Module * 5 questions of Function And Array
- Loading branch information
1 parent
6d9e00d
commit 34ae9d0
Showing
5 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
IOT/gurpreetk12_gurpreetkaur_2125cs1094_2/FunctionandArrays/AnyBaseToDecimal.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include<iostream> | ||
#include<math.h> | ||
using namespace std; | ||
int AnyToDec(int n, int b) { | ||
int g=n; | ||
int p=0,i=0,sum=0; | ||
int q=g; | ||
while(g!=0) | ||
{ | ||
q=g%10; | ||
p=pow(b,i); | ||
|
||
i++; | ||
g=g/10; | ||
sum=sum+(p*q); | ||
} | ||
return(sum); | ||
|
||
} | ||
int main() { | ||
int n; | ||
int b; | ||
int sum=0,h=0,g=0,i=0; | ||
cin >> n; | ||
cin >> b; | ||
int res = AnyToDec(n, b); | ||
cout << res << endl; | ||
} |
31 changes: 31 additions & 0 deletions
31
IOT/gurpreetk12_gurpreetkaur_2125cs1094_2/FunctionandArrays/Inverseofanarray.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include<iostream> | ||
using namespace std; | ||
|
||
int* inverse(int* arr, int n){ | ||
for(int i=0;i<n;i++) | ||
{ | ||
for(int j=0;j<n;j++) | ||
{ | ||
if(i==arr[j]) | ||
{ | ||
cout<<j<<"\n"; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
int main(){ | ||
int n; | ||
cin>>n; | ||
int* arr = new int[n]; | ||
for(int i = 0 ; i < n; i++){ | ||
cin>>arr[i]; | ||
} | ||
|
||
int* inv = inverse(arr,n); | ||
//display(inv,n); | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
IOT/gurpreetk12_gurpreetkaur_2125cs1094_2/FunctionandArrays/ReverseAnArray.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include<iostream> | ||
using namespace std; | ||
|
||
void reverse(int* arr, int n){ | ||
for(int i=n-1;i>=0;i--) | ||
{ | ||
cout<<arr[i]<<" "; | ||
} | ||
|
||
} | ||
int main(){ | ||
int n; | ||
cin>>n; | ||
|
||
int* arr = new int[n]; | ||
for(int i = 0 ; i < n; i++){ | ||
cin>>arr[i]; | ||
} | ||
reverse(arr,n); | ||
} |
72 changes: 72 additions & 0 deletions
72
IOT/gurpreetk12_gurpreetkaur_2125cs1094_2/FunctionandArrays/RotateAnArray.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include<iostream> | ||
using namespace std; | ||
|
||
void rotate(int* arr, int n, int k){ | ||
int r=k; | ||
if(r>0) | ||
{ | ||
if(r<n) | ||
{for(int i=n-k;i<n;i++) | ||
{ | ||
cout<<arr[i]<<" "; | ||
} | ||
for(int i=0;i<n-k;i++) | ||
{ | ||
cout<<arr[i]<<" "; | ||
}} | ||
else | ||
{ | ||
k=r%n; | ||
for(int i=n-k;i<n;i++) | ||
{ | ||
cout<<arr[i]<<" "; | ||
} | ||
for(int i=0;i<n-k;i++) | ||
{ | ||
cout<<arr[i]<<" "; | ||
} | ||
} | ||
} | ||
else | ||
{ k=k*-1; | ||
if(k<n) | ||
{for(int i=k;i<n;i++) | ||
{ | ||
cout<<arr[i]<<" "; | ||
} | ||
for(int i=0;i<k;i++) | ||
{ | ||
cout<<arr[i]<<" "; | ||
}} | ||
else | ||
{ | ||
k=k%n; | ||
for(int i=k;i<n;i++) | ||
{ | ||
cout<<arr[i]<<" "; | ||
} | ||
for(int i=0;i<k;i++) | ||
{ | ||
cout<<arr[i]<<" "; | ||
} | ||
|
||
} | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
|
||
int main(){ | ||
int n, r; | ||
cin>>n; | ||
int* arr = new int[n]; | ||
for(int i = 0 ; i < n; i++){ | ||
cin>>arr[i]; | ||
} | ||
cin>>r; | ||
|
||
rotate(arr,n,r); | ||
//display(arr,n); | ||
} |
55 changes: 55 additions & 0 deletions
55
IOT/gurpreetk12_gurpreetkaur_2125cs1094_2/FunctionandArrays/SumOfTwoArrays.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include<iostream> | ||
using namespace std; | ||
|
||
int main(){ | ||
int n1, n2; | ||
cin>>n1; | ||
int* a1 = new int[n1]; | ||
for(int i = 0 ; i < n1; i++){ | ||
cin>>a1[i]; | ||
} | ||
|
||
cin>>n2; | ||
int* a2 = new int[n2]; | ||
for(int i = 0 ; i < n2; i++){ | ||
cin>>a2[i]; | ||
} | ||
|
||
int m = max(n1,n2); | ||
int* ans = new int[m]; | ||
|
||
int i = n1 - 1; | ||
int j = n2 - 1; | ||
int k = m - 1; | ||
int carry = 0; | ||
|
||
while(k >= 0){ | ||
int sum = carry; | ||
if(i >= 0){ | ||
sum += a1[i]; | ||
} | ||
|
||
if(j >= 0){ | ||
sum += a2[j]; | ||
} | ||
|
||
int q = sum / 10; | ||
int r = sum % 10; | ||
|
||
ans[k] = r; | ||
carry = q; | ||
i--; | ||
j--; | ||
k--; | ||
} | ||
|
||
if(carry != 0){ | ||
cout<<carry<<endl; | ||
} | ||
|
||
for(int i = 0 ; i < m; i++){ | ||
cout<<ans[i]<<endl; | ||
} | ||
} | ||
|
||
|