Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

12 may assigment 2 (shivraj singh (jiet_cstt_batch_3)) #110

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions 12 may assigment 2/copy_string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//copy string without strcpy

#include <stdio.h>
#include <string.h>

int main()
{
char Str[100], CopyStr[100];
int i;

printf("\n Enter any String : ");
gets(Str);

for (i = 0; Str[i]!='\0'; i++)
{
CopyStr[i] = Str[i];
}
CopyStr[i] = '\0';

printf("\n String that we coped into CopyStr = %s", CopyStr);
printf("\n Total Number of Characters that we copied = %d\n", i);

return 0;
}
//output
//enter any string: happy
//string we copied in copystr = happy
//total number of characters that we copied = 5

23 changes: 23 additions & 0 deletions 12 may assigment 2/coversion_of_no.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Program to convert Decimal to Binary

#include <iostream>
using namespace std;
int main()
{
int a[10], n, i;
cout<<"Enter the number to convert: ";
cin>>n;
for(i=0; n>0; i++)
{
a[i]=n%2;
n= n/2;
}
cout<<"Binary of the given number= ";
for(i=i-1 ;i>=0 ;i--)
{
cout<<a[i];
}
}
//output:
//enter the number to convert:4
//Binary of the given number: 100
26 changes: 26 additions & 0 deletions 12 may assigment 2/palindrome.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Program to Check Whether String is Palindrome

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char str1[20], str2[20];
int i, j, len = 0, flag = 0;
cout << "Enter the string : ";
gets(str1);
len = strlen(str1) - 1;
for (i = len, j = 0; i >= 0 ; i--, j++)
str2[j] = str1[i];
if (strcmp(str1, str2))
flag = 1;
if (flag == 1)
cout << str1 << " is not a palindrome";
else
cout << str1 << " is a palindrome";
return 0;
}

//output:
//Enter the string:abba
//abba is a palindrome
38 changes: 38 additions & 0 deletions 12 may assigment 2/remove_duplicates.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// C++ Program to Delete Repeated Elements

#include<iostream>
using namespace std;
int main ()
{
int A[10], B[10], n, i, j, k = 0;
cout << "Enter size of array : ";
cin >> n;
cout << "Enter elements of array : ";
for (i = 0; i < n; i++)
cin >> A[i];
for (i = 0; i < n; i++)
{
for (j = 0; j < k; j++)
{
if (A[i] == B[j])
break;
}
if (j == k)
{
B[k] = A[i];
k++;
}
}
cout << "Repeated elements after deletion : ";
for (i = 0; i < k; i++)
cout << B[i] << " ";
return 0;
}

//output:
//Enter size of array: 4
//Enter elemnts of array: 11
//32
//45
//11
//repeated elements after deletion : 11 32 45
45 changes: 45 additions & 0 deletions 12 may assigment 2/search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Write a C++ program to search any element in an array.


#include<iostream>
using namespace std;

int main()
{
int arr[10], i, num, n, cnt=0, pos;
cout<<"\n Enter Array Size : ";
cin>>n;
cout<<"\n Enter Array Elements : \n";
for(i=0; i<n; i++)
{
cout<<" ";
cin>>arr[i];
}
cout<<"\n Enter Element to be Searched : ";
cin>>num;
for(i=0; i<n; i++)
{
if(arr[i]==num)
{
cnt=1;
pos=i+1;
break;
}
}
if(cnt==0)
{
cout<<"\n Element Not Found..!!";
}
else
{
cout<<"\n Element "<<num<<" Found At Position "<<pos;
}
return 0;
}


//output:
//enter array size: 4
//enter array elements: 23 45 32 78
//enter elemnts to be searched : 45
//element found at position 1
22 changes: 22 additions & 0 deletions assigment 1 (11 may 2020)/Ques1 (1).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <iostream>
using namespace std;

int findSingle(int ar[], int ar_size)
{
int res = ar[0];
for (int i = 1; i < ar_size; i++)
res = res ^ ar[i];

return res;
}

int main()
{
int ar[] = {2, 3, 5, 4, 5, 3, 4};
int n = sizeof(ar) / sizeof(ar[0]);
cout << "element occuring once is "
<< findSingle(ar , n);
return 0;
}

// output is : element occuring once is 2
Binary file added assigment 1 (11 may 2020)/Ques1 (1).exe
Binary file not shown.
19 changes: 19 additions & 0 deletions assigment 1 (11 may 2020)/Ques4 (1).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>
using namespace std;

