-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignments.c
More file actions
585 lines (567 loc) · 14.9 KB
/
Copy pathAssignments.c
File metadata and controls
585 lines (567 loc) · 14.9 KB
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//Simple College Assignment implimentation...
//Some question are dynamic and some are static feel free to modify accordingly..
//Author github username (yankit293)..
//Author email (yankit293@gmail.com)..
#include <stdio.h>
#include <string.h>
char line[70] = "================================================================"; //string to print a line...
//function for all question in assignment 3.
void assignment_3()
{
void am3_question_1();
void am3_question_2();
void am3_question_3();
void am3_question_4();
void am3_question_5();
int am3_question_no;
while (am3_question_no != 6)
{
printf("\n%s\n", line);
printf("\nPlease Choose a question!\n");
printf("1)Write a c program to fill 5*5 matrix with following value?\n\ta)upper left triangle with 1s\n\tb)lower right triangle with -1s\n\tc)right to left diagonal with zero\n");
printf("2)Write a C program to perform binary search operation for n element?\n");
printf("3)Write a C program to create function sorting that sort an array element of size 10.\n");
printf("4)Write a C program to calculate Greatest common divisor of two number\n");
printf("5)Write a C program that allow the user to enter a string and perform the following operation.\n\ta) Count the no of character in a string\n\tb) Removes spaces in a string\n\tc) Count the no of word in a string\n");
printf("6) To Exit from Assignment 3");
printf("\n%s\n", line);
scanf("%d", &am3_question_no);
switch (am3_question_no)
{
case 1:
system("CLS"); //use clrscr(); on Turbo only..... or comment every system("CLS"); working fine on Dev c++ 5 version.
am3_question_1();
break;
case 2:
system("CLS");
am3_question_2();
break;
case 3:
system("CLS");
am3_question_3();
break;
case 4:
system("CLS");
am3_question_4();
break;
case 5:
system("CLS");
am3_question_5();
break;
case 6:
am3_question_no = 6;
break;
default:
printf("Wrong Choice!");
break;
}
}
}
void am3_question_1()
{
int arr[5][5], i, j;
int option;
do
{
system("CLS");
printf("1)Write a c program to fill 5*5 matrix with following value?\n\ta)upper left triangle with 1s\n\tb)lower right triangle with -1s\n\tc)right to left diagonal with zero\n");
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
if (i + j == 4)
arr[i][j] = 0;
else if (i + j < 4)
arr[i][j] = 1;
else
arr[i][j] = -1;
}
}
printf("\n%s\n", line);
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
printf("%d\t", arr[i][j]);
printf("\n");
}
printf("Press 1 to continue or Press 2 to exit from question 1");
scanf("%d", &option);
} while (option == 1);
}
void am3_question_2()
{
int option;
do
{
system("CLS");
printf("2)Write a C program to perform binary search operation for n element?\n");
int binary_search(int *, int, int, int);
int result, num, i;
int arr[10] = {2, 3, 4, 6, 8, 9, 14, 17, 19, 21};
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
printf("\nEnter a Number to Search\n");
scanf("%d", &num);
result = binary_search(arr, 0, 9, num);
if (result != -1)
{
printf("\n%s\n", line);
printf("\nElement %d found at index : %d\n", num, result);
}
else
{
printf("\n%s\n", line);
printf("\nElement not Found!\n");
}
printf("\nPress 1 to continue or Press 2 to exit from question 2\n");
scanf("%d", &option);
} while (option == 1);
}
//function to search an element in a given sorted array...
int binary_search(int *arr, int low, int high, int elm)
{
int mid;
if (high >= low)
{
mid = (high + low) / 2;
if (arr[mid] == elm)
return mid;
else if (arr[mid] > elm)
return binary_search(arr, low, mid - 1, elm);
else if (arr[mid] < elm)
return binary_search(arr, mid + 1, high, elm);
}
else
return -1;
}
void am3_question_3()
{
void sort_array(int *);
int option;
do
{
system("CLS");
printf("3)Write a C program to create function sorting that sort an array element of size 10.\n");
int i, arr[10];
for (i = 0; i < 10; i++)
{
printf("Enter a Number\n");
scanf("%d", &arr[i]);
}
sort_array(arr);
printf("\n%s\n", line);
printf("\nSorted array in descending order.......\n");
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
printf("\nPress 1 to continue or Press 2 to exit from question 3\n");
scanf("%d", &option);
} while (option == 1);
}
//function to sort an integer array in descending order.....
void sort_array(int *arr)
{
int i, j, t;
for (i = 0; i < 10; i++)
{
for (j = i + 1; j < 10; ++j)
{
if (arr[i] < arr[j])
{
t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
}
}
void am3_question_4()
{
int option;
do
{
system("CLS"); //use clrscr(); in Turbo......
printf("4) Write a C program to calculate Greatest common divisor of two number\n");
int i, num1, num2, k, gcd = 1;
printf("\nEnter Two number to get it's GCD\n");
scanf("%d%d", &num1, &num2);
if (num1 > num2)
k = num2;
else
k = num1;
for (i = 2; i <= k; i++)
{
if (num1 % i == 0 && num2 % i == 0)
if (gcd < i)
gcd = i;
}
printf("\n%s\n", line);
printf("Greatest Common Divisor of %d and %d is: %d", num1, num2, gcd);
printf("\nPress 1 to continue or Press 2 to exit from question 4\n");
scanf("%d", &option);
} while (option == 1);
}
void am3_question_5()
{
void remove_spaces(char *);
int count_character(char *);
int count_word(char *);
int option;
do
{
system("CLS");
char str[50];
printf("5)Write a C program that allow the user to enter a string and perform the following operation.\n\ta) Count the no of character in a string\n\tb) Removes spaces in a string\n\tc) Count the no of word in a string\n");
printf("Enter A String <50.");
scanf(" %[^\n]s", str);
int option1 = 0;
while (option1 != 4)
{
printf("\n%s\n", line);
printf("\nPlease Choose an option to Perform below task!\n1) Count the no of character in a string.\n2) Removes spaces in a string.\n3) Count the no of word in a string.\n4) Exit\n");
printf("\n%s\n", line);
scanf(" %d", &option1);
if (option1 == 1)
{
printf("\n%s\n", line);
printf("\n\nTotal Character in String : %d\n", count_character(str));
}
else if (option1 == 2)
{
printf("\n%s\n", line);
remove_spaces(str);
}
else if (option1 == 3)
{
printf("\n%s\n", line);
printf("\n\nTotal Words in String : %d\n", count_word(str));
}
else if (option1 == 4)
break;
else
printf("\nWrong Option");
}
printf("\nPress 1 to continue or Press 2 to exit from question 5\n");
scanf(" %d", &option);
} while (option == 1);
}
//Function To remove spcaes
void remove_spaces(char *str)
{
char temp[50];
int i = 0, j = 0;
strcpy(temp, str);
while (temp[i])
{
if (temp[i] != ' ')
temp[j++] = temp[i];
i++;
}
temp[j] = '\0';
printf("\n%s\n", temp);
}
//function to count character in a string..
int count_character(char *str)
{
int i = 0, counter = 0;
while (str[i])
{
if (str[i] != ' ')
counter += 1;
i++;
}
return counter;
}
//function to count words in a string...
int count_word(char *str)
{
int i = 0, counter = 0;
while (str[i])
{
if (str[i] == ' ' && str[i + 1] != ' ')
counter++;
i++;
}
return counter + 1;
}
//Function for assignment 4 all question..
void assignment_4()
{
void am4_question_1();
void am4_question_2();
void am4_question_3();
void am4_question_4();
void am4_question_5();
void am4_question_6();
int am4_question_no;
while (am4_question_no != 7)
{
printf("\n%s\n", line);
printf("\nPlease Choose a question!\n");
printf("1)Write a program that accepts two strings.\nThe program should add second string at the end of the first string.(string concatenate)\n");
printf("2)Write a program to create a function name “avg” that accepts an array of integers and displays the average using pointer.\n");
printf("3)Write a C program to create swap function that will swap two variable using pointer.\n");
printf("4)write a program to reverse the character array using pointer.\nFor example:\n\tChar a[20]=”hello”;\n\tOutput:olleh\n");
printf("\nAdvanced Data Types & Sorting\n");
printf("5)Write a C program to implement an inventory system. Store the item number,name, rate and quantity on hand in a structure.\nAccept the details for five items into a structure array and display the item name and its total price.\n At the end,display the grand total value of the inventory.\n");
printf("6)Write a C program to store the names and scores of 5 students in a structure array.\nSort the structure array in descending order of scores. Display the top 3scores.\n");
printf("7)To Exit from Assignment 4");
printf("\n%s\n", line);
scanf("%d", &am4_question_no);
switch (am4_question_no)
{
case 1:
system("CLS");
am4_question_1();
break;
case 2:
system("CLS");
am4_question_2();
break;
case 3:
system("CLS");
am4_question_3();
break;
case 4:
system("CLS");
am4_question_4();
break;
case 5:
system("CLS");
am4_question_5();
break;
case 6:
system("CLS");
am4_question_6();
break;
case 7:
am4_question_no = 7;
break;
default:
printf("Wrong Choice!");
break;
}
}
}
void am4_question_1()
{
char str1[50], str2[20];
int i, j, option;
do
{
printf("1)Write a program that accepts two strings.\nThe program should add second string at the end of the first string.(string concatenate)\n\n");
printf("Enter String 1 <30\n");
scanf(" %[^\n]s", str1);
printf("Enter String 2 <20\n");
scanf(" %[^\n]s", str2);
for (i = 0; str1[i] != '\0'; i++)
;
for (j = 0; str2[j] != '\0'; j++)
{
str1[i] = str2[j];
i++;
}
str1[i] = '\0';
printf("\n%s\n", line);
printf("Concatenated String:%s", str1);
printf("\nPress 1 to continue or Press 2 to exit from question 5\n");
scanf(" %d", &option);
} while (option == 1);
}
void am4_question_2()
{
void avg(int arr[]);
int arr[5], i, option;
do
{
printf("2)Write a program to create a function name “avg” that accepts an array of integers and displays the average using pointer.\n");
for (i = 0; i < 5; i++)
{
printf("Enter Arr[%d]\n", i + 1);
scanf("%d", &arr[i]);
}
avg(arr);
printf("\nPress 1 to continue or Press 2 to exit from question 5\n");
scanf(" %d", &option);
} while (option == 1);
}
//function to print average of integer array...
void avg(int arr[])
{
int i, sum = 0, avrg, *p;
for (i = 0; i < 5; i++)
{
sum += arr[i];
}
avrg = sum / i;
p = &avrg;
printf("\n%s\n", line);
printf("Average of all number in array:%d", *p);
}
void am4_question_3()
{
int a, b, option;
void swap(int *, int *);
do
{
printf("3)Write a C program to create swap function that will swap two variable using pointer.\n");
printf("Enter Two Interger value to swap.\n");
scanf("%d%d", &a, &b);
printf("\n%s\n", line);
printf("Before swap a:%d b:%d\n", a, b);
swap(&a, &b);
printf("After swap a:%d b:%d\n", a, b);
printf("\nPress 1 to continue or Press 2 to exit from question 5\n");
scanf(" %d", &option);
} while (option == 1);
}
//function to swap to variable value using pointer.....
void swap(int *p, int *q)
{
int t; // temp variable
t = *p;
*p = *q;
*q = t;
}
void am4_question_4()
{
char str[50], *start_index, *end_index, ch;
int i, j, option;
do
{
printf("4)write a program to reverse the character array using pointer.\nFor example:\n\tChar a[20]=”hello”;\n\tOutput:olleh\n");
printf("Enter A string to reverse...\n");
scanf("%s", str);
start_index = str;
end_index = str;
j = strlen(str);
for (i = 0; i < j - 1; i++)
end_index++;
for (i = 0; i < j / 2; i++)
{
ch = *end_index;
*end_index = *start_index;
*start_index = ch;
end_index--;
start_index++;
}
printf("\n%s\n", line);
printf("Reverse of String:%s", str);
printf("\nPress 1 to continue or Press 2 to exit from question 5\n");
scanf(" %d", &option);
} while (option == 1);
}
void am4_question_5()
{
struct inventry
{
int item_number;
char item_name[20];
float item_rate;
int quantity_on_hand;
} item[5];
int i, option;
float total_price, grand_total = 0;
do
{
printf("5)Write a C program to implement an inventory system. Store the item number,name, rate and quantity on hand in a structure.\nAccept the details for five items into a structure array and display the item name and its total price.\n At the end,display the grand total value of the inventory.\n");
for (i = 0; i < 5; i++)
{
printf("Enter the Number of Item %d.\n", i + 1);
scanf(" %d", &item[i].item_number);
printf("Enter the name of Item %d.\n", i + 1);
scanf(" %[^\n]s", item[i].item_name);
printf("Enter the rate of Item %d.\n", i + 1);
scanf("%0.2f", &item[i].item_rate);
printf("Enter the quantity on hand of item %d.\n", i + 1);
scanf("%d", &item[i].quantity_on_hand);
}
printf("\n%s\n", line);
for (i = 0; i < 5; i++)
{
printf("\n%s\n", line);
printf("Item number:%d\nItem Name:%s\nItem Rate:%0.2fRs.\nItem Quantity on hand:%d\nTotal price of Item:%0.2fRs.\n\n", item[i].item_number, item[i].item_name, item[i].item_rate, item[i].quantity_on_hand, item[i].item_rate * item[i].quantity_on_hand);
grand_total += item[i].item_rate * item[i].quantity_on_hand;
}
printf("Grand Total:%f", grand_total);
printf("\nPress 1 to continue or Press 2 to exit from question 5\n");
scanf(" %d", &option);
} while (option == 1);
}
void am4_question_6()
{
struct records
{
char name[20];
int score;
} student[5];
int i, j, t, option;
char temp[20];
do
{
printf("6)Write a C program to store the names and scores of 5 students in a structure array.\nSort the structure array in descending order of scores. Display the top 3scores.\n");
for (i = 0; i < 5; i++)
{
printf("Enter the Name of student %d:", i + 1);
scanf(" %[^\n]s", student[i].name);
printf("Enter the score of student %d:", i + 1);
scanf("%d", &student[i].score);
}
for (i = 0; i < 5; i++)
{
for (j = i + 1; j < 5; ++j)
{
if (student[i].score < student[j].score)
{
//sorting scores
t = student[i].score;
student[i].score = student[j].score;
student[j].score = t;
//sorting names
strcpy(temp, student[i].name);
strcpy(student[i].name, student[j].name);
strcpy(student[j].name, temp);
}
}
}
printf("\n%s\n", line);
for (i = 0; i < 3; i++)
{
printf("\nName of student at %d position : %s\n Score of student:%d\n", i + 1, student[i].name, student[i].score);
}
printf("\nPress 1 to continue or Press 2 to exit from question 5\n");
scanf(" %d", &option);
} while (option == 1);
}
int main()
{
void assignment_3();
void assignment_4();
int m_option = 0;
while (m_option != 3)
{
system("CLS");
printf("\nChoose an option from below\n");
printf("1). Assignment 3\n2). Assignment 4\n3) Exit.\n");
scanf("%d", &m_option);
if (m_option == 1)
{
system("CLS"); //
assignment_3();
}
else if (m_option == 2)
{
system("CLS");
assignment_4();
}
else if (m_option == 3)
break;
else
printf("Wrong Choice");
continue;
}
return 0;
}