0% found this document useful (0 votes)
5 views

Automata 1

Uploaded by

Nan Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Automata 1

Uploaded by

Nan Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

1) find index of a given element in an array

#include <stdio.h>
int findIndex(int size, int* list, int key)
{
for(int i=0;i<size;i++)
{
if(list(i)==key)
return i;
}
return -1;
}

int main(void) {
int a[]={1,5,2,4,3,7};
int ind=findIndex(6,a,4);
printf("%d",ind);
return 0;
}
Ans: array indexing is done by subscript operator [], not by ()

#include <stdio.h>
int findIndex(int size, int* list, int key)
{
for(int i=0;i<size;i++)
{
if(list[i]==key)
return i;
}
return -1;
}

int main(void) {
int a[]={1,5,2,4,3,7};
int ind=findIndex(6,a,4);
printf("%d",ind);
return 0;
}
2) find the product of all even numbers in a given list

Example:
Input: 6, [1, 4, 3, 2, 6, 5]
Output: 48

#include <stdio.h>
int evenMultiplication(int size, int* arr){
int mul=1;
for(int i=0;i<size;i++) {
if(arr[i]%2==0)
mul=mul*arr[i];
}
return mul;
}

int main(void) {
int a[]={1,4,3,2,6,5};
int mul=evenMultiplication(6,a);
printf("%d",mul);
return 0;
}
3) Find the sum of odd elements in a given range of indices in a list

Example:
Input: 1, 4, [1,5,9,2,4,3,7]
Output: 14

#include <stdio.h>
int oddSum(int start,int end, int* arr)
{
int sum=0;
int i=start;
while(i<=end)
{
if(arr[i]%2!=0)
{
sum+=arr[i++];
}
i++;
}
return sum;
}
int main(void) {
int a[]={1,5,9,2,4,3,7};
int mul=oddSum(1,4,a);
printf("%d",mul);
return 0;
}

Ans: index variable is updated while adding to sum. No need for that

#include <stdio.h>
int oddSum(int start,int end, int* arr){
int sum=0;
int i=start;
while(i<=end)
{
if(arr[i]%2!=0)
{
sum+=arr[i];
}
i++;
}
return sum;
}
int main(void) {
int a[]={1,5,9,2,4,3,7};
int mul=oddSum(1,4,a);
printf("%d",mul);
return 0;
}
4) linear search by recursion
Example1:
Input: [1,5,9,2,4,3,7], 0, 6, 8
Output: -1

Example2:
Input: [1,5,9,2,4,3,7], 0, 6, 4
Output: 4

#include <stdio.h>
int recSearch(int arr[],int l, int r, int x)
{
// Write your code here
}

int main(void) {
int a[]={1,5,9,2,4,3,7};
int ind=recSearch(a,0,7,8);
printf("%d",ind);
return 0;
}
Ans:

#include <stdio.h>
int recSearch(int arr[],int l, int r, int x)
{
if(r<l)
return -1;
if(arr[l]==x)
return l;
if(arr[r]==x)
return r;
return recSearch(arr,l+1,r-1,x);
}

int main(void) {
int a[]={1,5,9,2,4,3,7};
int ind=recSearch(a,0,7,8);
printf("%d",ind);
return 0;
}
5) Iterative C program to reverse a linked list
#include <stdio.h>
#include <stdlib.h>
/* Link list node */
struct Node {
int data;
struct Node* next;
};
/* Function to reverse the linked list */
static void reverse(struct Node** head_ref)
{
struct Node* prev = NULL, *current = *head_ref,* next = NULL;
while (current != NULL) {
// Store next
next = current->next;
// Reverse current node's pointer
current->next = prev;
// Move pointers one position ahead.
prev = current;
current = next;
}
*head_ref = prev;
}
/* Function to push a node */
void push(struct Node** head_ref, int new_data)
{
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}

/* Function to print linked list */


void printList(struct Node* head)
{
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
}
/* Driver program to test above function*/
int main()
{
/* Start with the empty list */
struct Node* head = NULL;
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 85);
printf("Given linked list\n");
printList(head);
reverse(&head);
printf("\nReversed Linked list \n");
printList(head);
getchar();
}
6) 1
11
111
1111
#include <stdio.h>
void patternPrint(int num)
{
int print=1,i,j;
for(i=0;i<num;i++)
{
for(j=0;j<=i;j++);
{
printf("%d",print);
}
printf("\n");
}
}
int main(void) {
patternPrint(4);
return 0;
}
Ans: no ‘;’ after for j loop

#include <stdio.h>
void patternPrint(int num)
{
int print=1,i,j;
for(i=0;i<num;i++)
{
for(j=0;j<=i;j++)
{
printf("%d",print);
}
printf("\n");
}
}
int main(void)
{
patternPrint(4);
return 0;
}
7) Check whether given day is july 5th or not

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

int checkBirthday(char *month, int day)


{
if(strcmp(month,"July")||(day==5))
return 1;
else
return 0;
}

