c.
Write a C program that uses functions to perform the following:
1. Addition of Two Matrices
#include<stdio.h>
void matrixAddition(int a[10][10], int b[10][10], int sum[10][10]);
int rows, columns;
void main()
{
int a[10][10], b[10][10], sum[10][10], i, j;
printf("Enter the no of rows and columns:");
scanf("%d%d", &rows, &columns);
/* input first matrix */
printf("Enter the input for first matrix:\n");
for (i = 0; i <rows; i++)
for (j = 0; j <columns; j++)
scanf("%d", &a[i][j]);
/* input second matrix */
printf("Enter the input for second matrix:\n");
for (i = 0; i < rows; i++)
for (j = 0; j< columns; j++)
scanf("%d", &b[i][j]);
/* matrix addtion */
matrixAddition(a, b, sum);
/* print the results */
printf("\nResult of Matrix Addition:\n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < columns; j++)
printf("%d\t", sum[i][j]);
printf("\n";);
}
}
/* adds two matrices and stores the output in third matrix */
void matrixAddition(int a[10][10], int b[10][10], int sum[10][10])
{
int i, j;
for (i = 0; i< rows; i++)
for (j = 0; j < columns; j++)
sum[i][j] = a[i][j] + b[i][j];
}
2. Multiplication of Two Matrices
#include <stdio.h>
void mulmatrix(int [10][10],int [10][10],int,int,int,int);
void main()
{
int m, n, p, q, i,j, a[10][10], b[10][10];
printf("Enter number of rows and columns of first matrix");
scanf("%d%d", &m, & n);
printf("Enter elements of first matrix\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d",&a[i][j]);
printf("Enter number of rows and columns of first matrix");
scanf("%d%d", &p, & q);
if (n != p)
printf("The matrices can't be multiplied with each other.\n");
else
{
printf("Enter elements of second matrix\n");
for (i = 0; i < p; i++)
for (j = 0; j < q; j++)
scanf("%d",&b[i][j]);
mulmatrix(a,b,m,n,p,q);
}
}
void mulmatrix(int a[10][10],int b[10][10],int m,int n,int p,int q)
{
int i,j,k,sum=0,mul[10][10];
for (i = 0; i < p; i++){
for (j = 0; j < q; j++){
for (k = 0; k < p; k++) {
sum = sum + a[i][k]*b[k][j];
}
mul[i][j] = sum;
sum = 0;
}
}
printf("Product of the matrices:\n");
for (i = 0; i < p; i++){
for (j = 0; j < q; j++)
printf("%d" ,mul[i][j]);
printf("\n");
}
}
f. Transpose of a matrix with memory dynamically allocated for the new
matrix as row and column counts may not be the same.
g. Write C programs that use both recursive and non-recursive functions
1.To find the factorial of a given integer.
#include<stdio.h>
long int rfact(int );
void fact(int);
void main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf(" recursive function of Factorial of %d = %ld", n, rfact(n));
fact(n);
}
void fact(int x)
{
int i,f=1;
for(i=1’i<=x;i++)
f=f*I;
printf("Factorial of %d = %d", n, f);
}
long int rfact (int n)
{
if (n==1)
return 1;
else
return n*factorial(n-1);
}
2.To find the GCD (greatest common divisor) of two given integers.
#include <stdio.h>
int recgcd(int x, int y);
int nonrecgcd(int x, int y);
void main()
{
int a, b, c, d;
printf("Enter two numbers a and b");
scanf("%d%d", &a, &b);
c = recgcd(a, b);
printf("The gcd of two numbers using recursion is %d\n", c);
d = nonrecgcd(a, b);
printf("The gcd of two numbers using nonrecursion is %d", d);
}
int recgcd(int x, int y){
if(y == 0){
return(x);
}
else{
return(recgcd(y, x % y));
}
}
int nonrecgcd(int x, int y){
int z;
while(x % y != 0){
z = x % y;
x = y;
y = z;
}
return(y);
}
3.To find x^n
#include <stdio.h>
int rpower(int , int);
void power(int,int)
int main() {
int base, p, result;
printf("Enter base number:\n ");
scanf("%d", &base);
printf("Enter power number(positive integer):\n ");
scanf("%d", &p);
result = rpower(base, p);
printf("%d^%d = %d\n", base, a, result);
power(base,p);
return 0;
}
void power(int base,int p)
{
int i,re=1;
for(i=1;i<=p;i++)
re=re*base;
printf("%d^%d = %d\n", base, a, re);
}
int power(int base, int p) {
if (p != 0)
return (base * power(base, p - 1));
else
return 1;
}
Files:
a. Write a C program to display the contents of a file to standard output
device.
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
char ch;
fp=fopen("abc.txt","r");
if(fp==NULL)
{
printf(“file does not exists”)
exit(0);
}
else
{
while((ch=fgetc(fp)!=EOF)
printf("%c",ch);
}
fclose(fp);
printf("reading completed");
}
b. Write a C program which copies one file to another, replacing all
lowercase characters with their uppercase equivalents.
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int main()
{
FILE *fp1, *fp2;
char ch;
fp1 = fopen("abc.txt", "r");
fp2 = fopen("target.txt", "w");
if ((fp1 == NULL)|| (fp2 == NULL))
{
puts("File does not exist..");
exit(1);
}
while((ch=fgetc(fp1))!=EOF)
{
if(islower(ch))
ch = toupper(ch);
fputc(ch,fp2);
}
printf("\nFile successfully copied..");
return 0;
}
c. Write a C program to count the number of times a character occurs in a
text file. The file name and the character are supplied as command line
arguments.
#include<stdio.h>
#include<stdlib.h>
void main()
{
int count=0;
char sc,ch;
FILE *fp1;
fp1=fopen("source.txt","r");
if(fp1==NULL)
{
puts("Could not open files");
exit(0);
}
printf("enter a search character\n");
scanf("%c",&sc);
printf("entered character is %c\n",sc);
while((ch=fgetc(fp1))!=EOF)
{
if(ch==sc)
count++;
}
printf("\n %c appears %d times in the file\n",sc,count);
fclose(fp1);
return 0;
}
d. Write a C program to merge two files into a third file (i.e., the contents of
the first file followed by those of the second are put in the third file).
#include <stdio.h>
#include <stdlib.h>
void main()
{
// Open two files to be merged
FILE *fp1 = fopen("file1.txt", "r");
FILE *fp2 = fopen("file2.txt", "r");
// Open file to store the result
FILE *fp3 = fopen("file3.txt", "w");
char ch;
if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
{
puts("Could not open files");
exit(0);
}
// Copy contents of first file to file3.txt
while ((c = fgetc(fp1)) != EOF)
fputc(c, fp3);
// Copy contents of second file to file3.txt
while ((c = fgetc(fp2)) != EOF)
fputc(c, fp3);
printf("Merged file1.txt and file2.txt into file3.txt");
fclose(fp1);
fclose(fp2);
fclose(fp3);
}
Miscellaneous:
a. Write a menu driven C program that allows a user to enter n numbers
and then choose between finding the smallest, largest, sum, or average. The
menu and all the choices are to be functions.Use a switch statement to
determine what action to take. Display an error message if an invalid
choice is entered.
b. Write a C program to construct a pyramid of numbers as follows:
1 * 1 1 *
12 ** 23 22 **
123 *** 456 333 ***
**
*
#include <stdio.h>
void main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j)
{
printf("%d ",j);
}
printf("\n");
}
}
Output:
1
12
123
#include <stdio.h>
void main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i)
{
for (j = 1; j <= i; ++j)
{
printf("* ”);
}
printf("\n");
}
Output:
*
**
***
#include <stdio.h>
void main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i)
{
for (j = 1; j <= i; ++j)
{
printf("%d ",i);
}
printf("\n");
}
}
Output:
1
22
333
#include <stdio.h>
void main()
{
int i, j, rows,t=1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("%d ",t++);
}
printf("\n");
}
}
Output:
1
23
456
#include <stdio.h>
void main()
{
int i, j, rows,t=1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
for (i = rows-1; i >= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}}
Output:
*
**
***
**
*