int numberOfPaths(int m, int n)
{
if (m == 1 || n == 1)
return 1;

return numberOfPaths(m - 1, n) + numberOfPaths(m, n - 1);
}

int main()
{
cout << numberOfPaths(3, 3);
return 0;
}

// output is 6

Binary file added assigment 1 (11 may 2020)/Ques4 (1).exe
Binary file not shown.
40 changes: 40 additions & 0 deletions assigment 1 (11 may 2020)/Question3 (1).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <bits/stdc++.h>
using namespace std;

int solve(int low, int high, int T)
{
while (low <= high)
{
int mid = (low + high) / 2;

if ((mid * (mid + 1)) == T)
return mid;

if (mid > 0 && (mid * (mid + 1)) > T &&
(mid * (mid - 1)) <= T)
return mid - 1;

if ((mid * (mid + 1)) > T)
high = mid - 1;

else
low = mid + 1;
}
return -1;
}

int main()
{
int T = 15;

int ans = solve(1, T, 2 * T);

if (ans != -1)
ans--;

cout << "Number of stair steps = "
<< ans << endl;
return 0;
}

// output is Number of stair steps = 4
Binary file added assigment 1 (11 may 2020)/Question3 (1).exe
Binary file not shown.
49 changes: 49 additions & 0 deletions assigment 1 (11 may 2020)/qu5 (3).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <bits/stdc++.h>
using namespace std;

double Square(double n, double i, double j)
{
double mid = (i + j) / 2;
double mul = mid * mid;


if ((mul == n) || (abs(mul - n) < 0.00001))
return mid;

else if (mul < n)
return Square(n, mid, j);

else
return Square(n, i, mid);
}

void findSqrt(double n)
{
double i = 1;

bool found = false;
while (!found) {

if (i * i == n) {
cout << fixed << setprecision(0) << i;
found = true;
}
else if (i * i > n) {

double res = Square(n, i - 1, i);
cout << fixed << setprecision(5) << res;
found = true;
}
i++;
}
}

int main()
{
double n = 7;

findSqrt(n);

return 0;
}
//output is 2.4575
Binary file added assigment 1 (11 may 2020)/qu5 (3).exe
Binary file not shown.
38 changes: 38 additions & 0 deletions assigment 1 (11 may 2020)/que2 (1).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <stdio.h>

int PathCounting(int m, int n)
{
int ctr[m][n];
for (int i = 0; i < m; i++)
{
ctr[i][0] = 1;
}
for (int j = 0; j < n; j++)
{
ctr[0][j] = 1;
}
for (int i = 1; i < m; i++)
{
for (int j = 1; j < n; j++)
{
ctr[i][j] = ctr[i-1][j] + ctr[i][j-1];
}
}
return ctr[m-1][n-1];
}

int main()
{
int p,q;
printf("enter no. of rows of matrix ");
scanf("%d",&p);
printf("enter no. of column of matrix ");
scanf("%d",&q);
printf("The size of matrix is : %d, %d\n",p,q);
printf("The all possible paths from top left to bottom right is: %d \n",PathCounting(p,q));
}
// output is
// enter no. of rows of matrix 3
// enter no. of column of matrix 4
// The size of matrix is : 3, 4
// The all possible paths from top left to bottom right is: 10
Binary file added assigment 1 (11 may 2020)/que2 (2).exe
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//copy string without strcpy

#include <stdio.h>
#include <string.h>

int main()
{
char Str[100], CopyStr[100];
int i;

printf("\n Enter any String : ");
gets(Str);

for (i = 0; Str[i]!='\0'; i++)
{
CopyStr[i] = Str[i];
}
CopyStr[i] = '\0';

printf("\n String that we coped into CopyStr = %s", CopyStr);
printf("\n Total Number of Characters that we copied = %d\n", i);

return 0;
}
//output
//enter any string: happy
//string we copied in copystr = happy
//total number of characters that we copied = 5

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Program to convert Decimal to Binary

#include <iostream>
using namespace std;
int main()
{
int a[10], n, i;
cout<<"Enter the number to convert: ";
cin>>n;
for(i=0; n>0; i++)
{
a[i]=n%2;
n= n/2;
}
cout<<"Binary of the given number= ";
for(i=i-1 ;i>=0 ;i--)
{
cout<<a[i];
}
}
//output:
//enter the number to convert:4
//Binary of the given number: 100
Loading