Unit 3_SEE_QP_Programs

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Unit 3: Functions and Arrays

1. DAP to find whether the number is odd or even using function. 8M

#include <stdio.h>
void checkEvenOdd();
int main()
{
int num;
printf(“Enter The Number To Check Even or Odd\n”);
scanf(“%d”, &num);
checkEvenOdd(num);
}

void checkEvenOdd(int num)


{
if(num%2==0)
{
printf(“It is Even\n”);
}
else
{
printf(“It is odd\n”);
}
}

2. DAP to implement linear search. 6M

3. DAP to find the transpose of 3X3 matrix. 8M

4. DAP to print the position of the smallest of ‘n’ numbers using arrays. 6M

# include < stdio.h >


int main( )
{
int a[20], n, small=0, spos, i ;
printf(" Enter the Numbers of terms in Array: ") ;
scanf("%d ", & n) ;
printf("\n Enter the Array of Element : \n") ;
for ( i = 0 ; i < n ; i++ )
{
scanf("%d ", & a[i]) ;
}
small = a[0] ;
for ( i = 0 ; i < n ; i++ )
{
if ( a[i] <= small )
{
small = a[i] ;
spos = i ;
}
}
printf("\n Position of smallest Element is: %d ", spos) ;
printf("\n Smallest Number is : %d ", small) ;
return ( 0 ) ;
}

5. WAP to insert a number at a given location in an array. Trace the program for inserting a
new value in an existing array. 10M

6. WAP to add and subtract elements of two matrices. 8M


#include<stdio.h>
int main()
{
int n, m, i, j, a[10][10], b[10][10], sum[10][10], diff[10][10];
printf("\nEnter the number of rows and columns of the first matrix \n\n");
scanf("%d%d", &m, &n);

printf("\nEnter the elements of the first matrix \n");


for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
scanf("%d", &a[i][j]);

printf("\nEnter the elements of the second matrix \n");


for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
scanf("%d", &b[i][j]);

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


for(j = 0; j < n; j++)
{
sum[i][j] = a[i][j] + b[i][j];
diff[i][j] = a[i][j] - b[i][j];
}

printf("\n\nThe sum of the two entered matrices is: \n\n");


for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
printf("%d\t", sum[i][j]);
}
printf("\n");
}

printf("\n\nThe difference(subtraction) of the two entered matrices is: \n\n");


for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
printf("%d\t", diff[i][j]);
}
printf("\n");
}
return 0;
}

7. Write a function to determine the type of a triangle given its sides to a user defined function.
The function has to return an integer, based on the return value, the calling function prints
the appropriate triangle type. (Types of triangle: Equivalent= All sides are equal, Isosceles=
Any two sides are equal and Scalene = all sides are different) 7M

#include<stdio.h>

int checkTriangle(int x, int y, int z)


{
if (x == y && y == z)
return 1;
else if (x == y || y == z || z == x)
return 2;
else
return 3;
}

int main()
{

int x, y, z,type;
printf("Enter the sides of the triangle");
scanf("%d%d%d",&x,&y,&z);

type=checkTriangle(x, y, z);

if(type==1)
printf("Equilateral Triangle");
else if(type==2)
printf("Isosceles Triangle");
else
printf("Scalene Triangle");
return 0;
}

8. Given an array of size n=6, with the array elements as below: 8M


5 15 22 32 45 65
Identify the search technique that can be applied to find an element in the given array.
Write a C program to search an element and print the position of the element if present.

Solution: Binary Search Program

9. In an Institute there are 5 departments. All Departments organizes 3 events. Write a program
using two-dimensional array to store the total number of participants in each event of their
respective department. Print the total participants in each department and total participants
in each event. 10M

