0% found this document useful (0 votes)
57 views11 pages

MCS-011 (Problem Solving and Programming) Solved Assignment For 2017-18

This document contains the solved assignments for the course MCS-011 (Problem Solving and Programming) for the year 2017-18 by Khoji.net. It includes solutions to 4 questions: 1) A C program to convert an octal number to decimal, 2) An algorithm and C program for an ATM withdrawal operation, 3) A program to find the largest element in an array using recursion, and 4) A program to separate even and odd numbers in an array into two arrays. The solutions are provided by Ramanpreet Singh from Khoji.net and should not be claimed by others.

Uploaded by

khadija
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views11 pages

MCS-011 (Problem Solving and Programming) Solved Assignment For 2017-18

This document contains the solved assignments for the course MCS-011 (Problem Solving and Programming) for the year 2017-18 by Khoji.net. It includes solutions to 4 questions: 1) A C program to convert an octal number to decimal, 2) An algorithm and C program for an ATM withdrawal operation, 3) A program to find the largest element in an array using recursion, and 4) A program to separate even and odd numbers in an array into two arrays. The solutions are provided by Ramanpreet Singh from Khoji.net and should not be claimed by others.

Uploaded by

khadija
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

SOLVED BY WWW.KHOJI.NET MSC-011 SOLVED ASSIGNMENT BY KHOJI.

NET

MCS-011(Problem Solving
and Programming) Solved
Assignment for 2017-18

Solved by KHOJI.NET
Assignment is completely solved by WWW.KHOJI.NET owner
Ramanpreet Singh. No other can claim their rights on this
assignment. Solve your own and don’t try to steal my work

WWW.KHOJI.NET

WWW.KHOJINET.IN

WWW.FACEBOOK.COM/KHOJINET
LIKE OUR FACEBOOK PAGE facebook.com/khojinet for regular updates
31-Jul-17
SOLVED BY WWW.KHOJI.NET MSC-011 SOLVED ASSIGNMENT BY KHOJI.NET
Course Code : MCS-011 Course Title : Problem Solving and Programming

Assignment Number : BCA(2)/011/Assignment/17-18

Maximum Marks : 100

Weightage : 25%

Last Dates for Submission : 15th October, 2017 (For July 2017 Session)

15th April, 2018 (For January 2018 Session)

Q.1. Draw a flow chart and write its corresponding C program to convert an octal number to its equivalent decimal
number.

Do it from book…

2. Write an algorithm and its corresponding C program to illustrate an ATM money withdrawl operation from user’s
savings’ account.

Solution: This C Program performs ATM transaction. The types of ATM transaction are

1) Balance checking

2) Cash withdrawal

3) Cash deposition.

Here is C Program to display the ATM transaction:

1. #include <stdio.h>
2.
3. unsigned long amount=1000, deposit, withdraw;
4. int choice, pin, k;
5. char transaction ='y';
6.
7. void main()
8. {
9. while (pin != 1520)
10. {
11. printf("ENTER YOUR SECRET PIN NUMBER:");
12. scanf("%d", &pin);
13. if (pin != 1520)
14. printf("PLEASE ENTER VALID PASSWORD\n");
15. }
16. do
17. {
18. printf("********Welcome to ATM Service**************\n");
19. printf("1. Check Balance\n");
20. printf("2. Withdraw Cash\n");
LIKE OUR FACEBOOK PAGE facebook.com/khojinet for regular updates
SOLVED BY WWW.KHOJI.NET MSC-011 SOLVED ASSIGNMENT BY KHOJI.NET
21. printf("3. Deposit Cash\n");
22. printf("4. Quit\n");
23. printf("******************?**************************?*\n\n");
24. printf("Enter your choice: ");
25. scanf("%d", &choice);
26. switch (choice)
27. {
28. case 1:
29. printf("\n YOUR BALANCE IN Rs : %lu ", amount);
30. break;
31. case 2:
32. printf("\n ENTER THE AMOUNT TO WITHDRAW: ");
33. scanf("%lu", &withdraw);
34. if (withdraw % 100 != 0)
35. {
36. printf("\n PLEASE ENTER THE AMOUNT IN MULTIPLES OF 100")
;
37. }
38. else if (withdraw >(amount - 500))
39. {
40. printf("\n INSUFFICENT BALANCE");
41. }
42. else
43. {
44. amount = amount - withdraw;
45. printf("\n\n PLEASE COLLECT CASH");
46. printf("\n YOUR CURRENT BALANCE IS%lu", amount);
47. }
48. break;
49. case 3:
50. printf("\n ENTER THE AMOUNT TO DEPOSIT");
51. scanf("%lu", &deposit);
52. amount = amount + deposit;
53. printf("YOUR BALANCE IS %lu", amount);
54. break;
55. case 4:
56. printf("\n THANK U USING ATM");
57. break;
58. default:
59. printf("\n INVALID CHOICE");
60. }
61. printf("\n\n\n DO U WISH TO HAVE ANOTHER TRANSCATION?(y/n): \n");
62. fflush(stdin);
63. scanf("%c", &transaction);
64. if (transaction == 'n'|| transaction == 'N')
65. k = 1;
LIKE OUR FACEBOOK PAGE facebook.com/khojinet for regular updates
SOLVED BY WWW.KHOJI.NET MSC-011 SOLVED ASSIGNMENT BY KHOJI.NET
66. } while (!k);
67. printf("\n\n THANKS FOR USING OUT ATM SERVICE");
68. }

