Data Structures Programmes, Lab Reports of Data Structures and Algorithms

Data Structure programs are written with question,answer and output

Typology: Lab Reports

2019/2020

Available from 12/26/2021

sneha-dr-1
sneha-dr-1 🇮🇳

1 document

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Partb
1.Write a c program to find the length of string without using built in function
#include <stdio.h>
void main()
{
char string[50];
int i, length = 0;
printf("Enter a string \n");
gets(string);
for (i = 0; string[i] != '\0'; i++)
{
length++;
}
printf("The length of a string is the number of characters in it \n");
printf("So, the length of %s = %d\n", string, length);
}
Output:
Enter a string
Sanfoundry
The length of a string is the number of characters in it
So, the length of Sanfoundry = 10
2.write a c program to demonstrate string functions.
#include <stdio.h>
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download Data Structures Programmes and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

Partb 1.Write a c program to find the length of string without using built in function #include void main() { char string[50]; int i, length = 0; printf("Enter a string \n"); gets(string); for (i = 0; string[i] != '\0'; i++) { length++; } printf("The length of a string is the number of characters in it \n"); printf("So, the length of %s = %d\n", string, length); } Output: Enter a string Sanfoundry The length of a string is the number of characters in it So, the length of Sanfoundry = 10 2.write a c program to demonstrate string functions. #include

#include int main() { char nickname[20]; printf("Enter your Nick name:"); scanf("%s", nickname); printf("%s",nickname); return 0; } Output: Enter your Nick name:Negan Negan 3.write a c program to demonstrate pointers in c. #include int main () { int var1; char var2[10]; printf("Address of var1 variable: %x\n", &var1 ); printf("Address of var2 variable: %x\n", &var2 ); return 0; } Output: Address of var1 variable: bff5a Address of var2 variable: bff5a3f

Enter the number to check prime: Number is prime 5.write a c program to read,display and to find the trace of a square matrix

include

main( ) { int a[10][10], m,i,j, sum; printf ("\n Enter order of the square matrix :") ; scanf ("%d", &m); printf ("\n Enter the matrix \n"); for( i=0; i

  1. write a c program to read,display and add two m x n matrices using function #include #include void main() { int a[2][3],b[2][3],c[2][3],i,j; clrscr(); printf("\nENTER VALUES FOR MATRIX A:\n"); for(i=0;i<2;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("\nENTER VALUES FOR MATRIX B:\n"); for(i=0;i<2;i++) for(j=0;j<3;j++) scanf("%d",&b[i][j]); for(i=0;i<2;i++) for(j=0;j<3;j++) c[i][j]=a[i][j]+b[i][j];

void display(int mult[][10], int rowFirst, int columnSecond); int main() { int firstMatrix[10][10], secondMatrix[10][10], mult[10][10], rowFirst, columnFirst, rowSecond, columnSecond, i, j, k; printf("Enter rows and column for first matrix: "); scanf("%d %d", &rowFirst, &columnFirst); printf("Enter rows and column for second matrix: "); scanf("%d %d", &rowSecond, &columnSecond); while (columnFirst != rowSecond) { printf("Error! column of first matrix not equal to row of second.\n"); printf("Enter rows and column for first matrix: "); scanf("%d%d", &rowFirst, &columnFirst); printf("Enter rows and column for second matrix: "); scanf("%d%d", &rowSecond, &columnSecond); } enterData(firstMatrix, secondMatrix, rowFirst, columnFirst, rowSecond, columnSecond); multiplyMatrices(firstMatrix, secondMatrix, mult, rowFirst, columnFirst, rowSecond, columnSecond); display(mult, rowFirst, columnSecond);

return 0; } void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond) { int i, j; printf("\nEnter elements of matrix 1:\n"); for(i = 0; i < rowFirst; ++i) { for(j = 0; j < columnFirst; ++j) { printf("Enter elements a%d%d: ", i + 1, j + 1); scanf("%d", &firstMatrix[i][j]); } } printf("\nEnter elements of matrix 2:\n"); for(i = 0; i < rowSecond; ++i) { for(j = 0; j < columnSecond; ++j) { printf("Enter elements b%d%d: ", i + 1, j + 1); scanf("%d", &secondMatrix[i][j]); } }

void display(int mult[][10], int rowFirst, int columnSecond) { int i, j; printf("\nOutput Matrix:\n"); for(i = 0; i < rowFirst; ++i) { for(j = 0; j < columnSecond; ++j) { printf("%d ", mult[i][j]); if(j == columnSecond - 1) printf("\n\n"); } } } Output: Enter rows and column for first matrix: 3 2 Enter rows and column for second matrix: 3 2 Error! column of first matrix not equal to row of second. Enter rows and column for first matrix: 2 3 Enter rows and column for second matrix: 3 2 Enter elements of matrix 1: Enter elements a11: 3 Enter elements a12: - 2 Enter elements a13: 5 Enter elements a21: 3

Enter elements a22: 0 Enter elements a23: 4 Enter elements of matrix 2: Enter elements b11: 2 Enter elements b12: 3 Enter elements b21: - 9 Enter elements b22: 0 Enter elements b31: 0 Enter elements b32: 4 Output Matrix: 24 29 6 25 8.Write a C program to read a string and to find the number of digits, vowels, consonants and spaces #include #include int isVowel(char c); int isConsonant(char c); int isDigit(char c); int isWhitespace(char c);

} else { return 0; } } int isConsonant(char c) { if(((c>='a'&& c<='z') || (c>='A'&& c<='Z')) && !isVowel(c)){ return 1; } else { return 0; } } int isDigit(char c) { if(c>='0'&&c<='9'){ return 1; } else { return 0; } } int isWhitespace(char c) { if(c == ' '){ return 1; } else {

return 0; } } Output Enter a string: C is my 1st programming language Vowels: 8 Consonants: 18 Digits: 1 White spaces: 5 9.Write a C program to find reverse a string using pointer. #include int string_length(char); void reverse(char); main() { char string[100]; printf("Enter a string\n"); gets(string);