int main(void)
{
int c=checkBirthday("July",07);
printf("%d",c);
return 0;
}
Ans: strcmp return 0 on true case. So equality condition has to be written
fully.
Both month and date has to be matched. So use and operator instead of
or.

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

int checkBirthday(char *month, int day)


{
if(strcmp(month,"July")==0 &&(day==5))
return 1;
else
return 0;
}

int main(void)
{
int c=checkBirthday("July",07);
printf("%d",c);
return 0;
}
8) 11
1111
111111

#include <stdio.h>
void patternPrint(int num)
{
int print=1,i,j;
for(i=1;i<=num;i++)
{
for(j=1;j<=2*i;j++);
{
printf("%d",print);
}
printf("\n");
}
}
int main(void)
{
patternPrint(4);
return 0;
}
Ans: no ‘;’ at the end of j for loop

#include <stdio.h>
void patternPrint(int num)
{
int print=1,i,j;
for(i=1;i<=num;i++)
{
for(j=1;j<=2*i;j++)
{
printf("%d",print);
}
printf("\n");
}
}
int main(void)
{
patternPrint(4);
return 0;
}
9) find product of maximum two numbers out of three numbers
Example:
Input: 5, 2, 7
Output: 35

int multiplyNumber(int numA,int numB,int numC)


{
int result,min,max,mid;
max = (numA>numB) ?numA>numC) ?numA:numC) :
(numB>numC) ? numB:numC);
min = (numA<numB) ? numA<numC) ? numA:numC) :
(numB<numC)?numB:numC);
mid = (numA+numB+numC)-(min+max);
result = (max*int mid);
return result;
}
Ans: paranthesis not properly opened and closed.
int has to be removed during multiplication

int multiplyNumber(int numA,int numB,int numC)


{
int result,min,max,mid;
max = (numA>numB) ? (numA>numC ? numA : numC) :
(numB>numC)?numB:numC;
min = (numA<numB) ? (numA<numC ? numA:numC) :
(numB<numC)?numB:numC;
mid = (numA+numB+numC)-(min+max);
result = (max*mid);
return result;
}
10) find number of occurrences of a given element.

Example:
Input: 7, 4, [1,8,4,9,4,6,4]
Output: 3

int countOccurance(int len,int value,int *arr)


{
int i=0,count =0;
while(i<len){
if(arr[i] == value)
count += 1;
}
return count;
}
Ans: updation of index variable is missed.

int countOccurance(int len,int value,int *arr)


{
int i=0,count =0;
while(i<len){
if(arr[i] == value)
count += 1;
i+=1;
}
return count;
}
11) count number of elements greater than twice the given
element in an array

Example1:
Input: 5, 3, [0, -3, 1, -6, 2]
Output: 0

Example2:
Input: 5, 3, [11, 2, 3, 14, 5]
Output: 2

intcountElement(intsize,intnumK,int *inputList)
{
inti,cou-nt = 0;
for(i=0;i<size;i++)
{
if(inputList[i]>2numK)
cou-nt+=1;
}
returncou-nt;
}
Ans:
2numK - there should be explicit multiplication opertor like 2*numK
cou-nt - is not allowed in variable name.

intcountElement(intsize,intnumK,int *inputList)
{
inti,count = 0;
for(i=0;i<size;i++)
{
if(inputList[i]>2*numK)

count+=1;
}
return count;
}
12) replace all values of an array with 1 if array size is odd.
Otherwise replace with 0.
Example1:
Input: 5, [1, 2, 3, 4, 5]
Output: [1, 1, 1, 1 ,1]

Example2:
Input: 5, [1, 2, 3, 4, 5,6]
Output: [0, 0, 0, 0 ,0,0]

intreplaceValues(intsize,int *inputList)
{
inti,j;
if(size%2==0)
{
i=0;
while(i<size)
{
inputList[i]=0;
i+=2;
}
}
else
{
j=0;
while(j<size)
{
inputList[j]=1;
j+=2;
}
}
}

Ans: updation of index variable is done by 2 instead of 1


#include <stdio.h>
intreplaceValues(intsize,int *inputList)
{
inti,j;
if(size%2==0)
{
i=0;
while(i<size)
{
inputList[i]=0;
i+=1;
}
}
else
{
j=0;
while(j<size)
{
inputList[j]=1;
j+=1;
}
}
}
13) find the sum of odd elements in a matrix whose row
index and column index is equal

Example:
Input: 3, 3, [1,2,3,4,5,6,7,9,8]
Output: 6

#include <stdio.h>
intmatrixSum(int rows, int columns, int matrix[][3]))
{
inti,j,sum=0;
if(row>0&&column>0) {
for(i=0;i<row;i++) {
sum=0;
for(j=0;j<column;j++) {
if(i==j) {
if(matrix[i][j]%2!=0)
sum+=matrix[i][j];
}
}
}
return sum;
}
else
return sum;
}
Ans: row and column variable names are not matching.
Sum=0 to be one time before processing not in loop.