Q.3. Write a program to find the largest element in an array using Recursion.

Solution: Write a program to find the largest element in an array using Recursion

Here is the source code of the C program to print the largest number in an unsorted array:

1. #include <stdio.h>
2.
3. int large(int[], int, int);
4.
5. int main()
6. {
7. int size;
8. int largest;
9. int list[20];
10. int i;
11.
12. printf("Enter size of the list:");
13. scanf("%d", &size);
14. printf("Printing the list:\n");
15. for (i = 0; i < size ; i++)
16. {
17. list[i] = rand() % size;
18. printf("%d\t", list[i]);
19. }
20. if (size == 0)
21. {
22. printf("Empty list\n");
23. }
24. else
25. {
26. largest = list[0];
27. largest = large(list, size - 1, largest);
28. printf("\nThe largest number in the list is: %d\n", largest);
29. }
30. }
31. int large(int list[], int size, int largest)
32. {
33. if (size == 1)
34. return largest;
35.
36. if (size > -1)
LIKE OUR FACEBOOK PAGE facebook.com/khojinet for regular updates
SOLVED BY WWW.KHOJI.NET MSC-011 SOLVED ASSIGNMENT BY KHOJI.NET
37. {
38. if (list[size] > largest)
39. {
40. largest = list[size];
41. }
42. return(largest = large(list, size - 1, largest));
43. }
44. else
45. {
46. return largest;
47. }
48. }

Q.4. Write a C program to separate even and odd numbers of an array and put them in two arrays.

Solution: This C Program puts even & odd elements of an array in 2 separate arrays. The program first finds the odd and
even elements of the array. Then the odd elements of an array are stored in one array and even elements of an array
are stored in another array.

1. #include <stdio.h>
2.
3. void main()
4. {
5. long int ARR[10], OAR[10], EAR[10];
6. int i, j = 0, k = 0, n;
7.
8. printf("Enter the size of array AR \n");
9. scanf("%d", &n);
10. printf("Enter the elements of the array \n");
11. for (i = 0; i < n; i++)
12. {
13. scanf("%ld", &ARR[i]);
14. fflush(stdin);
15. }
16. /* Copy odd and even elements into their respective arrays */
17. for (i = 0; i < n; i++)
18. {
19. if (ARR[i] % 2 == 0)
20. {
21. EAR[j] = ARR[i];
22. j++;
23. }
24. else
25. {
LIKE OUR FACEBOOK PAGE facebook.com/khojinet for regular updates
SOLVED BY WWW.KHOJI.NET MSC-011 SOLVED ASSIGNMENT BY KHOJI.NET
26. OAR[k] = ARR[i];
27. k++;
28. }
29. }
30. printf("The elements of OAR are \n");
31. for (i = 0; i < j; i++)
32. {
33. printf("%ld\n", OAR[i]);
34. }
35. printf("The elements of EAR are \n");
36. for (i = 0; i < k; i++)
37. {
38. printf("%ld\n", EAR[i]);
39. }
40. }

