Professional Documents
Culture Documents
a) #include <stdio.h>
void main()
{
int k = 5;
int *p = &k;
int **m = &p;
printf("%d%d%d\n", k, *p, **p);
}
b) #include <stdio.h>
void main()
{
int k = 5;
int *p = &k;
int **m = &p;
printf("%d%d%d\n", k, *p, **m);
}
Options:
a) 5 5 5
b) 5 5 junk value
c) 5 junk junk
d) Run time error
Solution: i) d ii) a
----------------------------------------------------------------2) Write a C program to add 2 numbers without using arithmetic and
logical operators.
Solution:
#include<stdio.h>
int Add(int x, int y){
while (y != 0){
int carry = x & y;
x = x ^ y;
y = carry << 1;
}
return x;
}
int main(){
printf("%d", Add(15, 32));
return 0;
}
-----------------------------------------------------------------
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
// Driver program to test above functions
int main()
{
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr)/sizeof(arr[0]);
quickSort(arr, 0, n-1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Selection Sort:
#include <stdio.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first
element
}
swap(&arr[min_idx], &arr[i]);
}
int main()
{
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
----------------------------------------------------------------5) Write a generic program in C or C++ to print the sequence
#include<bits/stdc++.h>
#define SIZE 10
using namespace std;
int main(void)
{
int L1_a, L2_a, L3_a, L4_a;
int L1_A[SIZE], L2_A[SIZE-1], L3_A[SIZE-2];
int i;
cin >> L1_a >> L2_a >> L3_a >> L4_a;
L3_A[0] = L3_a;
for(i=1;i<SIZE-2;i++)
L3_A[i] = L3_A[i-1] + L4_a;
L2_A[0] = L2_a;
for(i=1;i<SIZE-1;i++)
L2_A[i] = L2_A[i-1] + L3_A[i-1];
L1_A[0] = L1_a;
for(i=1;i<SIZE;i++)
L1_A[i] = L1_A[i-1] + L2_A[i-1];
",L1_A[i]);
",L2_A[i]);
",L3_A[i]);
",L4_a);
return 0;
}
----------------------------------------------------------------6) Two strings str1 and str2 are called isomorphic if there is a one
to one mapping possible for every character of str1 to every character
of str2. And all occurrences of every character in 'str1' map to same
character in 'str2'
Input: str1 = "aab", str2 = "xxy"
Output: 1
'a' is mapped to 'x' and 'b' is mapped to 'y'
Input: str1 = "aab", str2 = "xyz"
Output: 0
One occurrence of 'a' in str1 has 'x' in str2 and other
occurrence of 'a' has 'y'
Solution:
#include<bits/stdc++.h>
using namespace std;
#define MAX_CHARS 256
bool areIsomorphic(string str1, string str2)
{
int m = str1.length(), n = str2.length();
if (m != n)
return false;
bool marked[MAX_CHARS] = {false};
int map[MAX_CHARS];
memset(map, -1, sizeof(map));
}
int main()
{
cout << areIsomorphic("aab", "xxy") << endl;
cout << areIsomorphic("aab", "xyz") << endl;
return 0;
}
-----------------------------------------------------------------