0% found this document useful (0 votes)
24 views13 pages

Osw 2022 Midsem Pyq Solved

Uploaded by

faujiantony
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)
24 views13 pages

Osw 2022 Midsem Pyq Solved

Uploaded by

faujiantony
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/ 13

1A.

Assuming that side and area are type double variables


containing of one side in cm and the area of a square in
square cm, write a c statement that will display this
information in this form:
“The area of a square whose side length is _____ cm is _____
square cm.”
printf(“The area of a square whose side length is %.2f cm, is
%.2f square cm.\n.”,side, area);

B.Write the #define preprocessor directive and declarations


for a program that has a constant macro for PI (3.14159) and
variables radius, area and circumf declared as doubled,
variable num_circ as an int, and variable circ_name as a char.
#define PI 3.14159
double radius;
double area;
int num_circ;
char circ_name;

C.If you have N eggs, then you have N/12 dozen eggs, with N%12
eggs left over. Write a program that asks the user how many
eggs she has and then tells the user how many dozen eggs she
has and how many extra eggs are left over. A gross of eggs is
equal to 144 eggs. Extend your program so that it will tell
the user how many gross, how many dozen, and how many left
over eggs she has.
#include <stdio.h>
int main() {
int eggs = 144;
int gross = eggs / 144;
int remaining = eggs % 144;
int dozens = remaining / 12;
int leftover_eggs = remaining % 12;
printf("%d gross of eggs\n", gross);
printf("%d dozens of eggs\n", dozens);
printf("%d leftover eggs\n", leftover_eggs);
return 0;
}
2A.Trace the execution sequence and determine the output of
the following code snippet.
int jumble(int x,int y)
{
x = 2 * x + y;
return x;
}
int main() {
int x = 2, y = 5;
y=jumble(x,y);
x=jumble(y,x);
printf(“%d\n”,x);
return 0;}
Output: 20.

B.Write a program for an automatic teller machine that


dispenses money. The user should enter the amount desired (a
multiple of 10 dollars) and the machine dispenses this amount
using the least number of bills. The bills dispensed are 50s,
20s, and 10s. Write a function that determines how many of
each kind of bill to dispense.
#include <stdio.h>
int get_bills(int amount, int *fifties, int *twenties, int
*tens) {
*fifties = amount / 50;
amount %= 50;
*twenties = amount / 20;
amount %= 20;
*tens = amount / 10;
return *fifties || *twenties || *tens;
}
int main() {
int desired_amount, fifties, twenties, tens;
printf("Enter desired amount (multiple of 10): ");
scanf("%d", &desired_amount);
if (desired_amount % 10 != 0) {
printf("Error: Amount must be a multiple of 10\n");
return 1;
}
if (!get_bills(desired_amount, &fifties, &twenties, &tens))
{
printf("Error: Insufficient funds\n");
return 1;
}
printf("Dispensing %d dollars...\n", desired_amount);
if (fifties) printf("- %d $50 bills\n", fifties);
if (twenties) printf("- %d $20 bills\n", twenties);
if (tens) printf("- %d $10 bills\n", tens);
return 0;
}

C.Write a program to process a collection of scores obtained


by students of a class of certain strength. Your program
should count and print the number of students with Grade A (80
and higher), Grade B (65-79), Grade C(40-64) and Grade F(39
and below). Ensure that the entered scores must remain in
between 0 and 100 (inclusive). Test your program on the
following data:
-23 567 65 12 89 32 17 45 41 58 60 78 82 88 19 22 70 88 41 89
78 79 72 68 74 59 75 81 44 59 -23 -12
#include <stdio.h>
const int grades[][2] = {
{ 80, 100 }, // A
{ 65, 79 }, // B
{ 40, 64 }, // C
{ 0, 39 } // F
};
void count_grades(int scores[], int num_students) {
int grade_counts[sizeof(grades) / sizeof(grades[0])];
for (int i = 0; i < sizeof(grade_counts) /
sizeof(grade_counts[0]); i++) {
grade_counts[i] = 0;
}
for (int i = 0; i < num_students; i++) {
if (scores[i] < 0 || scores[i] > 100) {
printf("Invalid score: %d\n", scores[i]);
continue;
}
for (int j = 0; j < sizeof(grades) / sizeof(grades[0]);
j++) {
if (scores[i] >= grades[j][0] && scores[i] <=
grades[j][1]) {
grade_counts[j]++;
break;
}
}
}
printf("\nGrade Counts:\n");
for (int i = 0; i < sizeof(grade_counts) /
sizeof(grade_counts[0]); i++) {
printf("- %s: %d\n", grades[i][0] == 80 ? "A" :
(grades[i][0] == 65 ? "B" : (grades[i][0] == 40 ? "C" : "F")),
grade_counts[i]);
}
}
int main() {
int scores[] = {-23, 567, 65, 12, 89, 32, 17, 45, 41, 58,
60, 78, 82, 88, 19, 22, 70, 88, 41, 89, 78, 79, 72, 68, 74,
59, 75, 81, 44, 59, -23, -12};
int num_students = sizeof(scores) / sizeof(scores[0]);
count_grades(scores, num_students);
return 0;
}