#include <stdio.h>
intmatrixSum(int row, int column, int**matrix)
{
inti,j,sum=0;
if(row>0&&column>0) {
for(i=0;i<row;i++) {
for(j=0;j<column;j++) {
if(i==j) {
if(matrix[i][j]%2!=0)
sum+=matrix[i][j];
}
}
}
return sum;
}
else
return sum;
}
14) sort the given array in descending order

#include <stdio.h>
void sortArray(int len,int *arr)
{
int i,j,max,location,temp;
for(i=0;i<len;i++)
{
max=arr[i];
location=i;
for(j=i;j<len;j++)
{
if(max>arr[j])
{
max=arr[j];
location=j;
}
}
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
}
}
Ans: max variable has to be updated when current element is
greater than max

#include <stdio.h>

void sortArray(int len,int *arr)


{
int i,j,max,location,temp;
for(i=0;i<len;i++)
{
max=arr[i];
location=i;
for(j=i;j<len;j++)
{
if(max>arr[j])
{
max=arr[j];
location=j;
}
}
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
}
}
15) Find the sum of odd elements whose row and column indices are same
Example:
Input: 3, 3, [1,2,3,4,5,6,7,9,8]
Output: 6

#include <stdio.h>
int matrixSum(int row, int column, int matrix[][3])
{
int result=0;
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
if((i==j) || (matrix[i][j]%2!=0))
result*=matrix[i][j];
}
}
if(result<=1)
return result;
else
return result;
}
Ans: two conditions has to be checked. So use and operator
As we have to find sum, use + operator instead of *
If result is lees than or equal to zero then return 0

#include <stdio.h>
int matrixSum(int row, int column, int matrix[][3])
{
int result=0;
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
if((i==j) && (matrix[i][j]%2!=0))
result+=matrix[i][j];
}
}
if(result<=0)
return 0;
else
return result;
}
16) sum of elements twice greater than a given number and stored at
even index positions.

#include <stdio.h>

int countElement(int size,int numK,int *inputList)


{
int i,sum=0;
for(i=0;i<size;i++)
{
if(inputList[i]>2*numK && i/2==0)
sum=inputList[i];
}
return sum;
}
Ans: for even or odd checking use modulus(%) operator not divison
To perform addition use + operator

#include <stdio.h>

int countElement(int size,int numK,int *inputList)


{
int i,sum=0;
for(i=0;i<size;i++)
{
if(inputList[i]>2*numK && i%2==0)
sum+=inputList[i];
}
return sum;
}
17) convert binary to decimal

Example:
Input: 11101
Output: 29

int binarytodecimal(int n)
{
// write your code here
}

Ans: multiply each bit with corresponding 2 power from right to left
and add all those products

(1*(2^0))+(0*(2^1))+ (1*(2^2))+ (1*(2^3))+ (1*(2^4))


1+0+4+8+16
29
#include <stdio.h>
int binarytodecimal(int n)
{
int r,d=0,p=1;
while(n!=0)
{
r=n%10;
d=d+r*p;
n=n/10;
p=p*2;
}
return d;
}
18) factorial of a number

#include <stdio.h>

int main(void) {
long int fact=1,n,i;
scanf("%d",&n);

for(i=1;i<=n;i++)
fact=fact*i;

printf("%d",fact);
return 0;
}
Ans: for long int format specifier is %ld

#include <stdio.h>

int main(void) {
long int fact=1,n,i;
scanf("%d",&n);

for(i=1;i<=n;i++)
fact=fact*i;

printf("%d",fact);
return 0;
}
19) 1111
222
33
4

#include <stdio.h>

int main(void) {
int i,j,n;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d",i);
}
printf("\n");
}
return 0;
}
Ans: for every line no of symbols decreases. So start j loop from
i instead of 1

#include <stdio.h>

int main(void) {
int i,j,n;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
printf("%d",i);
}
printf("\n");
}
return 0;
}
20) check for primality of a number

#include <stdio.h>
int isprime(int n)
{
// write your code here
}

int main(void) {
int i,arr[100],n,m,size=0;

scanf("%d",&n);
for(m=2;m<=n;m++)
{
if(isprime(m))
{
arr[size++]=m;
}
}

for(i=0;i<size;i++)
printf("%d ",arr[i]);
return 0;
}
Ans: a number not having factors in the range (2, n/2) is called
prime number.

#include <stdio.h>

int isprime(int n)
{
int i;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
return 0;
}
return 1;
}
21) biggest of three numbers

#include <stdio.h>

int main(void) {
int n1,n2,n3;

scanf("%d %d %d",&n1,&n2,&n3);

if (n1>n2) && (n1>n3)


{
printf("%d",n1);
}
elseif(n2>n3)
{
printf("%d",n2);
}
else
{
printf("%d",n3);
}
return 0;
}
Ans: condition of if must be enclosed in brackets
else and if are two separate keywords

#include <stdio.h>

int main(void) {
int n1,n2,n3;

scanf("%d %d %d",&n1,&n2,&n3);

if ( (n1>n2) && (n1>n3) )


{
printf("%d",n1);
}
else if(n2>n3)
{
printf("%d",n2);
}
else
{
printf("%d",n3);
}
return 0;
}

You might also like