0% found this document useful (0 votes)
76 views7 pages

Paneer Love

The document contains 10 coding problems related to arrays and pointers in C/C++. The problems cover topics like: 1) Finding the minimum cost using arrays 2) Printing patterns using loops 3) String manipulation using pointers 4) Converting numbers to Excel column titles using functions 5) Introduction to pointers by dynamically allocating memory 6) Rotating a 2D matrix by 90 degrees 7) Printing border elements of an array

Uploaded by

Dileep L
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views7 pages

Paneer Love

The document contains 10 coding problems related to arrays and pointers in C/C++. The problems cover topics like: 1) Finding the minimum cost using arrays 2) Printing patterns using loops 3) String manipulation using pointers 4) Converting numbers to Excel column titles using functions 5) Introduction to pointers by dynamically allocating memory 6) Rotating a 2D matrix by 90 degrees 7) Printing border elements of an array

Uploaded by

Dileep L
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

PAGE 7

1. Paneer Love
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

int n;
scanf("%d",&n);
int day[n],pcost[n],i;
for(i=0;i<n;i++)
scanf("%d %d",&day[i],&pcost[i]);

for(i=0;i<n-1;i++)
if(pcost[i]<pcost[i+1])
pcost[i+1]=pcost[i];

int result=0;
for(i=0;i<n;i++)
result=result+(day[i]*pcost[i]);

printf("%d",result);
return 0;
}

2. Number Pattern-9

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

int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
for(int s=1;s<=n-i;s++) printf(" ");
for (int j=1;j<=i;j++) printf("%d",j);
printf("\n");
}
return 0;
}

3. String Conversion:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {

char str[10000];
scanf("%s",str);
int k,flag;
scanf("%d",&k);
if(k==1) printf("%s",str);
else{
for(int i=0;i<strlen(str);i+=(k-1)*2) printf("%c",str[i]);
for (int j=1;j<k-1;j++){
flag=1;
for(int i=j;i<strlen(str);){
printf("%c",str[i]);
if(flag==1) i+=(k-j-1)*2;
else i+=(k-1)*2-(k-j-1)*2;
flag=!flag;
}
}
for(int i=k-1;i<strlen(str);i+=(k-1)*2) printf("%c",str[i]);
}
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}

4. Discover the right character

#include <stdio.h>
static char *convert_To_Excel_Title(int column_no)
{
if (column_no <= 0) {
return "";
}

char *result = malloc(1024);


int len = 0;
do {
result[len++] = ((column_no - 1) % 26) + 'A';
column_no = (column_no - 1) / 26;
} while (column_no > 0);
result[len] = '\0';

int i, j;
for (i = 0, j = len - 1; i < j; i++, j--) {
char c = result[i];
result[i] = result[j];
result[j] = c;
}
return result;
}

int main(void)
{
int n ;
scanf("%d",&n);
printf("%s ",convert_To_Excel_Title(n));

}
5. Dis.the Right Number
PYTHON.3

def titleToNumber(s):
result = 0;
for B in range(len(s)):
result *= 26;
result += ord(s[B]) - ord('A') + 1;

return result;

x = str(input())
print(titleToNumber(x));

6. Intro to Pointers
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int *var = (int*) malloc(sizeof(int));

scanf("%d", var );
printf("%d", *var);
return 0;
}

7. Array Using Pointers


#include<stdio.h>
#include<string.h>
#include<stdlib.h> //this library contains the function malloc.
int main()
{
int i, N, sum = 0;
scanf("%d", &N);
int *arr = (int*) malloc(N*sizeof(int)); //allocate memory to a new int pointer
for (i = 0; i < N; i++) scanf("%d", (arr + i));//can also write &arr[i];
for (i = 0; i < N; i++) sum += *(arr + i); //can also write arr[i];
printf("%d", sum);
return 0;
}

8.2D Arr using Pointers

#include<stdio.h>
#include<string.h>
#include<stdlib.h> //this library contains the function malloc.
int main()
{
int i, j, N,flag=0,flag1=0;
scanf("%d", &N);
int *matrix[N];
for (i = 0; i < N; i++) matrix[i] = (int*) malloc(sizeof(int) * N);
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
scanf("%d", &matrix[i][j]);
}
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
if((i!=j)&&(N-1!=i+j)){
flag1++;
if(matrix[i][j]==0)
flag++;
}}
if(flag==flag1)
printf("yes\n");
else
printf("no\n");

//your code here


return 0;
}

9.2D array using pointer to pointer


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char** str_arr;
char* result; //don't forget to allocate sufficient memory to result string
int i;
//your code here
result=(char*)malloc(sizeof(char)*15000);
str_arr = (char**)malloc(sizeof(char*)*5);
for (i = 0; i < 5; i++)
{
str_arr[i]=(char*)malloc(sizeof(char)*1000);
}
for (i = 0; i < 5; i++)
{
scanf("%s",str_arr[i]);
}

for (i = 0; i < 5; i++)


strcat(result, str_arr[i]);
puts(result);
return 0;
}
10. Relatioship between Array and Pointers:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char *strrev(char* str)
{ int i = 0;
int j = strlen(str) - 1;
int temp;
while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
return str;

}
int main()
{
char *str = (char*) malloc(sizeof(char) * 100);
scanf("%s",str);
str = strrev(str);
puts(str);
return 0;
}

PAGE.8
1. Rotating the Matrix

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

int main()
{
int N;
scanf("%d",&N);
int a[N][N],b[N][N],i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
scanf("%d",&a[i][j]);
for(i=0;i<N;i++)
for(j=0;j<N;j++)
b[i][j]=a[N-j-1][i];
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
printf("%d ",b[i][j]);
printf("\n");
}

/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}

2. Border Elements
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n,b;
scanf("%d %d",&n,&b);
int arr[n],i;
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=b-1;i<n;i+=b)
printf("%d ",arr[i]);
return 0;
}

3. Here Comes Noddy

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

int main()
{
int rows,cols;
scanf("%d %d",&rows,&cols);
int arr[rows][cols],i,j;
for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
scanf("%d",&arr[i][j]);
int rowIndex=0,colIndex=0;
while(rowIndex<rows&&colIndex<cols)
{
for(i=colIndex;i<cols;i++)
printf("%d ",arr[rowIndex][i]);
rowIndex++;//1
for(i=rowIndex;i<rows;i++)
printf("%d ",arr[i][cols-1]);
cols--;//2
if(rowIndex<rows)
{
for(i=cols-1;i>=colIndex;i--)
printf("%d ",arr[rows-1][i]);
rows--;//2
}
if(colIndex<cols)
{
for(i=rows-1;i>=rowIndex;i--)
printf("%d ",arr[i][colIndex]);
colIndex++;//1
}
}

/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}

---Pavan

You might also like