3A.An integer n is divisible by 9 if the sum of its digits is


divisible by 9. Develop a program to display each digit,
starting with the rightmost digit. Your program should also
determine whether or not the number is divisible by 9.
#include <stdio.h>
int is_divisible_by_9(int n) {
int sum_of_digits = 0;
while (n > 0) {
int digit = n % 10;
sum_of_digits += digit;
n /= 10;
}
return sum_of_digits % 9 == 0;
}
void print_digits_and_divisibility(int n) {
printf("Digits: ");
while (n > 0) {
int digit = n % 10;
printf("%d", digit);
n /= 10;
}
printf("\n");
if (is_divisible_by_9(n)) {
printf("The number is divisible by 9.\n");
} else {
printf("The number is not divisible by 9.\n");
}
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
print_digits_and_divisibility(number);
return 0;
}

B.Fill the formal parameters at the symbols, ??, to the


functions f1 and f2. Also write the output of the code
segment.
void f1(int ??, int ??)
{int c;
c=a; a=b; b=c;
}
void f2(int ??,int ??)
{ int c;
c=*a; *a=*b; *b=c;
}
int main(void) {
int a=10,b=20,c=9;
f1(a,b);
f2(&b, &c);
printf(“%d”,a);
printf(“%d”,b);
printf(“%d”,c);
}
Missing parameters:
1. f1(int a, int b)
2. **f2(int a, int b)
Output:
20
9
9

C.Develop a program to use the idea of multiple calls to a


