Osw Assignment 3
Osw Assignment 3
W EEK -E ND A SSIGNMENT-03
Operating Systems Workshop (CSE 3541)
Problem Statement:
Working with repetition control structure (for, while and do-while) in programming.
Assignment Objectives:
To learn how to use C repetition control structure in programming and when to use each type in developing
programs.
Output
Code snippet: 1(c)
123456789
int a=1;
do {
printf("%d ", a++);
} while ( a < 10 );
1
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
Output
Code snippet: 1(d) 11
int i, j,n=5; 31
for(i=1, j=1; j<= n; i+= 2, j++){ 51
71
printf("%d%d\n", i, j); 91
}
Output
count down is 0
Code snippet: 1(e)
int count = 11;
while (--count+l);
printf("count down is %d \n",count);
2. During execution of the following program segment, how many lines of hash marks are displayed?
Output
45
for (m = 9; m > 0; --m)
for (n = 6; n > 1; --n)
printf("#########\n");
Outputt
for (m = 10; m > 0; --m){ a. 10 times
for (n = m; n > 1; --n)
printf("%d ", m + n); b. 9 times
printf("\n"); c. The last value
} displayed is 1,
2
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
4. 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. Test it on the following numbers:
n = 154368
n = 621594
n = 123456
Hint: Use the % operator to get each digit; then use / to remove that digit. So 154368 % 10 gives 8
and 154368 / 10 gives 15436. The next digit extracted should be 6, then 3 and so on.
#include <stdio.h>
int main() {
int n, sum, digit;
sum = 0;
while (n > 0) {
digit = n % 10;
sum += digit;
n /= 10;
}
if (sum % 9 == 0) {
printf("%d is divisible by 9.\n", n);
} else {
printf("%d is not divisible by 9.\n", n);
}
printf("The digits of %d, starting with the rightmost digit, are: ", n);
while (n > 0) {
digit = n % 10;
printf("%d ", digit);
n /= 10;
}
printf("\n");
return 0;
}
Output:-
Enter a number: 154368
154368 is divisible by 9.
The digits of 154368, starting with the rightmost digit, are: 8 6 3 4 1 5
3
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
5. The greatest common divisor (gcd) of two integers is the product of the integers common factors.
Write a program that inputs two numbers and implements the following approach to finding their gcd.
We will use the numbers −252 and 735. Working with the numbers’ absolute values, we find the
remainder of one divide by the other.
0
735 252
- 0
252
Now we calculate the remainder of the old divisor divided by the remainder found.
2
252 735
- 504
231
4
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
#include <stdio.h>
#include <stdlib.h>
// While b is not equal to 0, find the remainder of a divided by b and assign the remainder to a.
while (b != 0) {
int remainder = a % b;
a = b;
b = remainder;
}
int main() {
int a, b;
return 0;
}
5
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
6. Write a program to process a collection of the speeds of vehicles. Your program should count and
print the number of vehicles moving at a high speed (90 miles/hour or higher), the number of vehicles
moving at a medium speed (50-89 miles/hour), and the number of vehicles moving at a slow speed
(less than 50 miles/hour). It should also display the category of each vehicle. Test your program on
the following data in a file:
43 23 54 57 68 67 51 90 33 22 11 88 34 52 75 12 78 32 89 14 65 67 97
53 10 47 34
a. Also code to display the average speed of a category of vehicle (a real number) at the end of the
run.
b. Store the data into a file vspeed.txt. Use input redirection to read all numbers from that file.
(i.e. ./a.out < vspeed.txt)
c. While reading the input from the file, apply the idea of scanf function returns. The scanf
returns: (1) On success, this function returns the number of input items successfully matched and assigned (ii) The
value EOF is returned if the end of input is reached before either the first successful conversion or a matching
failure occurs.
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int s;
int c;
} V;
6
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
int speed;
int count = 0;
while ((fscanf(file, "%d", &speed)) != EOF) {
v[count].s = speed;
v[count].c = (speed >= SPEED_CATEGORY_HIGH) ? SPEED_CATEGORY_HIGH :
(speed >= SPEED_CATEGORY_MEDIUM) ? SPEED_CATEGORY_MEDIUM :
SPEED_CATEGORY_LOW;
count++;
}
fclose(file);
}
int main() {
V* v = malloc(sizeof(V) * 100);
if (v == NULL) {
printf("Error allocating memory for vehicles.\n");
exit(1);
}
dr(v, 100);
free(v);
return 0;
}
7
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
7. A baseball player’s batting average is calculated as the number of hits divided by the official number
of at-bats. In calculating official at-bats, walks, sacrifices, and occasions when hit by the pitch are
not counted. Write a program that takes an input file containing player numbers and batting records.
Trips to the plate are coded in the batting record as follows: H-hit, O-out, W-walk, S-sacrifice, P-hit
by pitch. The program should output for each player the input data followed by the batting average.
Each batting record is followed by a newline character. Your program should not use any kind of
array and array processing.
Corresponding output:
int main() {
// Open the input file.
FILE* input_file = fopen("input.txt", "r");
if (input_file == NULL) {
printf("Error opening input file.\n");
return 1;
}
// Read the player number and batting record from the input file.
int player_number;
char batting_record[100];
while (fscanf(input_file, "%d %s", &player_number, batting_record) != EOF) {
// Calculate the number of hits and at-bats for the player.
int hits = 0;
int at_bats = 0;
for (int i = 0; i < strlen(batting_record); i++) {
if (batting_record[i] == 'H') {
hits++;
} else if (batting_record[i] == 'O') {
at_bats++;
}
}
return 0;
}
8
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
9
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
8. 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:
8
23 67 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
a. Read the same input from a file using input redirection. First line represents number of students
and rest of the lines represent the marks obtained by each student in 4 subjects.
b. Display average score and grade of each student in form of a table.(Hint: Average score of a
student=(m1 + m2 + m3 + m4 )/4. where mi , i = 1, 2, 3, 4 represent mark in subject i and
calculate grade according to the specified condition given above.
#define GRADE_A 80
#define GRADE_B 65
#define GRADE_C 40
#define GRADE_F 0
typedef struct {
int scores[4];
float average_score;
char grade;
} Student;
return 'F';
}
}
// Function to display the average score and grade of each student in the form of a table.
void displayStudentScoresTable(Student* students, int num_students) {
printf("Student Number\tAverage Score\tGrade\n");
for (int i = 0; i < num_students; i++) {
printf("%d\t\t%.2f\t\t%c\n", i + 1, students[i].average_score, students[i].grade);
}
}
int main() {
// Open the input file.
FILE* input_file = fopen("input.txt", "r");
if (input_file == NULL) {
printf("Error opening input file.\n");
return 1;
}
#include <stdio.h>
int main() {
int n = 7;
return 0;
}
12
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
#include <stdio.h>
#include <math.h>
int main() {
// Get the input value for x.
float x;
printf("Enter a value for x: ");
scanf("%f", &x);
return 0;
}
13
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
1. Factorial of a number.
2. Prime or not
3. Odd or even
4. Exit
Use input-validation loop and program should terminate only when option 4 is selected.
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int isPrime(int n) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
int isEven(int n) {
return (n % 2 == 0);
}
int main() {
int option;
int number;
do {
printf("Menu:\n");
printf("1. Factorial of a number\n");
printf("2. Prime or not\n");
printf("3. Odd or even\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &option);
);
14
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
return 0;
}
15