You are on page 1of 18

Amcat Coding Question

1. Rotation of matrix

#include<bits/stdc++.h>
using namespace std;

int** rotatePictureMethod(int row, int column,int arr[10][10], int flag){


int result = (int*)malloc(row * column * sizeof(int));
int m = 0, n = 0;
if(flag == 1){
for(int i = column - 1; i >= 0; i--){
for(int j = 0; j < row; j++){
result[m][n] = arr[j][i];
n++;
}
m++;
}
}
else{
for(int i = 0; i < column; i++){
for(int j = row - 1; j >= 0; j--){
result[m][n] = arr[j][i];
n++;
}
m++;
}
}
return result;
}

int main(){
int arr[10][10];
int row, column, flag;
cin >> row;
cin >> column;
for(int i = 0; i < column; i++){
for(int j = 0; j < row; j++){
cin >> arr[i][j];
}
}
cout << "Enter flag: ";
cin >> flag;
int *p;

*p = rotatePictureMethod(row, column, arr, flag);


for (int i = 0; i < row; i++){
for (int j = 0; j < column; j++){
cout << *(p + i*column + j) << " ";
}
cout << endl;
}

return 0;
}

2.
Swap coresponding elements of aray.
Traversing the array and swapping i and i + 1 element.

#include<bits/stdc++.h>
using namespace std;

void swap(int *p1, int *p2){


//Function swaps two element
int t;
t = *p1;
*p1 = *p2;
*p2 = t;
}

int * swapArr(int *arr, int len){


int *t;
for(int i = 0; i < len - 1; i++){
//Sends a pointer to two coresponding elements
swap(arr + i, arr + i + 1);
}
return arr;
}

int main(){
int n, *p;
//Input size of array
cin >> n;
int arr[n];
for(int i = 0; i < n; i++){
cin >> arr[i];
}
//p gets the pointer to array after swapping
p = swapArr(arr, n);
cout << endl;
for(int i = 0; i < n; i++){
cout << *(arr + i) << " ";
}
cout << endl;
return 0;
}
3. Removing vowels from a string
#include<iostream>
using namespace std;
int main(){
string s, res = "";
cout << "Enter string: ";
//cin.ignore();
cin >> s;
for(int i = 0; i < s.length(); i++){
char c = s[i];
switch(c){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
break;
default:
res += s[i];
}
}
cout << "String = " + res << endl;
}
4. Print the Pattern

1
11
121
1331
14641

#include<bits/stdc++.h>
using namespace std;

void printPascal(int n){


int x = n;
for (int line = 1; line <= n; line++){
for(int j = x; j > 0; j--){
cout << " ";
}
x--;
int C = 1;
for (int i = 1; i <= line; i++){
cout << C << " ";
C = C * (line - i) / i;
}
cout << endl;
}
}

int main(){
int x;
//Input number of rows
cin >> x;
printPascal(x);
return 0;
}

5 . Print the pattern


1*2*3*4
9*10*11*12
13*14*15*16
5*6*7*8

#include <bits/stdc++.h>
using namespace std;
void squarePatternPrint(int n){
int p = n;
for(int i = 1; i <= n; i += 2){
int k = (i - 1) * n + 1;
for(int j = 0; j < n - 1; j++){
cout << k << "*";
k++;
}
cout << k;
cout << endl;
}
if(n % 2 != 0){
p = n - 1;
}
for(int i = p; i > 0; i -= 2){
int k = (i - 1) * n + 1;
for(int j = 0;j < n - 1; j++){
cout << k << "*";
k++;
}
cout << k;
cout << endl;
}
}
int main(){
int x;
cin >> x;
squarePatternPrint(x);
return 0;
}
6. Print the Pattern

Input
n=6
Output
1111112
3222222
3333334
5444444
5555556
7666666

#include <bits/stdc++.h>
using namespace std;
void patternprint(int);

int main(){
int num;
cin>>num;
patternprint(num);
return 0;
}

void patternprint(int num){


int x = 1;
for( int i = 0 ; i < num ; i++ ){
if(i % 2 == 0){
for( int j = 1 ; j <= num ; j++){
cout << x;
}
cout << x + 1;
}
else{
cout << x + 1;
for( int j = 1 ; j <= num ; j++){
cout << x;
}
}
cout << "\n";
x++;
}
}

7. Print the pattern


*
***
*****
***
*
#include<iostream>
#include<map>
using namespace std;

void printPattern(int x){


int a = 1, b = x, c = 1;
//Printing upper half of diamond
while(x--){
for(int j = x; j > 0; j--){
cout << " ";
}
for(int i = 0; i < 2 * a - 1; i++){
cout << "*";
}
a++;
cout << endl;
}
//Printing lower half of diamond
while(b--){
for(int i = 0; i < c; i++){
cout << " ";
}
for(int j = 0; j < 2 * b - 1; j++){
cout << "*";
}
c++;
cout << endl;
}
}

int main(){
int x;
//Input number of rows
cin >> x;
printPattern(x);
return 0;
}

8. Print the Pattern

***********
*********
*******
*****
***
*
#include<iostream>
#include<map>
using namespace std;

void printPattern(int x){


int a = 0;
while(x--){
for(int i = 0; i < a; i++){
cout << " ";
}
for(int j = 0; j < 2 * x + 1; j++){
cout << "*";
}
a++;
cout << endl;
}
}
int main(){
int x;
//Input number of rows
cin >> x;
printPattern(x);
return 0;
}
9. Print the pattern
3
44
555
6666
77777
77777
6666
555
44
3

#include<iostream>
using namespace std;

void printPattern(int n){


for(int i = 0; i < 5; i++){
for(int j = 0; j <= i; j++){
cout << n;
}
n++;
cout << endl;
}
n--;
for(int i = 4; i >= 0; i--){
for(int j = 0; j <= i; j++){
cout << n;
}
n--;
cout << endl;
}
}

int main(){
int x = 3;
printPattern(x);
return 0;
}

10. Print the pattern


1
2*2
3*3*3
3*3*3
2*2
1
#include<iostream>
#include<map>
using namespace std;

void printPattern(int x){


int n = 1;
for(int i = 0; i < x; i++){
for(int j = 0; j <= 2*i; j++){
if(j % 2 == 0){
cout << n;
}
else{
cout << "*";
}
}
n++;
cout << endl;
}
n--;
for(int i = x - 1; i >= 0; i--){
for(int j = 0; j <= 2*i; j++){
if(j % 2 == 0){
cout << n;
}
else{
cout << "*";
}
}
n--;
cout << endl;
}
}

int main(){
int x;
//Input number of rows
cin >> x;
printPattern(x);
return 0;
}

11. Print the pattern


*
***
*****
*******
*********
***********
#include<bits/stdc++.h>
using namespace std;

void printPattern(int x){


int a = 1;
while(x--){
for(int j = x; j > 0; j--){
cout << " ";
}
for(int i = 0; i < 2 * a - 1; i++){
cout << "*";
}
a++;
cout << endl;
}
}

int main(){
int x;
//Input number of rows
cin >> x;
printPattern(x);
}

You might also like