Skip to content

Commit

Permalink
Questions of Functions and arrays module (#259) [HF]
Browse files Browse the repository at this point in the history
* 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
gurpreetk12 authored Oct 27, 2022
1 parent 6d9e00d commit 34ae9d0
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 0 deletions.
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;
}
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);

}
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);
}
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);
}
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;
}
}


0 comments on commit 34ae9d0

Please sign in to comment.