#include <stdio.h>
int main() {
int dept[5][3],i,j,total_par=0;
printf("Enter the data");
for(i=0;i<5;i++)
for(j=0;j<3;j++)
scanf("%d",&dept[i][j]);
for(i=0;i<5;i++)
{
total_par=0;
for(j=0;j<3;j++)
total_par+=dept[i][j];
printf("Total particpants by department %d =%d\n",i,total_par);
}
for(i=0;i<3;i++)
{
total_par=0;
for(j=0;j<5;j++)
total_par+=dept[j][i];
printf("Total particpants in event %d =%d\n",i,total_par);
}
return 0;
}

10. Predict the output of the following C program: 5M


#include<stdio.h>
int main( )
{
int A[5] = { 1,4,7,6,8};
int B[5], i=0,k=0;
for(i=0; i<5; i++)
{
B[k]=A[i]*2 +1 ;
k++;
}
printf("The array 3 elements are:\n" );
for(i=0; i<k ; i++)
printf("%d ", B[i]);
return 0;
}

Output:
The array 3 elements are:
3 9 15 13 17

11. Develop a C program to demonstrate Binary Search algorithm. 8M

12. Write the expected output of the following program. 7M


#include <stdio.h>
int func(int);
int main( )
{
int a=1,b=2;
printf("The values before first call to function : a= %d b=%d\n", a, b);
b=func(a);
printf("The value after first function call :a= %d b=%d\n", a, b);
b*=10;
printf("The values before second call to function: a= %d b=%d\n", a, b);
b=func(b);
printf("The value after second function call a= %d b=%d\n", a, b);
return 0;
}

int func(int a)
{
return ++a;
}

Output:
The values before first call to function : a= 1 b=2
The value after first function call :a= 1 b=2
The values before second call to function: a= 1 b=20
The value after second function call a= 1 b=21

13. DAP to compute the row sum, column sum and the sum of all elements of mXn matrix. 8M

#include <stdio.h>
int main()
{
int m,n,sum=0;
printf("Enter the number of rows and column\n");
scanf("%d %d",&m,&n);
int arr[m][n];
printf("Enter the elements of the matrix\n");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
scanf("%d",&arr[i][j]);
sum+=arr[i][j];
}
}

for(int i=0;i<m;i++)
{
int rsum=0;
for(int j=0;j<n;j++)
{
rsum=rsum+arr[i][j];
}
printf("\nSum of all the elements in row %d is %d\n",i,rsum);
}

for(int i=0;i<m;i++)
{
int csum=0;
for(int j=0;j<n;j++)
{
csum=csum+arr[j][i];
}
printf("\nSum of all the elements in column %d is %d\n",i,csum);
}
printf("Sum of all the elements is %d",sum);
return 0;
}

14. WAP to check the given character is vowel or not using functions (the function should return
a value). 8M
#include <stdio.h>
int isvowel(char ch)
{
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E'
|| ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O'
|| ch == 'u' || ch == 'U')

return 1;
else
return 2;
}
int main()
{
char ch;
int c;
printf("Enter a character\n");
scanf("%c",&ch);
c=isvowel(ch);
if(c==1)
printf("The character %c is a vowel.\n", ch);
else
printf("The character %c is a consonant.\n", ch);
return 0;
}

15. WAP to insert an element in the given array. 6M


16. Analyse the code by filling missing data and predict the probable output. 4M

#include<stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE], size,i;

printf("Enter the size\n");


Scanf(“%d”,&size);
Printf(“Enter the elements”);
for(i = 0; i < size; i++)
scanf("%d", &arr[i]);

printf("The Arrayin reverse order is:\n");


for(i = size-1; i >= 0; i--)
printf("%d\t", arr[i]);

return 0;
}
Output: Program will print the accepted array elements in reverse order.

17. WAP to find out the average of 4 integers using functions. 6M


#include <stdio.h>
void avg()
{
int avg = 0,sum =0,i, a[4];
printf("Enter 4 numbers\n");
for (i=0; i<4;i++)
{
scanf("%d", &a[i]);
sum = sum+a[i];
}
avg = sum/4;
printf("Average of entered numbers is: %d", avg);
}