begin++; end--; } } int string_length(char *pointer) { int c = 0; while( *(pointer+c) != '\0' ) c++; return c; } Output: Enter a string: Yapph eb The reverse order of entered string is “Be Happy”. 10.Write a C program to swap two numbers using pointers. #include void swap(int *x,int *y) {

int t; t = *x; *x = *y; *y = t; } int main() { int num1,num2; printf("Enter value of num1: "); scanf("%d",&num1); printf("Enter value of num2: "); scanf("%d",&num2); printf("Before Swapping: num1 is: %d, num2 is: %d\n",num1,num2); swap(&num1,&num2); printf("After Swapping: num1 is: %d, num2 is: %d\n",num1,num2); return 0; } Output: Enter value of num1: 56 Enter value of num2: 67 Before swapping num1 is 56 and num2 is 67. After swapping num1 is 67 and num2 is 56.

Output: Enter details of students: Name: Mike Roll No : Percentage :89. Entered details: Name:Mike RollNo: 101 Percentage: 89. 12.Write a C program to demonstrate the difference between structure and union. #include #include struct struct_example { int integer; float decimal; char name[20]; }; union union_example { int integer; float decimal; char name[20]; };

void main() { struct struct_example stru ={5, 15, "John"}; union union_example uni = {5, 15, "John"}; printf("data of structure:\n integer: %d\n decimal: %.2f\n name: %s\n", stru.integer, stru.decimal, stru.name); printf("\ndata of union:\n integer: %d\n" "decimal: %.2f\n name: %s\n", uni.integer, uni.decimal, uni.name); printf("\nAccessing all members at a time:"); stru.integer = 163; stru.decimal = 75; strcpy(stru.name, "John"); printf("\ndata of structure:\n integer: %d\n " "decimal: %f\n name: %s\n", stru.integer, stru.decimal, stru.name); uni.integer = 163; uni.decimal = 75; strcpy(uni.name, "John"); printf("\ndata of union:\n integeer: %d\n " "decimal: %f\n name: %s\n", uni.integer, uni.decimal, uni.name); printf("\nAccessing one member at a time:"); printf("\ndata of structure:"); stru.integer = 140; stru.decimal = 150;