Q.3. Write a C program to determine a given matrix is a sparse matrix.

Solution: This C Program determines the given matrix is a sparse matrix. Sparse matrix is a matrix with the majority of its
elements equal to zero. This program accepts matrix and checks whether the given matrix is a sparse matrix.

1. #include <stdio.h>
2.
3. void main ()
4. {
5. static int array[10][10];
6. int i, j, m, n;
7. int counter = 0;
8.
9. printf("Enter the order of the matix \n");
10. scanf("%d %d", &m, &n);
11. printf("Enter the co-efficients of the matix \n");
12. for (i = 0; i < m; ++i)
13. {
14. for (j = 0; j < n; ++j)
15. {
16. scanf("%d", &array[i][j]);
17. if (array[i][j] == 0)
18. {
19. ++counter;
20. }
21. }
22. }
23. if (counter > ((m * n) / 2))
24. {
25. printf("The given matrix is sparse matrix \n");
LIKE OUR FACEBOOK PAGE facebook.com/khojinet for regular updates
SOLVED BY WWW.KHOJI.NET MSC-011 SOLVED ASSIGNMENT BY KHOJI.NET
26. }
27. else
28. printf("The given matrix is not a sparse matrix \n");
29. printf("There are %d number of zeros", counter);
30. }

Q.6. Write an interactive C program to calculate the sum of array elements using pointer.

Solution: This C Program calculates the sum of array elements using pointer. The program uses the pointer to traverse
the array and adds up the element values there.

1. #include <stdio.h>
2. #include <malloc.h>
3.
4. void main()
5. {
6. int i, n, sum = 0;
7. int *a;
8.
9. printf("Enter the size of array A \n");
10. scanf("%d", &n);
11. a = (int *) malloc(n * sizeof(int));
12. printf("Enter Elements of First List \n");
13. for (i = 0; i < n; i++)
14. {
15. scanf("%d", a + i);
16. }
17. </* Compute the sum of all elements in the given array */
18. for (i = 0; i < n; i++)
19. {
20. sum = sum + *(a + i);
21. }
22. printf("Sum of all elements in array = %d\n", sum);
23. }

Q.7. Write an interactive C program to append the contents of a file at the end of another file without using any built-
in functions.

Solution: This C Program appends the content of file at the end of another.

1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. main()
5. {

LIKE OUR FACEBOOK PAGE facebook.com/khojinet for regular updates


SOLVED BY WWW.KHOJI.NET MSC-011 SOLVED ASSIGNMENT BY KHOJI.NET
6. FILE *fsring1, *fsring2, *ftemp;
7. char ch, file1[20], file2[20], file3[20];
8.
9. printf("Enter name of first file ");
10. gets(file1);
11. printf("Enter name of second file ");
12. gets(file2);
13. printf("Enter name to store merged file ");
14. gets(file3);
15. fsring1 = fopen(file1, "r");
16. fsring2 = fopen(file2, "r");
17. if (fsring1 == NULL || fsring2 == NULL)
18. {
19. perror("Error has occured");
20. printf("Press any key to exit...\n");
21. exit(EXIT_FAILURE);
22. }
23. ftemp = fopen(file3, "w");
24. if (ftemp == NULL)
25. {
26. perror("Error has occures");
27. printf("Press any key to exit...\n");
28. exit(EXIT_FAILURE);
29. }
30. while ((ch = fgetc(fsring1)) != EOF)
31. fputc(ch, ftemp);
32. while ((ch = fgetc(fsring2) ) != EOF)
33. fputc(ch, ftemp);
34. printf("Two files merged %s successfully.\n", file3);
35. fclose(fsring1);
36. fclose(fsring2);
37. fclose(ftemp);
38. return 0;
39. }

Q.8. Write an interactive C program to create a file containing student’s records and also give a provision to
update/modify the records too.

Solution: This C Program creates student record and updates it.

