-
Notifications
You must be signed in to change notification settings - Fork 99
/
main.c
522 lines (493 loc) · 13.6 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#include <stdio.h>
#include "./headerfiles/00.h"
#include "./headerfiles/01.h"
#include "./headerfiles/02.h"
#include "./headerfiles/03.h"
#include "./headerfiles/04.h"
#include "./headerfiles/05.h"
#include "./headerfiles/06.h"
#include "./headerfiles/07.h"
#include "./headerfiles/08.h"
#include "./headerfiles/09.h"
#include "./headerfiles/10.h"
#include "./headerfiles/11.h"
#include "./headerfiles/12.h"
#include "./headerfiles/13.h"
#include "./headerfiles/14.h"
#include "./headerfiles/15.h"
#include "./headerfiles/16.h"
#include "./headerfiles/17.h"
#include "./headerfiles/18.h"
#include "./headerfiles/19.h"
#include "./headerfiles/20.h"
#include "./headerfiles/21.h"
#include "./headerfiles/22.h"
#include "./headerfiles/23.h"
#include "./headerfiles/24.h"
#include "./headerfiles/25.h"
#include "./headerfiles/26.h"
#include "./headerfiles/27.h"
#include "./headerfiles/28.h"
#include "./headerfiles/29.h"
#include "./headerfiles/30.h"
#include "./headerfiles/31.h"
#include "./headerfiles/32.h"
#include "./headerfiles/33.h"
void main()
{
int ch;
printf("List of Programs:\n00. Program to find a sum of two numbers(Example)\n");
printf("01. Count distinct elements of an array\n02. Program to convert string from lower case to upper case\n03. Sort a set of strings in ascending alphabetical order\n04. Find length of a string without using strlen()\n05. String concatenation without using strcat\n06. Reverse a String using recursion\n");
printf("07. Program to sort array in ascending order\n08. Find largest element of given array\n09. Program to find sum of array elements\n10. Program to find number of elements in an array\n");
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("\nSelect your program number from above: ");
scanf("%d", &ch);
switch (ch)
{
case 0:
{
int x, y;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
printf("Sum of %d and %d is %d\n", x, y, sum(x, y));
break;
}
case 1:
{
int n;
printf("\nEnter the size of an array\n");
scanf("%d", &n);
int A[n];
printf("\nEnter the values of an array\n");
for (int i = 0; i < n; i++)
{
scanf("%d", &A[i]);
}
printf("\nDistinct Elements count: %d\n", Array_count_distinct(A, n));
break;
}
case 2:
{
char s[100];
printf("\nEnter a string : ");
scanf("\n%[^\n]%*c", s);
toUpper(s);
break;
}
case 3: //sort a set of strings in ascending alphabetical order
{
char str[100][100]; //store the strings
int n; //number of strings entered
printf("\n\nEnter the number of strings: ");
scanf("%d", &n);
printf("Enter %d strings: \n", n);
for(int i = 0; i < n; i++)
scanf(" %s", &str[i]);
//Call StringSort function in header 03.h to sort and print the strings
StringSort(str, n);
break;
}
case 4:
{
char input_str[1000];
printf("\nEnter any string:\n");
scanf(" %[^\t\n]s", &input_str);
int length = string_len(input_str);
printf("\nThe length of the input string is : %d\n", length);
break;
}
case 5:
{
char str1[50], str2[50];
printf("\nEnter first string: ");
scanf("%s", str1);
printf("\nEnter second string: ");
scanf("%s", str2);
concat_str(str1, str2);
break;
}
case 6:
{
char f[100];
printf("Enter a string :- ");
gets(f);
printf("The reverse of the string is :- ");
rev(f, 0, strlen(f) - 1);
printf("%s\n", f);
break;
}
case 7:
{
int a[100], n;
printf("\nEnter size of the array");
scanf("%d", &n);
printf("Enter the elements :");
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
Sort_ele(n, a);
break;
}
case 8:
{
int a[100], n, i;
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter %d elements :", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("%d", largest(a, n));
break;
}
case 9:
{
int n, a[100];
printf("enter the number of elements\n");
scanf("%d", &n);
printf("Enter the elements\n");
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
sum_elements(n, a);
break;
}
case 10:
{
int *A, i = 0;
A = (int *)malloc(sizeof(int));
char ch;
do
{
printf("Enter the data: ");
scanf("%d", (A + i));
i++;
printf("Do you want to enter more data(y/n): ");
scanf(" %ch", &ch);
} while (ch == 'y');
count(i);
break;
}
case 11:
{
int num1, num2, num3;
printf("Enter First Number: ");
scanf("%d", &num1);
printf("Enter Second Number: ");
scanf("%d", &num2);
printf("Enter Third Number: ");
scanf("%d", &num3);
largestOfThree(num1, num2, num3);
break;
}
case 12:
{
char s[1000];
int count_vowels, count_consonants;
printf("Please enter a string: ");
scanf("\n%[^\n]", s);
count_vowels_and_consonants(s, &count_vowels, &count_consonants);
printf("Number of vowels in the string= %d\nNumber of consonants in the string= %d\n", count_vowels, count_consonants);
break;
}
case 13:
{
char s[1000];
printf("Enter the string :- ");
gets(s);
printf("The string is :- ");
display_str(s);
printf("\n");
break;
}
case 14:
{
int num1, num2;
printf("Enter value of num1: ");
scanf("%d", &num1);
printf("Enter value of num2: ");
scanf("%d", &num2);
//displaying numbers before swapping
printf("Before Swapping: num1 is: %d, num2 is: %d\n", num1, num2);
//calling the user defined function swap()
swapusingpointer(&num1, &num2);
//displaying numbers after swapping
printf("After Swapping: num1 is: %d, num2 is: %d\n", num1, num2);
break;
}
case 15:
{
actionsonpointer();
break;
}
case 16:
{
int n, r;
printf("Enter n: ");
scanf("%d", &n);
printf("Enter r: ");
scanf("%d", &r);
printf("%dP%d is %d", n, r, nPr(n, r));
break;
}
case 17:
{
int n, r;
long ncr;
printf("Enter the value of n and r\n");
scanf("%d%d",&n,&r);
ncr = find_ncr(n, r);
printf("%dC%d = %ld\n", n, r, ncr);
break;
}
case 18:
{
float num1, num2;
printf("Enter value of num1: ");
scanf("%f", &num1);
printf("Enter value of num2: ");
scanf("%f", &num2);
//calling the user defined function findAverage
double pruduct = findProduct(num1, num2);
//displaying the average
printf("Product of %f and %f is %lf\n", num1, num2, pruduct);
break;
}
case 19:
{
int num1, num2, quot, rem;
printf("Enter dividend:");
scanf("%d", &num1);
printf("Enter divisor:");
scanf("%d", &num2);
//calling function quotient()
quot= quotient(num1, num2);
//calling function remainder()
rem = remainder(num1, num2);
printf("Quotient is: %d\n",quot);
printf("Remainder is : %d",rem);
break;
}
case 20:
{
int n1, n2;
printf("Enter first number: ");
scanf("%d", &n1);
printf("Enter second number: ");
scanf("%d", &n2);
avg(n1, n2);
break;
}
case 21:
{
int r, c;
printf("Enter number of rows and column for the matrices: \n");
scanf("%d%d", &r, &c);
int mat1[r][c], mat2[r][c], i, j;
printf("Enter the elements of the first matrix: \n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d", &mat1[i][j]);
}
}
printf("Enter the elements of the second matrix: \n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d", &mat2[i][j]);
}
}
addMultidimentionalArray((int *)mat1, (int *)mat2, r, c);
break;
}
case 22:
{
int first[10][10], second[10][10], result[10][10], r1, c1, r2, c2;
printf("Enter rows and column for the first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for the second matrix: ");
scanf("%d %d", &r2, &c2);
// Taking input until
// 1st matrix columns is not equal to 2nd matrix row
while (c1 != r2)
{
printf("Error! Enter rows and columns again.\n");
printf("Enter rows and columns for the first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and columns for the second matrix: ");
scanf("%d%d", &r2, &c2);
}
// get elements of the first matrix
getMatrixElements(first, r1, c1);
// get elements of the second matrix
getMatrixElements(second, r2, c2);
// multiply two matrices.
multiplyMatrices(first, second, result, r1, c1, r2, c2);
// display the result
display(result, r1, c2);
break;
}
case 23:
{
int a, b, c;
printf("Enter a, b and c (space separated): ");
scanf("%d %d %d", &a, &b, &c);
printf("::Before Swapping::\n");
printf("a = %d \nb = %d \nc = %d\n", a, b, c);
cyclicSwapbyRef(&a, &b, &c);
printf("::After Swapping::\n");
printf("a = %d \nb = %d \nc = %d", a, b, c);
printf("\n");
break;
}
case 24:
{
int i, n;
int *a;
printf("Enter the no. of elements : ");
scanf("%d", &n);
a = (int *)calloc(n, sizeof(int));
if (a == NULL)
{
printf("No memory allocated");
exit(0);
}
printf("\n");
printf("Enter the numbers : ");
for (i = 0; i < n; ++i)
{
scanf("%d", a + i);
}
Largest(a, n);
//return 0;
break;
}
case 25:
{
char stringMatrix[100][100];
//Find the number of words
int numberOfWords;
printf("\nEnter the number of words: ");
scanf("%d", &numberOfWords);
//Input the words
getWords(stringMatrix, numberOfWords);
//Sort the words in lexicographical order
sortWords(stringMatrix, numberOfWords);
//Print the words
printWords(stringMatrix, numberOfWords);
break;
}
case 26:
{
struct time start, stop, diff;
printf("Enter hours, minutes and seconds of start time: ");
scanf("%d%d%d", &start.hours, &start.minutes, &start.seconds);
printf("Enter hours, minutes and seconds of stop time: ");
scanf("%d%d%d", &stop.hours, &stop.minutes, &stop.seconds);
timeDiff(start, stop, diff);
break;
}
case 27:
{
GET_SRC_CODE;
break;
}
case 28:
{
int row, column;
printf("\nEnter the number of rows: ");
scanf("%d", &row);
printf("\nEnter the number of column: ");
scanf("%d", &column);
int matrix[row][column], clockwise[column][row], aclockwise[column][row];
inputMatrix(&matrix[0][0], row, column);
printf("\nOriginal Matrix:\n");
printMatrix(&matrix[0][0], row, column);
//Function to find out the clockwise rotated matrix
findClockwiseMatrix(&matrix[0][0], &clockwise[0][0], row, column);
//Function to find out the anticlockwise rotated matrix
findAntiClockwiseMatrix(&matrix[0][0], &aclockwise[0][0], row, column);
printf("\nMatrix rotated by 90 degree clockwise: \n");
printMatrix(&clockwise[0][0], column, row);
printf("\nMatrix rotated by 90 degree anticlockwise: \n");
printMatrix(&aclockwise[0][0], column, row);
break;
}
case 29:
{
int rows, columns;
printf("Enter the number rows matrix ");
scanf("%d", &rows);
printf("Enter the number columns matrix ");
scanf("%d", &columns);
int a[rows][columns];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
scanf("%d", &a[i][j]);
}
}
int result = getSadlePoint(rows, columns, a);
printf("The saddle point of given matrix is %d", result);
break;
}
case 30:
{
int i, j, row, column;
printf("\nEnter the number of rows: ");
scanf("%d", &row);
printf("Enter the number of columns: ");
scanf("%d", &column);
int matrix[row][column];
printf("Enter values into the matrix:\n");
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
printf("matrix[%d][%d] : ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
printf("\nOriginal Matrix:\n");
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
printf("\n");
print_matrix_aspiral(&matrix[0][0], row, column);
break;
}
case 31:
MeanMedian();
break;
case 32:
{
int p_row;
printf("Enter no of rows: ");
scanf("%d", &p_row);
pascal(p_row);
break;
}
case 33:
{
int n;
printf("\n Enter the number : \n");
scanf("%d", &n);
check_prime(n);
break;
}
default:
printf("Invalid Input. Try Again!\n");
break;
}
}