0% found this document useful (0 votes)
79 views15 pages

CS111

Uploaded by

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

CS111

Uploaded by

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

RECURSION

Question 1
Code:

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

int armstrong (int num, int n);

void main(){
int num;
scanf("%d",&num);
printf("Armstrong number test:\n");
int dnum = num;
int count = 0;
while (dnum){
dnum/=10;
count++;
}
if (num == armstrong(num, count))
printf("Positive\n");
else printf("Negative\n");

int armstrong(int num, int n){


if (num == 0)
return num;
else{
return pow(num%10,n) + armstrong(num/10,n);
}
}

Output:
Question 2
Code:

#include <stdio.h>

float series_sum (int);


int fact (int);

void main(){
int n;
scanf("%d",&n);
if (n%2)
printf("%f\n",series_sum(n));
else printf("Invalid entry\n");
}

float series_sum(int n){


if (n == 1)
return 1;
else
return (1.0/fact(n) + series_sum(n-2));
}

int fact(int n){


if (n == 1)
return 1;
else
return n*fact(n-1);
}

Output:

1. It outputs the sum 1 + 1/3!


2. It outputs the sum 1 + 1/5!
3. It shows error message as even numbers are not included in the series

Question 3
Code:

#include <string.h>
#define SIZE 100

int countFN(char*);

void main(){
char s[SIZE];
scanf("%[^\n]s",s);
printf("%d\n",countFN(s));
}

int countFN(char* s){


if (!*s)
return 0;
char consonants[] = "qwrtypsdfghjklzxcvbnm";
static int flagarray[21] = {0};
int i = 0;
int flag = 0;
int newflag = 1;
int index = 0;

while (*(consonants+i)){
if ((*s == *(consonants+i)) || (*s == *(consonants+i)-
32)){
flag = 1;
flagarray[i]++;
break;
}
i++;
}

if (flag)
if (*(flagarray+i) == 1)
printf("%c, ",*s);

for (i=0; i<strlen(s)-1; i++)


*(s+i) = *(s+i+1);
*(s+i) = 0;

return countFN(s) + flag;

Output:
It doesn’t print duplicates, but does count them.
Question 4
Code:

#include <stdio.h>
#include <string.h>
#define SIZE 100

int palindrome(char*);

void main(){
char string[SIZE];
scanf("%[^\n]s",string);
printf("%d\n",palindrome(string));
}

int palindrome(char* s){


if (strlen(s) < 2){
return 1;
}
else if (*s == *(s+strlen(s)-1)){
for (int i=1; i<strlen(s); i++)
*(s+i-1) = *(s+i);
*(s+strlen(s)-2) = 0;
return palindrome(s);
}
else
return 0;
}

Output:
1 for positive and 0 for negative result

Question 5
Code:

#include <stdio.h>
#define SIZE 100

int list[SIZE];
int key;
int binarysearch(int, int);

void main(){
int length = 0;
char ch = 0;

while (ch!='\n'){
scanf("%d",list+length++);
ch = getchar();
}

scanf("%d",&key);

int flag = 1;

for (int i=0; i<length; i++)


if (key == *(list+i))
flag = 0;

if (flag){
printf("Invalid key\n");
return;
}

printf("%d\n",binarysearch(0, length));
}

int binarysearch(int low, int high){


int mid = (high+low)/2;
if (key == *(list+mid)){
return mid + 1;
}
else if (key>*(list+mid)){
return binarysearch(mid, high);
}
else{
return binarysearch(low, mid);
}
}

Output:

Since non existence of key would result in a stack overflow.

POINTERS

Question 1
Code:
#include <stdio.h>
#include <math.h>

void swap(int*,int*);
void main(){
int a,b;
scanf("%d%d",&a,&b);
printf("Before\na[%p] = %d\nb[%p] = %d\n",&a,a,&b,b);
swap(&a,&b);
printf("After\na[%p] = %d\nb[%p] = %d\n",&a,a,&b,b);
}

void swap(int* a, int* b){


int temp = *a;
*a = *b;
*b = temp;
}

Output:

Question 2
Code:

#include <stdio.h>
#define SIZE 100

int countFN(char*);
void main(){
char s[SIZE];
scanf("%[^\n]s",s);
int count = 0;
char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
int j = 0;
for (int i=0; i<26; i++){
count = 0;
j = 0;
while (*(s+j)){
if ((*(alphabet+i) == *(s+j)) || (*(alphabet+i)-32
== *(s+j))){
count++;
}
j++;
}
if (count){
printf("%c | ",*(alphabet+i));
for (int i=0; i<count; i++) printf("|");
printf("\n");
}
}
}

Output:
Question 3

Code:

#include <stdio.h>
#define SIZE 100

void main(){
char s[SIZE];
scanf("%[^\n]s",s);
int j = 0;
while (*(s+j)){
if (*(s+j) == 32){
*(s+j) = 10;
}
j++;
}
printf("%s\n",s);
}

Output:
Question 4
Code:

#include <stdio.h>

void main(){
int rows, columns;
int sum;
scanf("%d%d",&rows,&columns);
int matrix[rows][columns];
for (int i=0; i<rows; i++)
for (int j=0; j<columns; j++)
scanf("%d",&matrix[i][j]);
for (int i=0; i<rows; i++){
sum = 0;
for (int j=0; j<columns; j++)
sum += *(matrix[i]+j);
printf("Row %d:%d\n",i+1,sum);
}
for (int j=0; j<columns; j++){
sum = 0;
for (int i=0; i<rows; i++)
sum += *(matrix[i]+j);
printf("Column %d:%d\n",j+1,sum);
}
}

Output:
Question 5
Code:

#include <stdio.h>
#define SIZE 100

void main(){
int m,n;
scanf("%d%d",&m,&n);

int arr1[m+n], arr2[n];


for (int i=0; i<m; i++)
scanf("%d",arr1+i);
for (int i=0; i<n; i++)
scanf("%d",arr2+i);
for (int i=m; i<m+n; i++){
*(arr1+i) = *(arr2+i-m);
}
printf("Unsorted combined array:\n");
for (int i=0; i<m+n; i++)
printf("%d ",arr1[i]);
int temp;
for (int i=m+n-1; i>=0; i--)
for (int j=0; j<=i; j++)
if (arr1[j] > arr1[j+1]){
temp = *(arr1+j+1);
*(arr1+j+1) = *(arr1+j);
*(arr1+j) = temp;
}
printf("\nSorted combined array:\n");
for (int i=0; i<m+n; i++)
printf("%d ",arr1[i]);
printf("\n");
}

Output:

Question 6

I have written the functions into a custom library file named instring.h for “indigeneous string” and
made use of a custom constants file named inconstants.h for “indigeneous constants”.

Code:

inconstants.h:

const int size = 15;

instring.h:

#include "inconstants.h"

char* instrcat(char* str_1, char* str_2){


int len1 = 0, len2 = 0;
while (str_1[len1++ + 1]);
while (str_2[len2++ + 1]);

int index;
for (index = len1; index < size; index++){
if (index - len1 < len2){
*(str_1 + index) = *(str_2 + index - len1);
}
else break;
}
str_1[index] = 0;
return str_1;
}

int instrcmp(char* str_1, char* str_2){


int len1 = 0, len2 = 0;
while (str_1[len1++ + 1]);
while (str_2[len2++ + 1]);
if (len1 == len2){
for (int i = 0; i < len1; i++){
if (str_1[i] < str_2[i]) return -1;
else if (str_1[i] > str_2[i]) return 1;
}
}
return 0;
}

int instrlen(char* str){


int len = 0;
while (str[len++ + 1]);
return len;
}

char* instrcpy(char* str_1, char* str_2){


int len2 = 0;
while (str_2[len2++ + 1]);
int index;
for (index = 0; index < size; index++){
if (str_2[index] == 0){
break;
}
str_1[index] = str_2[index];
}
str_1[index] = 0;
return str_1;
}

char* instrrev(char* str){


int len = 0;
while (str[len++ + 1]);
printf("%d\n",len);
int index;
char temp;
for (index = 0; index < len/2; index++){
temp = str[len - index - 1];
str[len - index - 1] = str[index];
str[index] = temp;
}
str[len] = 0;
return str;
}

q6.c:

#include <stdio.h>
#include "instring.h"

void main(){
int choice;
char str_1[size];
char str_2[size];

printf("Enter\n1 for concatenation (string 2 added to string


1)\n"
"2 for comparison\n3 for counting length\n4 for copying
(string 2"
"copied to the location of\nstring 1)\n5 for reversing\n\n");

scanf("%d",&choice);
printf("Enter string 1: ");
scanf("%s",str_1);
printf("Enter string 2: ");
scanf("%s",str_2);

switch (choice){
case 1:
printf("%s\n",instrcat(str_1,str_2));
break;
case 2:
printf("%d\n",instrcmp(str_1,str_2));
break;
case 3:
printf("%d\n",instrlen(str_1));
printf("%d\n",instrlen(str_2));
break;
case 4:
printf("%s\n",instrcpy(str_1,str_2));
break;
case 5:
printf("%s\n",instrrev(str_1));
printf("%s\n",instrrev(str_2));
break;
default:
printf("Invalid choice\n");
}
}
Output:

You might also like