Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added sort elements in lexicographical order using library functions #167

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 21 additions & 66 deletions headerfiles/34.h
Original file line number Diff line number Diff line change
@@ -1,69 +1,24 @@
#include <stdio.h>
struct TIME {
int seconds;
int minutes;
int hours;
};

void differenceBetweenTimePeriod(struct TIME t1,
struct TIME t2,
struct TIME *diff);

int main() {
struct TIME startTime, stopTime, diff;

printf("Enter the start time. \n");
printf("Enter hours, minutes and seconds: ");
scanf("%d %d %d", &startTime.hours,
&startTime.minutes,
&startTime.seconds);

printf("Enter the stop time. \n");
printf("Enter hours, minutes and seconds: ");
scanf("%d %d %d", &stopTime.hours,
&stopTime.minutes,
&stopTime.seconds);

// Difference between start and stop time
differenceBetweenTimePeriod(startTime, stopTime, &diff);
printf("\nTime Difference: %d:%d:%d - ", startTime.hours,
startTime.minutes,
startTime.seconds);
printf("%d:%d:%d ", stopTime.hours,
stopTime.minutes,
stopTime.seconds);
printf("= %d:%d:%d\n", diff.hours,
diff.minutes,
diff.seconds);
return 0;
}

// Computes difference between time periods
void differenceBetweenTimePeriod(struct TIME start,
struct TIME stop,
struct TIME *diff) {
while (stop.seconds > start.seconds) {
--start.minutes;
start.seconds += 60;
}
diff->seconds = start.seconds - stop.seconds;
while (stop.minutes > start.minutes) {
--start.hours;
start.minutes += 60;
#include <string.h>
void dictionary_i(char str[][50],int n) {

char temp[50];
// storing strings in the lexicographical order
for (int i = 0; i < n-1; ++i) {
for (int j = i + 1; j < n; ++j) {

// swapping strings if they are not in the lexicographical order
if (strcmp(str[i], str[j]) > 0) {
strcpy(temp, str[i]);
strcpy(str[i], str[j]);
strcpy(str[j], temp);
}
}
}
diff->minutes = start.minutes - stop.minutes;
diff->hours = start.hours - stop.hours;
}


OUTPUT:-
Enter the start time.
Enter hours, minutes and seconds: 12
34
55
Enter the stop time.
Enter hours, minutes and seconds: 8
12
15

Time Difference: 12:34:55 - 8:12:15 = 4:22:40
printf("\nIn the lexicographical order: \n");
for (int i = 0; i < 5; ++i) {

puts(str[i]);
}
}
14 changes: 13 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "./headerfiles/31.h"
#include "./headerfiles/32.h"
#include "./headerfiles/33.h"
#include "./headerfiles/34.h"

void main()
{
Expand All @@ -43,7 +44,7 @@ void main()
printf("11. Program to find the largest of three numbers using Pointers\n12. Program to count vowels and consonants in a String using pointer\n13. Program to print String using Pointer\n14. Program to swap two numbers using pointers\n15. Program to create initialize and access a pointer variable\n");
printf("16. Find the value of nPr for given value of n & r\n17. Find the value of nCr for given value of n & r\n18. Program to multiply two floating numbers\n19. Program to find out Quotient and Remainder\n20. Program to find average of two numbers\n");
printf("21. Program to Add Two Matrices Using Multi-dimensional Arrays\n22. Program to Multiply Two Matrices Using Multi-dimensional Arrays\n23. Program Swap Numbers in Cyclic Order Using Call by Reference\n24. Program to Find Largest Number Using Dynamic Memory Allocation\n25. Program to Sort Elements in Lexicographical Order (Dictionary Order)\n");
printf("26. Program to Calculate Difference Between Two Time Periods\n27. Program to Display its own Source Code as Output\n28. Matrix rotation by 90 degrees clockwise and anticlockwise\n29. Saddle point coordinates of a given matrix\n30. Matrix printing in aspiral form\n31. Mean and Median of an unsorted array\n32. Print Pascal's triangle\n33.Check if a number is Prime");
printf("26. Program to Calculate Difference Between Two Time Periods\n27. Program to Display its own Source Code as Output\n28. Matrix rotation by 90 degrees clockwise and anticlockwise\n29. Saddle point coordinates of a given matrix\n30. Matrix printing in aspiral form\n31. Mean and Median of an unsorted array\n32. Print Pascal's triangle\n33.Check if a number is Prime\n34.Sort Elements in Lexicographical Order Using library functions (Dictionary Order)\n");
printf("\nSelect your program number from above: ");
scanf("%d", &ch);
switch (ch)
Expand Down Expand Up @@ -507,6 +508,17 @@ void main()
check_prime(n);
break;
}
case 34:{
char str[5][50];
printf("Enter 5 words: ");

// Getting strings input
for (int i = 0; i < 5; ++i) {
scanf("%s[^\n]",str[i]);
}
dictionary_i(str,5);
break;
}

default:
printf("Invalid Input. Try Again!\n");
Expand Down