int main()
{
avg();
return 0;
}

18. Write a program to sum the series – 1/1! +4/2! +27/3! +……. using user defined functions.
i. factorial ( ) -function to calculate the denominator in each term.
ii. display ( ) – function to print the sum of the series.

#include <stdio.h>
int fact(int);
void display(int n);
void main()
{
int n;
printf("Enter the no. of terms required in series");
scanf("%d",&n);
display(n);
}
int fact(int n)
{
int i,fact=1;
for(i=n; i>=1; i--)
fact= fact*i;
return fact;
}
void display(int n)
{
int i;
float sum=0.0f;
for(i=1;i<=n;i++)
sum = sum + ( (float)(i) / (float)(fact(i)) );
printf("\nSum of the series is :: %f\n",sum);
}

19. Given the array elements: 2 4 5 6 8 10 12 14

The array had to store even numbers from 1 to 15. But element 5 is stored in it, which is an odd
number. WAP to remove the element 5 from the array. 6M

Solution: Refer program for deleting element from the given position.

20. Write a function to check whether the given number is a palindrome or not. (function should
return a value to the calling function) 6M
#include<stdio.h>
int checkPalindrome(int number)
{
int temp, remainder, rev=0;
temp = number;

while( number!=0 )
{
remainder = number % 10;
rev = rev*10 + remainder;
number /= 10;
}

if ( rev == temp ) return 0;


else return 1;
}

int main()
{
int number;

printf("Enter the number: ");


scanf("%d", &number);

if(checkPalindrome(number) == 0)
printf("%d is a palindrome number.\n",number);
else
printf("%d is not a palindrome number.\n",number);

return 0;
}

21. Write a program to determine the type of a triangle (Types: equilateral, isosceles and
scalene) given its three sides using four functions. 8M

22. Write a program to store 4X3 matrix. Calculate each row sum and column sum and display
the same. 6M

#include <stdio.h>
int main()
{
int m,n;
printf("Enter the number of rows and column\n");
scanf("%d %d",&m,&n);
int arr[m][n];
printf("Enter the elements of the matrix\n");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
scanf("%d",&arr[i][j]);
}
}

for(int i=0;i<m;i++)
{
int rsum=0, csum=0;
for(int j=0;j<n;j++)
{
rsum=rsum+arr[i][j];
csum=csum+arr[j][i];
}
printf("\nSum of all the elements in row %d is %d\n",i,rsum);
printf("\nSum of all the elements in column %d is %d\n",i,csum);
}
return 0;
}

23. Suppose the following input set is given, where n = 8 and are trying to find the key value 7.
Write an efficient searching program to find the key value. 8M

Solution: Binary Search


24. WAP to update a value at a given location or index in an array. Assume array of size ‘n’
having n values.

25. WAP that prints the location of the number if it exists in the given 1-D sorted array.
Searching shall be performed using binary search algorithm.

26. DAP using functions to convert degrees Fahrenheit into degrees Celsius. 6M

#include <stdio.h>
float convertFahToCels(float fh)
{
return ((fh-32)*5/9);
}
int main()
{
float cel, fah;
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fah);
cel=convertFahToCels(fah);
printf("%.2f Fahrenheit = %.2f Celsius", fah, cel);
return 0;
}

27. Given a sequence of integers, WAP using arrays to find the number of distinct numbers in
the sequence. The sequence need not be sorted. 5M

#include <stdio.h>
int countDistinct(int a[], int n)
{
int i, j, count = 1;
//Traverse the array
for (i = 1; i < n; i++)
{
for (j = 0; j < i; j++)
{
if (a[i] == a[j])
{
break;
}
}
if (i == j)
{
count++;
}
}
return count;
}
int main()
{
int n;
printf("Enter the number of elements \n");
scanf("%d",&n);

int a[n];
printf("Enter the array elements : ");
for (int i=0;i<n;i++)
scanf("%d",&a[i]);
int c= countDistinct(a,n);
printf("The number of distinct elements are %d",c);
return 0;
}