function with input/output parameters to sort 6 integer
numbers in ascending order without using any sorting
algorithms. The prototype of the function to be used in your
program to sort the numbers is given as void arrange(int *,
int *); and also draw the data areas of the calling function
and arrange() function for the first function call
arrange(...).
#include <stdio.h>
void arrange(int *a, int *b) {
if (*a > *b) {
int temp = *a;
*a = *b;
*b = temp;
}
}
void sort_numbers(int numbers[6]) {
for (int i = 0; i < 5; i++) {
arrange(&numbers[i], &numbers[i + 1]);
}
for (int i = 1; i < 4; i++) {
arrange(&numbers[i], &numbers[i + 1]);
}
for (int i = 2; i < 3; i++) {
arrange(&numbers[i], &numbers[i + 1]);
}
}
int main() {
int numbers[6] = {5, 3, 1, 6, 2, 4};
printf("Original numbers: ");
for (int i = 0; i < 6; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
sort_numbers(numbers);
printf("Sorted numbers: ");
for (int i = 0; i < 6; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}

Data Areas for first arrange(...) call:

● Calling function:
○ numbers[0]: Stores the value of the first element (5
in this case).
○ numbers[1]: Stores the value of the second element
(3 in this case).
● arrange() function:
○ a: Points to the memory location of numbers[0] (5).
○ b: Points to the memory location of numbers[1] (3).

4A.Write a program to copy the distinct elements of an int


type array to another int type array. For example, if the
input array is 4 7 7 3 2 5 5 then the output array will be 4
7 3 2 5.
#include <stdio.h>
int copy_distinct(int original_array[], int distinct_array[],
int original_size) {
int distinct_index = 0;
for (int i = 0; i < original_size; i++) {
int found = 0;
for (int j = 0; j < distinct_index; j++) {
if (original_array[i] == distinct_array[j]) {
found = 1;
break;
}
}
if (!found) {
distinct_array[distinct_index] = original_array[i];
distinct_index++;
}
}
return distinct_index;
}
int main() {
int original_array[] = {4, 7, 7, 3, 2, 5, 5};
int distinct_array[sizeof(original_array) /
sizeof(original_array[0])];
int distinct_size = copy_distinct(original_array,
distinct_array, sizeof(original_array) /
sizeof(original_array[0]));
printf("Original array: ");
for (int i = 0; i < sizeof(original_array) /
sizeof(original_array[0]); i++) {
printf("%d ", original_array[i]);
}
printf("\n");
printf("Distinct array: ");
for (int i = 0; i < distinct_size; i++) {
printf("%d ", distinct_array[i]);
}
printf("\n");
return 0;
}

B. Given the string char pres[]=”Adams,John Quincy”; and the


40-character temporary variables tmp1 and tmp2. State the
execution of the code snippet and output at printf.
strncpy(tmp1, &pres[7], 4);
tmp1[4] = ‘\0’;
strcat(tmp1, “ ”);
strncpy(tmp2, pres, 5);
tmp2[5] = ‘\0’;
printf(“%s\n”, strcat(tmp1, tmp2));

Execution and Output:

1. strncpy(tmp1, &pres[7], 4):

● This copies 4 characters from pres starting at index 7


(inclusive) to tmp1.
● Since the source string is "Adams,John Quincy", the
copied characters will be "Quin".

2. tmp1[4] = '\0';

● This adds a null terminator (\0) to the end of tmp1 at


position 5, marking the end of the string.
● Now, tmp1 contains "Quin\0".

3. strcat(tmp1, " ")

● This appends a space (" ") to the end of tmp1.


● So, tmp1 now holds "Quin \0".

4. strncpy(tmp2, pres, 5):

● This copies 5 characters from pres starting at index 0


(inclusive) to tmp2.
● The copied characters are "Adams".
5. tmp2[5] = '\0';

● This adds a null terminator to the end of tmp2 at


position 6.
● Now, tmp2 contains "Adams\0".

6. printf("%s\n", strcat(tmp1, tmp2));

● This calls strcat to append tmp2 to the end of tmp1 and


stores the result in a temporary string.
● Since both strings are null-terminated, the resulting
string is "Quin Adams\0".
● Finally, printf prints the content of the temporary
string without the null terminator, resulting in:

Output: Quin Adams

C.Organise the Unix commands you have learned to achieve the


given task; (1) create a directory (2) create a file in that
directory (3) display the content of the file using cat (4)
append few more lines to the file using cat (5) display the
list of files.
1. Create a directory:
mkdir directory_name

2. Create a file in that directory


cd directory_name
touch file_name.txt

3. Display the content of the file using cat:


Cat file_name.txt

4. Append few more lines to the file using cat (with


redirection):
echo “Line 1” >> file_name.txt
echo “Line 2” >> file_name.txt
5. Display the list of files in the directory:
Ls directory_name

5A.Draw the diagram of the argument array prepared by the


shell given to int main(int argc, char *argv[]) for the
command line $./a.out “usp dos” SOA iter CSE.
+--------+--------+--------+--------+--------+
| | | | | |
| argc | argv[0]| argv[1]| argv[2]| argv[3]| argv[4]|
| | | | | | |
+--------+--------+--------+--------+--------+
| 5 | ./a.out | "usp dos" | SOA | iter | CSE |
+--------+--------+--------+--------+--------+

B.Write the output of the following code snippet and state the
reason for such output. Assume all required headers are
present.
int main(void) {
char str[] =”SOA-ITER-IBCS-SUM-IDS-IBCS”;
char ptr[] =”CSE-CSIT-MTECH-ECE-EE-EEE”;
char *sptr,*token,*ptoken;
token=strtok(str,”-”);
ptoken=strtok_r(ptr,”-”,&sptr);
while (ptoken!=NULL){
printf(“Token=%s---%s\n”,token,ptoken);
token=strtok(str,”-”);
ptoken=strtok_r(NULL,”-”,&sptr);
}
return 0;
}
Output:
Token=SOA---CSE
Token=ITER---CSIT
Token=IBCS---MTECH
Token=SUM---ECE
Token=IDS---EE
Token=IBCS---EEE
Explanation:
1. String Initialization:
○ str stores the string "SOA-ITER-IBCS-SUM-IDS-IBCS".
○ ptr stores the string "CSE-CSIT-MTECH-ECE-EE-EEE".
2. strtok vs. strtok_r:
○ strtok is a function that parses a string for tokens
delimited by a specific character (e.g., -).
However, it modifies the original string.
○ strtok_r is a reentrant version of strtok that takes
an additional pointer argument (sptr) to reference
the current position in the string, enabling parsing
without modifying the original string.
3. Tokenization:
○ The first call to strtok(str, "-") extracts the
first token ("SOA") from str and stores it in token.
○ The first call to strtok_r(NULL, "-", &sptr) uses a
null pointer for the string and initializes sptr to
point to the beginning of ptr. This effectively
starts parsing ptr from the beginning. It extracts
the first token ("CSE") and stores it in ptoken.
4. Looping:
○ The loop iterates as long as ptoken is not null.
○ Inside the loop:
■ printf prints the current tokens from token and
ptoken separated by "---".
■ The second call to strtok(str, "-") extracts
the next token ("ITER") from str and stores it
in token.
■ The second call to strtok_r(NULL, "-", &sptr)
uses the updated value of sptr to continue
parsing ptr. It extracts the next token
("CSIT") and stores it in ptoken.
5. Output:
○ The loop repeats until all tokens are extracted.
This results in the displayed output, with matching
tokens from both strings.
C.Construct the process tree diagram with the output of the
following code snippet assuming fork never fails.
int main(void){
int i;pid_t childpid;
for(i=1;i<8;i++){
childpid=fork();
if(childpid<=-1)
break;
}
printf(“is-a-parent relationship\n”);
return 0;
}

NO BLOODY IDEA ABOUT THIS ONE

You might also like