1. /*
2. * C Program to Create Student Record and Update it
3. */
4. #include <stdio.h>
5. #include <stdlib.h>
6. #include <string.h>
LIKE OUR FACEBOOK PAGE facebook.com/khojinet for regular updates
SOLVED BY WWW.KHOJI.NET MSC-011 SOLVED ASSIGNMENT BY KHOJI.NET
7. #define size 200
8.
9. struct std
10. {
11. int id;
12. char *name;
13. }*std1, *std3;
14.
15. void display();
16. void create();
17. void update();
18.
19. FILE *fp, *fp1;
20. int count = 0;
21.
22. void main(int argc, char **argv)
23. {
24. int i, n, ch;
25.
26. printf("1] Create a Record\n");
27. printf("2] Display Records\n");
28. printf("3] Update Records\n");
29. printf("4] Exit");
30. while (1)
31. {
32. printf("\nEnter your choice : ");
33. scanf("%d", &ch);
34. switch (ch)
35. {
36. case 1:
37. fp = fopen(argv[1], "a");
38. create();
39. break;
40. case 2:
41. fp1 = fopen(argv[1],"rb");
42. display();
43. break;
44. case 3:
45. fp1 = fopen(argv[1], "r+");
46. update();
47. break;
48. case 4:
49. exit(0);
50. }
51. }
52. }
LIKE OUR FACEBOOK PAGE facebook.com/khojinet for regular updates
SOLVED BY WWW.KHOJI.NET MSC-011 SOLVED ASSIGNMENT BY KHOJI.NET
53.
54. /* To create an student record */
55. void create()
56. {
57. int i;
58. char *p;
59.
60. std1 = (struct std *)malloc(sizeof(struct std));
61. std1->name = (char *)malloc((size)*(sizeof(char)));
62. printf("Enter name of student : ");
63. scanf(" %[^\n]s", std1->name);
64. printf("Enter student id : ");
65. scanf(" %d", &std1->id);
66. fwrite(&std1->id, sizeof(std1->id), 1, fp);
67. fwrite(std1->name, size, 1, fp);
68. count++; // count to number of entries of records
69. fclose(fp);
70. }
71.
72. /* Display the records in the file */
73. void display()
74. {
75. std3=(struct std *)malloc(1*sizeof(struct std));
76. std3->name=(char *)malloc(size*sizeof(char));
77. int i = 1;
78.
79. if (fp1 == NULL)
80. printf("\nFile not opened for reading");
81. while (i <= count)
82. {
83. fread(&std3->id, sizeof(std3->id), 1, fp1);
84. fread(std3->name, size, 1, fp1);
85. printf("\n%d %s",std3->id,std3->name);
86. i++;
87. }
88. fclose(fp1);
89. free(std3->name);
90. free(std3);
91. }
92.
93. void update()
94. {
95. int id, flag = 0, i = 1;
96. char s[size];
97.
98. if (fp1 == NULL)
LIKE OUR FACEBOOK PAGE facebook.com/khojinet for regular updates
SOLVED BY WWW.KHOJI.NET MSC-011 SOLVED ASSIGNMENT BY KHOJI.NET
99. {
100. printf("File cant be opened");
101. return;
102. }
103. printf("Enter Student id to update : ");
104. scanf("%d", &id);
105. std3 = (struct std *)malloc(1*sizeof(struct std));
106. std3->name=(char *)malloc(size*sizeof(char));
107. while(i<=count)
108. {
109. fread(&std3->id, sizeof(std3->id), 1, fp1);
110. fread(std3->name,size,1,fp1);
111. if (id == std3->id)
112. {
113. printf("Enter new name of Student to update : ");
114. scanf(" %[^\n]s", s);
115. fseek(fp1, -204L, SEEK_CUR);
116. fwrite(&std3->id, sizeof(std3->id), 1, fp1);
117. fwrite(s, size, 1, fp1);
118. flag = 1;
119. break;
120. }
121. i++;
122. }
123. if (flag != 1)
124. {
125. printf("No Student record found");
126. flag = 0;
127. }
128. fclose(fp1);
129. free(std3->name); /* to free allocated memory */
130. free(std3);
131. }

LIKE OUR FACEBOOK PAGE facebook.com/khojinet for regular updates

You might also like