28. DAP to swap the values of two variables using call by value and call by reference. 10M

29. DAP to find the transpose of mxn matrix. 5M

30. DAP to implement linear search. 5M


31. Write C programs to illustrate the working of insertion and deletion operations of an array.
10M

32. Design a C program to multiply two matrices. 7M

#include<stdio.h>
int main()
{
int a[10][10], b[10][10], m[10][10], r,c, i, j, k;

printf("Enter the size of matrix: ");


scanf("%d%d", &r,&c);

printf("Enter the elements of Matrix-A: \n");


for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
scanf("%d", & a[i][j]);
}
}

printf("Enter the elements of Matrix-B: \n");


for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
scanf("%d", & b[i][j]);
}
}

for (i = 0; i < r; i++) {


for (j = 0; j < c; j++) {
m[i][j] = 0;
for (k = 0; k < c; k++) {
m[i][j] += a[i][k] * b[k][j];
}
}
}

printf("The product of the two matrices is: \n");


for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
printf("%d\t", m[i][j]);
}
printf("\n");
}
return 0;
}

33. Develop a C program to search a Book ID from an organized bookshelf which has N number
of Books using appropriate searching technique. 7M
34. A school kid gets homework to check if a given number is prime or not. Help him by writing
a C function for the same. Display the result in the main function. 6M

#include <stdio.h>
int primenumber(int number)
{
int i;
for (i = 2; i <= number / 2; i++)
{
if (number % i != 0)
continue;
else
return 1;
}
return 0;
}

int main()
{
int num, res = 0;
printf("Enter an integer:");
scanf("%d",&num);
res = primenumber(num);
if (res == 0)
printf("%d is a prime number", num);
else
printf("%d is not a prime number", num);
}

35. Write a C code to swap the highest and the lowest element in an array. 7M

#include<stdio.h>
void main()
{
int a[5],max,min,maxpos=0,minpos=0,i,temp;
printf("Enter 5 integers: ");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
max=a[0];
min=a[0];
for(i=1;i<5;i++)
{
if(a[i]>max)
{
max=a[i];
maxpos=i;
}
if(a[i]<min)
{
min=a[i];
minpos=i;
}
}
temp=a[maxpos];
a[maxpos]=a[minpos];
a[minpos]=temp;
printf("After interchange array elemnts are: ");
for(i=0;i<5;i++)
printf("%d ",a[i]);
}

36. Unique IDs are allotted to ‘n’ students of a section. However, by mistake a few IDs are
repeated. Write a program to eliminate these duplicates. 7M

#include<stdio.h>
int main()
{
int a[50],i,j,k, count = 0, dup[50], n;
printf("Enter size of the array");
scanf("%d",&n);
printf("Enter Elements of the array:");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
dup[i] = -1;
}

for(i=0;i<n;i++)
{
for(j = i+1; j < n; j++)
{
if(a[i] == a[j])
{
for(k = j; k <n; k++)
{
a[k] = a[k+1];
}
j--;
n--;
}
}
}
printf("After deleting the duplicate element the Array is:");
for(i=0;i<n;i++){
printf("%d ",a[i]);
}
}
37. Write two C functions that computes the square and cube of a number. Both the functions
should accept parameters and return the calculated value. Print the returned values in the
main. 6M

#include<stdio.h>
int square(int m);
int cube(int n);
void main()
{
int x,s,c;
printf("\n Enter Any Number : ");
scanf("%d",&x);
s=square(x);
c=cube(x);
printf("\n Square of %d is = %d",x,s);
printf("\n\n Cube of %d is = %d",x,c);
}

int square(int m)
{
return(m*m);
}

int cube(int n)
{
return(n*n*n);
}

You might also like