Lab 6 TO 10
Lab 6 TO 10
Lab 6 TO 10
1.
//Min and max ofa1-D integer array.
#include<stdio.h>
int main()
{
int A[10], i, max, min, n;
printf("Input the number of elements to be stored in the array :");
scanf ("%d", &n) ;
printf("Input %d elements in the array :\n" ,n);
for (i=0;i<n;i++)
scanf(“%d”,&A[i]);
max = A[0];
min = A[0];
for (i=1 ;i<n; i++)
{
if(A[i]>max)
max = A[i];
if (A[i]<min)
min = A[i];
}
printf("%d is maximum and %d is minimum element in the array",max,min);
}
Output:
Output:
Output:
Output:
Enter the binary number :00010011
1's complement of binary number 00010011 is 11101100
2's complement of binary number 00010011 is 11101101
Lab 7: Matrix problems, String operations, Bubble sort
i) Addition of two matrices
ii) Multiplication two matrices
iii) Sort array elements using bubble sort
iv) Concatenate two strings without built-in functions
v) Reverse a string using built-in and without built-in string functions
1.
//Addition of two matrices.
#include<stdio.h>
int main()
{
int r, c, A[10][10], B[10][10], sum[10][10], i, j;
printf("Enter the number of rows: ");
scanf("%d", &r);
printf("Enter the number of columns: ");
scanf("%d", &c);
printf("Enter elements of A matrix: \n");
for (i=0; i< r; i++)
for (j=0; j < c; ++j)
scanf("%d", &A[i][j]);
printf("Enter elements of B matrix: \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++)
sum[i][j] = A[i][j] + B[i][j];
printf("Matrix A is: \n");
for (i=0; i<r; i++)
{
for (j= 0; j < c; j++)
printf("%2d ",A[i][j]);
printf("\n");
}
printf("Matrix B is: \n");
for (i = 0; i<r;++i)
{
for (j= 0; j < c; ++j)
printf( "%2d ", B[i][j]);
printf("\n");
}
printf("Sum of two matrices is: \n");
for (i = 0; i<r;++i)
{
for (j= 0; j < c; ++j)
printf( "%2d ", sum[i][j]);
printf("\n");
}
return 0;
}
Output:
Output:
Input the rows and columns of first matrix:
2
3
Input the rows and columns of second matrix :
3
2
Enter elements of A matrix:
1
2
3
4
5
6
Enter elements of B matrix:
1
2
3
4
5
6
Matrix A is:
1 2 3
4 5 6
Matrix B is:
1 2
3 4
5 6
The multiplication of two matrices is :
22 28
49 64
3.
//Sort 1-D Array
#include<stdio.h>
void main()
{
int arr[100];
int n, i, j, tmp;
printf("Input the size of array :");
scanf("%d", &n );
printf("Input %d elements into the array :\n",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]) ;
Output:
Output:
Enter First String:
RISE
Enter Second String:
GANDHI
Concatenated String is:
RISEGANDHI
5.
//Reverse a string using built-in stringfunctions.
#include<stdio.h>
int main( )
{
char str [20] ;
printf ("Enter a string”\n");
gets (str);
strrev (str);
printf("Reversed string = \n%s", str)
return 0;
}
Output:
Enter a string:
CSE-DS
Reverse of the string is:
SD-ESC
Lab 8: Pointers and structures, memory dereference.
i) Write a C program to find the sum of a 1D array using malloc()
ii) Write a C program to find the total, average of n students using structures
iii) Enter n students data using calloc() and display failed students list
iv) Read student name and marks from the command line and display the student details
alongwith the total.
v) Write a C program to implement realloc()
1.
//Sum of n elements entered by using malloc() function
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i, n, sum = 0;
int *p;
printf("Enter number of elements : ");
scanf( "%d", &n);
p = (int *)malloc (n * sizeof (int) );
printf("Enter %d elements into array:\n",n);
for(i=0;i<n;i++)
scanf ("%d", p + i);
for (i = 0; i< n; i++)
sum += *(p + i);
printf("sum is %d \n", sum);
free (p);
return 0;
}
Output:
2.
// To find the total, average of n students using structures
#include <stdio.h>
#include <stdlib.h>
// Define a structure to store student information
struct student
{
char name[50];
int roll;
float marks;
};
int main()
{
int n, i;
float total = 0.0, avg;
// Ask the user to enter the number of students
printf("Enter the number of students: ");
scanf("%d", &n);
// Allocate memory for an array of structures
struct student *students = (struct student *)malloc(n * sizeof(struct student));
// Check if memory allocation was successful
if (students == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
// Ask the user to enter the details of each student
printf("Enter the details of %d students:\n", n);
for (i = 0; i< n; i++)
{
printf("Student %d:\n", i + 1);
printf("Name: ");
scanf("%s", students[i].name);
printf("Roll: ");
scanf("%d", &students[i].roll);
printf("Marks: ");
scanf("%f", &students[i].marks);
// Add the marks to the total
total += students[i].marks;
}
// Calculate the average by dividing the total by n
avg = total / n;
// Print the results
printf("The total marks of %d students are: %.2f\n", n, total);
printf("The average marks of %d students are: %.2f\n", n, avg);
// Free the memory allocated for the array of structures
free(students);
return 0;
}
Output:
Output:
5.
// realloc() function
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr, i , n1, n2;
printf("Enter size: ");
scanf ("%d", &n1);
ptr = (int*) malloc (n1 * sizeof (int ) );
printf("Addresses of previous ly allocated memory:\n");
for (i = 0; i< n1; i++)
printf("%u \n",ptr + i);
printf("\nEnter the new size: ") ;
scanf ("%d", &n2) ;
ptr = realloc(ptr, n2 * sizeof (int) ) ;
printf( "Addresses of newly allocated memory:\n");
for (i = 0; i< n2; i++)
printf("%u\n", ptr + i);
free (ptr);
return 0;
}
Output:
Enter size: 5
Addresses of previous ly allocated memory:
37149376
37149380
37149384
37149388
37149392
Enter the new size: 4
Addresses of newly allocated memory:
37149376
37149380
37149384
37149388
1.
//Create and display a singly linked list using self-referential structure.
#include <stdio.h>
#include <stdlib.h>
int main() {
// Initialize an empty linked list
struct Node* head = NULL;
return 0;
}
Output:
// Size comparison
printf("Size of Person structure: %zu bytes\n", sizeof(struct Person));
printf("Size of StorageUnit union: %zu bytes\n", sizeof(union StorageUnit));
return 0;
}
Output:
Person:
Name: John
Age: 25
Height: 5.90
return 0;
}
Output:
Initial Value: 219
After Left Rotation: 183
After Right Rotation: 219
4.
//To copy one structure variable to another structure of the same type.
#include <stdio.h>
#include <string.h>
// Define a structure
struct Person
{
char name[50];
int age;
};
int main()
{
// Declare and initialize a structure variable
struct Person person1;
strcpy(person1.name, "John");
person1.age = 25;
Output:
Person 1:
Name: John
Age: 25
Lab 10: Simple functions using call by value, solving differential equations using Eulers
theorem.
i) Write a C function to calculate NCR value.
ii) Write a C function to find the length of a string.
iii) Write a C function to transpose of a matrix.
iv) Write a C function to demonstrate numerical integration of differential equations using
Euler’smethod
1.
// To calculate NCR value.
#include <stdio.h>
// Function to calculate the factorial of a number
int fact(int num)
{
int i,f=1;
if (num == 0 || num == 1)
return 1;
else
for(i=2;i<=num;i++)
f=f*i;
return f;
}
int main()
{
int n, r, ncr;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Enter the value of r: ");
scanf("%d", &r);
ncr = fact(n)/(fact(r)*fact(n-r));
printf("The value of %dC%d is: %d\n", n, r, ncr);
return 0;
}
Output:
Enter the value of n: 5
Enter the value of r: 3
The value of 5C3 is: 10
2.
//Length of a string using function.
#include <stdio.h>
// Function to calculate the length of a string
int calculateStringLength(const char *str)
{
int length = 0;
while (str[length] != '\0')
length++;
return length;
}
int main() {
char inputString[100];
printf("Enter a string: ");
scanf("%[^\n]s", inputString);
int length = calculateStringLength(inputString);
printf("Length of the string: %d\n", length);
return 0;
}
Output:
Enter a string: rise krishnasaigandhi
Length of the string: 23
3.
//Transpose of a matrix using function.
#include <stdio.h>
// Function to calculate the transpose of a matrix using pointers
void calculateTranspose(int rows, int cols, int (*matrix)[cols], int (*result)[rows])
{
for (int i = 0; i< rows; ++i)
for (int j = 0; j < cols; ++j)
result[j][i] = matrix[i][j];
}
// Function to display a matrix using pointers
void displayMatrix(int rows, int cols, int (*matrix)[cols])
{
for (int i = 0; i< rows; ++i)
{
for (int j = 0; j < cols; ++j)
printf("%d\t", matrix[i][j]);
printf("\n");
}
}
int main() {
int rows, cols;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int matrix[rows][cols];
printf("Enter matrix elements:\n");
for (int i = 0; i< rows; ++i)
for (int j = 0; j < cols; ++j)
scanf("%d", &matrix[i][j]);
printf("Original Matrix:\n");
displayMatrix(rows, cols, matrix);
int transposeMatrix[cols][rows];
calculateTranspose(rows, cols, matrix, transposeMatrix);
printf("Transpose Matrix:\n");
displayMatrix(cols, rows, transposeMatrix);
return 0;
}
Output:
Output: