KEMBAR78
OSW Assignment - 06 Complete | PDF | Pointer (Computer Programming) | Integer (Computer Science)
0% found this document useful (0 votes)
13 views17 pages

OSW Assignment - 06 Complete

The document outlines a weekend assignment for a computer science course focused on pointers and modular programming. It includes a series of programming tasks that require students to work with pointers, structures, and arrays in C, emphasizing indirect referencing and the differences between call-by-value and call-by-reference. Students are instructed to write their own programs without copying from sources and to demonstrate their understanding through various coding exercises.

Uploaded by

mardiblbiswajit
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)
13 views17 pages

OSW Assignment - 06 Complete

The document outlines a weekend assignment for a computer science course focused on pointers and modular programming. It includes a series of programming tasks that require students to work with pointers, structures, and arrays in C, emphasizing indirect referencing and the differences between call-by-value and call-by-reference. Students are instructed to write their own programs without copying from sources and to demonstrate their understanding through various coding exercises.

Uploaded by

mardiblbiswajit
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/ 17

Department of Computer Science and Engineering

Institute of Technical Education & Research, SOA, Deemed to be University


W EEK -E ND A SSIGNMENT -06
Pointers and Modular programming
Operating Systems Workshop (CSE 3541)
Problem Statement:
Working with pointers, referencing a variable through a pointer and accessing the contents of a memory cell
through a pointer variable that stores its address (i.e. indirect reference).

Assignment Objectives:
To learn about pointers, referencing, indirect referencing and how to return function results through a func-
tion‟s parameters (input parameters, input/output parameters, output parameters). Also to understand the
differences between call-by value & call-by-reference.

Instruction to Students (If any):


Students are required to write his/her own program by avoiding any kind of copy from any sources.
Additionally, They must be able to realise the outcome of that question in relevant to systems pro-
gramming. You may use additional pages on requirement.

Programming/ Output Based Questions:


1. For the given structure below, declare the variable type, and print their values as well as addresses;

Ia Fb Chvar ←−V ariable name

345 4.5 Z ←− V alue

Space for Program ▼ Output ▼


#include <stdio.h>
struct SampleStruct {
int integerVar;
float floatVar;
char charArray[20];
};
int main() {
struct SampleStruct myStruct;
myStruct.integerVar = 345;
myStruct.floatVar = 4.5;
strcpy(myStruct.charArray, "Variable name");}

2. Declare two integer variable and assign values to them, and print their addresses. Additionally, Swap
the contents of the variables and print their addresses after swap. State whether the addresses before
and after are equal or not.
Space for Program ▼ Output ▼

#include <stdio.h>
int main() {
int a, b;
int *ptr_a, *ptr_b;
a = 10;
b = 20;
ptr_a = &a;
ptr_b = &b;
} 1
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

3. Write the C statement to declare and initialize the pointer variable,p, for the given structure and
display the values of x, y and z with the help of p.

x y z

6.7 1.2 2.3

p
Space for Program ▼ Output ▼

#include <stdio.h>
struct MyStruct {
float x;
float y;
float z;
};
int main() {
struct MyStruct instance = {6.7, 1.2,
2.3};
struct MyStruct *p = &instance;
return 0;
}

4. Write the C statement to declare and initialize the pointer variables p1, p2 and p3 for the given
structure and display the value of x from p1. Also update the value of x to 100 using pointer p3.
p1 p2 p3

89
x
Space for Program ▼ Output ▼
#include <stdio.h>
struct MyStruct {
int x;
};

int main() {
struct MyStruct instance = {89};
struct MyStruct *p1, *p2, *p3;
p1 = p2 = p3 = &instance;
p3->x = 100;
return 0;
}

2
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

5. Write the C statement to declare and initialize the pointer variable for the given structure and update
the values of a, b and c to be incremented by 10 through the pointer variable.

a b c

12 25 18

case-2
&a
ptr
Space for Program ▼ Output ▼
#include <stdio.h>
struct MyStruct {
int a;
int b;
int c;
};
int main() {
struct MyStruct instance = {12, 25,
18};
struct MyStruct *ptr = &instance;
ptr->a += 10;
ptr->b += 10;
ptr->c += 10;
return 0;
}

6. Write the C statement to declare and initialize the pointer variable for the given structure and update
the values of a, b and c to be incremented by 10 through their respective pointers.
a b c
12 52 8
case-1

case-2

case-3

&a &b &c


ptr1 ptr2 ptr3
Space for Program ▼ Output ▼
#include <stdio.h>
struct MyStruct {
int a;
int b;
int c;
};
int main() {
struct MyStruct instance = {12, 52,
8};
int *ptr1 = &instance.a;
int *ptr2 = &instance.b;
int *ptr3 = &instance.c;
*ptr1 += 10;
*ptr2 += 10;
*ptr3 += 10;

3
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

7. Two pointers are pointing to different variables. Write the C statement to find the greater between a,
and b using pointer manipulation.

ptr1 a b ptr2

&a 52 18 &b

Space for Program ▼ Output ▼

#include <stdio.h>
int main() {
int a = 52;
int b = 18;
int *ptr1 = &a;
int *ptr2 = &b;
int greater;
if (*ptr1 > *ptr2) {
greater = *ptr1;
} else {
greater = *ptr2;
}

8. Create a program to display the address and value of each element of the given integer array a. Also
perform a close observation on the format of the address and the change of address from index 0 to
the last index of the array.

0 1 2 3 4 5 6 7 8 9
0 10 20 30 40 50 60 70 80 90
&a[0]&a[1]&a[2]&a[3]&a[4]&a[5]&a[6]&a[7]&a[8]&a[9]

Space for Program ▼ Output ▼

#include <stdio.h>

int main() {
int a[] = {10, 20, 30, 40, 50, 60, 70, 80, 90};
int length = sizeof(a) / sizeof(a[0]);
for (int i = 0; i < length; i++) {
printf("%d\n", i);
printf("%d\n", a[i]);
printf("&a[%d]\n", i);
printf("%p\n", &a[i]);
}
return 0;
}

4
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

9. Declare the two arrays to hold the values as shown in the given rectangular boxes. Write the equivalent
C statement to print their values and addresses through pointer (Hint: an array name is a pointer to
the first element in the array).

10 13 20 33 44 10.2 13.3 20.0 33.3 45.3 89.9

Space for Program ▼ Output ▼


#include <stdio.h>

int main() {
int array1[] = {1, 2, 3, 4, 5};
int array2[] = {10, 20, 30, 40, 50};
printf("Values and addresses of array1:\n");
for (int i = 0; i < 5; i++) {
printf("Value at index %d: %d\n", i, array1[i]);
printf("Address of element %d: %p\n", i, &array1[i]);
}
printf("\nValues and addresses of array2:\n");
for (int i = 0; i < 5; i++) {
printf("Value at index %d: %d\n", i, array2[i]);
printf("Address of element %d: %p\n", i, &array2[i]);
}

return 0;
}

10. Write the C statement to declare and initialize the pointer variable for the given structure and display
the array content using pointer.

Index 0 1 2 3 4 5
Array a 120 502 118 188 106 447

&a[0] &a[1] &a[2] &a[3] &a[4] &a[5]


ptr1 ptr2 ptr3 ptr4 ptr5 ptr6

Space for Program Output ▼


#include <stdio.h>
int main() {
int a[] = {120, 502, 118, 188, 106, 447};
int length = sizeof(a) / sizeof(a[0]);
int *ptr1 = a;
int *ptr2 = a + 1;
int *ptr3 = a + 2;
int *ptr4 = a + 3;
int *ptr5 = a + 4;
int *ptr6 = a + 5;
printf("Array a\n");
printf("Index\tValue\tAddress\n");
for (int i = 0; i < length; i++) {
printf("%d\t%d\t%p\n", i, *(a + i), a + i);
}
printf("\n&a[0]\tptr1\n");
printf("%d\t%d\n", *ptr1, ptr1);
printf("&a[1]\tptr2\n");
printf("%d\t%d\n", *ptr2, ptr2);

5
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

11. Write the C statement to declare and initialize the pointer variable for the given structure and display
the array content using pointer.

Index 0 1 2 3 4 5
Array a 120 502 118 188 106 447

&a[0]
ptr
Space for Program Output ▼
#include <stdio.h>
int main() {
int a[] = {120, 502, 118, 188, 106, 447};
int length = sizeof(a) / sizeof(a[0]);
int *ptr = a;
printf("Array a\n");
printf("Index\tValue\n");
for (int i = 0; i < length; i++) {
printf("%d\t%d\n", i, *(ptr + i));
}
printf("&a[0]: %p\n", &a[0]);
printf("ptr: %p\n", ptr);
return 0;
}

12. As array name is a pointer, so modify the assignment ptr=a rather ptr=&a[0]. Write the C statement
to declare and initialize the pointer variable for the given structure and display the array content using
pointer.

Index 0 1 2 3 4 5
Array a 120 502 118 188 106 447

&a[0]
ptr
Space for Program Output ▼
#include <stdio.h>
int main() {
int a[] = {120, 502, 118, 188, 106, 447};
int length = sizeof(a) / sizeof(a[0]);
int *ptr = a;
printf("Array a\n");
printf("Index\tValue\n");
for (int i = 0; i < length; i++) {
printf("%d\t%d\n", i, *(ptr + i));
}
printf("&a[0]: %p\n", &a[0]);
printf("ptr: %p\n", ptr);
return 0;
}

6
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

13. Trace the execution of the following fragment at line -1.


int m = 10, n = 5; Output▼
int *mp, *np;
mp = &m; 15 15
np = &n; -5 -5
*mp = *mp + *np;
*np = *mp - *np;
printf("%d %d\n%d %d\n", m, *mp, n, *np); /*
line-1 */

14. Given the declarations;


int m = 25, n = 77;
char c = ’*’; Output▼
int *itemp;
/* describe the errors in each of the Trying to store pointer value in
following statements. */ integer.variable.
m = &n;
itemp = m;
*itemp = c;
*itemp = &c;

15. Simulate the following structure in C to store 55 in a, 105 in b, 89 in c and 68 in d using their re-
spective pointers. Additionally find the maximum among a, b, c and d through pointer manipulation.
Finally Store the maximum to the required variable and display the maximum.
C simulation▼
#include <stdio.h>
struct Data {
An array int a;
int b;
Ordinary variable
arr int type
int c;
int d;
a };
int main() {
0 &a
int *a_ptr = &arr.a;
int *b_ptr = &arr.b;
int *c_ptr = &arr.c;
int *d_ptr = &arr.d;
*a_ptr = 55;
b *b_ptr = 105;
*c_ptr = 89;
1 &b
*d_ptr = 68;
int *max_ptr = a_ptr;
if (*b_ptr > *max_ptr) {
max_ptr = b_ptr;
}
if (*c_ptr > *max_ptr) {
c max_ptr = c_ptr;
2 }
if (*d_ptr > *max_ptr) {
&c max_ptr = d_ptr;
}
printf("Maximum value: %d\n", maxvar);

d return 0;
3 }

&d
Maximum value: 105
maxvar
To store max of a,b,c and d

7
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

16. Simulate the following structure in C to find the element sum of the given arrays a, b, c and d into
sumarray using their respective pointers. The 1-D arrays must be read/scanned through the pointers.
An array C simulation▼
Arrays
arr int type #include <stdio.h>
a struct Arrays {
0 int a[5];
int b[5];
int c[5];
int d[5];
b };
1
int main() {
struct Arrays arr;
int values_a[] = {10, 20, 30, 40, 50};
c int values_b[] = {15, 25, 35, 45, 55};
2 int values_c[] = {5, 10, 15, 20, 25};
int values_d[] = {7, 14, 21, 28, 35};
for (int i = 0; i < 5; i++) {
arr.a[i] = values_a[i];
d arr.b[i] = values_b[i];
3 arr.c[i] = values_c[i];
arr.d[i] = values_d[i];
}
int sum_a = 0, sum_b = 0, sum_c = 0, sum_d = 0;
sumarr for (int i = 0; i < 5; i++) {
Elementwise sum sum_a += arr.a[i];
sum_b += arr.b[i];
sum_c += arr.c[i];
Test case: Input & Output▼ sum_d += arr.d[i];
}
int sumarray[] = {sum_a, sum_b, sum_c, sum_d};
Sum of a: 150 printf("Sum of a: %d\n", sum_a);
Sum of b: 175 printf("Sum of b: %d\n", sum_b);
Sum of c: 75 printf("Sum of c: %d\n", sum_c);
printf("Sum of d: %d\n", sum_d);
Sum of d: 105 return 0;
}

8
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

17. An argument array is an array of pointers to strings. The end of the array is marked by an entry
containing a NULL pointer as shown in the figure. Write a C Simulation to implement the following
figure and manipuate the character array to hold all capital case letters using pointer. Finally display
the strings.
C simulation▼

argv[ ] #include <stdio.h>


int main() {
[0] m i n e \0 char *argv[] = {
"mine",
[1] c s \0 "cs",
"sc",
[2] s c \0 "s",
"oa",
[3] s a \0 NULL
o
};
[4] NULL void toUpperCase(char *str) {
for (int i = 0; str[i]; i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] - ('a' - 'A');
}
}
}
for (int i = 0; argv[i] != NULL; i++) {
toUpperCase(argv[i]);
Test case: Input & Output▼ }

// Display the strings


argv[0]: MINE for (int i = 0; argv[i] != NULL; i++) {
argv[1]: CS printf("argv[%d]: %s\n", i, argv[i]);
argv[2]: SC }
argv[3]: S
argv[4]: OA return 0;
}

9
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

18. Consider the following figures 1, 2 and 3 to manipulate the ordinary variables, integer arrays and
strings through pointers. There exist no names associated with the variables, arrays and strings. State
the method to allocate memory for the pointers to manipulate the desired variables.

No names No names
An array
An array
arr Arrays
int type arr int type
0
0 argv[ ] No names

1 [0] m i n e \0

1 [1] c s \0
2
[2] s c \0
2
3 [3] s o a \0

maxvar 3
[4] NULL

sumarr
Elementwise sum
Figure-1 Figure-3
Figure-2

Answer ▼
a.) int *ptr;
b.) int *arr = (int *)malloc(5 * sizeof(int));
c.) char *str = (char *)malloc(50 * sizeof(char));

19. Write a prototype for a function sum n avg that has three type double input parameters and two
output parameters. The function computes the sum and the average of its three input arguments and
relays its results through two output parameters.
Output▼

void sum_n_avg(double num1, double


num2, double num3, double *sum,
double *avg);

20. The following code fragment is from a function preparing to call sum n avg (see question-19).
Complete the function call statement.
double one, two, three, sum_of_3, avg_of_3;
printf("Enter three numbers> ");
scanf("%lf%lf%lf", &one, &two, &three);
sum_n_avg( );
. . .

Define the function sum n avg whose prototype you wrote in question-19.

10
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

Space for Program Output ▼


double one, two, three, sum_of_3, avg_of_3;

printf("Enter three numbers> ");

scanf("%lf%lf%lf", &one, &two, &three);

sum_n_avg(one, two, three, &sum_of_3,


&avg_of_3);

21. Given the memory setup shown, fill in the chart by indicating the data type and value of each reference
as well as the name of the function in which the reference would be legal. Describe pointer values by

referring to cell attributes. For example, the value of valp would be “pointer to color-shaded cell”,
and the value of &many would be “pointer to gray-shaded cell“.

11
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

22. Write 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 calling function and arragne() function for the first function
call arrange(. ...... ).

Sample Run
printf("Enter SIX numbers separated by blanks> ");
12 3 56 8 20 654

/* Displays results */
printf("The numbers in ascending order are: %d %d %d %d %d %d\n", n1,
n2, n3, n4, n5, n6);
3 8 12 20 56 654

Space for Program ▼ Output ▼


#include <stdio.h>
void arrange(int *a, int *b) {
if (*a > *b) {
int temp = *a;
*a = *b;
*b = temp;
}
}
int main() {
int n1, n2, n3, n4, n5, n6;
printf("Enter SIX numbers separated by blanks> ");
scanf("%d %d %d %d %d %d", &n1, &n2, &n3, &n4, &n5, &n6);
arrange(&n1, &n2);
arrange(&n1, &n3);
arrange(&n1, &n4);
arrange(&n1, &n5);
arrange(&n1, &n6);
arrange(&n2, &n3);
arrange(&n2, &n4);
arrange(&n2, &n5);
arrange(&n2, &n6);
arrange(&n3, &n4);
arrange(&n3, &n5);
arrange(&n3, &n6);
arrange(&n4, &n5);
arrange(&n4, &n6);
arrange(&n5, &n6);
printf("The numbers in ascending order are: %d %d %d %d %d %d\n", n1, n2, n3, n4, n5, n6);
return 0;
}

Output :
Enter SIX numbers separated by blanks: 12 3 56 8 20 654
The numbers in ascending order are: 3 8 12 20 56 654

12
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

23. Show the table of values for x , y , and z that is the output displayed by the following program.
#include <stdio.h>
void sum(int a, int b, int *cp);
int main(void){ Output▼
int x, y, z;
x = 7; y = 2;
printf("x y z\n\n"); x y z
sum(x, y, &z);
printf("%4d%4d%4d\n", x, y, z);
sum(y, x, &z); 7 2 9
printf("%4d%4d%4d\n", x, y, z);
sum(z, y, &x); 7 2 7
printf("%4d%4d%4d\n", x, y, z); 7 2 2
sum(z, z, &x);
printf("%4d%4d%4d\n", x, y, z); 7 2 2
sum(y, y, &y); 7 7 2
printf("%4d%4d%4d\n", x, y, z);
return (0);
}

void sum(int a, int b, int *cp){


*cp = a + b;
}

24. (a) What values of x and y are displayed by this program? ( Hint: Sketch the data areas of main ,
trouble , and double trouble as the program executes.)
void double_trouble(int *p, int y); Output▼
void trouble(int *x, int *y);
int main(void){
int x, y; x = -10, y = 14
trouble(&x, &y);
printf("x = %d, y = %d\n", x, y);
return (0);
}
void double_trouble(int *p, int y){
int x;
x = 10;
*p = 2 * x - y;
}
void trouble(int *x, int *y){
double_trouble(x, 7);
double_trouble(y, *x);
}

(b) Classify each formal parameter of double trouble and trouble as input, output, or in-
put/output.
Formal parameter classification ▼

double_trouble = „y‟
trouble = „x‟ & „y‟

13
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

25. A finite state machine (FSM) consists of a set of states, a set of transitions, and a string of input
data. In the FSM of Figure 1, the named ovals represent states, and the arrows connecting the states
represent transitions. The FSM is designed to recognize a list of C identifiers and nonnegative
integers, assuming that the items are ended by one or more blanks and that a period marks the end
of all the data. The following table traces how the diagrammed machine would process a string
composed of one blank, the digits 9 and 5, two blanks, the letter K, the digit 9, one blank, and a
period. The machine begins in the start state.

Figure 1: Finite State Machine for Numbers and Indentifiers

Figure 2: FSM tracing for 95 and K9

14
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

Write a program that uses an enumerated type to represent the names of the states. Your program
should process a correctly formatted line of data, identifying each data item. Here is a sample of
correct input and output.
Input :

rate R2D2 48 2 time 555666

Output :

rate - Identifier
R2D2 - Identifier
48 - Number
2 - Number
time - Identifier
555666 - Number

Use the following code fragment in main , and design function transition to return the next state
for all the numbered transitions of the finite state machine. If you include the header file ctype.h,
you can use the library function isdigit which returns 1 if called with a digit character, 0 other-
wise. Similarly, the function isalpha checks whether a character is a let- ter. When your program
correctly models the behavior of the FSM shown, extend the FSM and your program to allow optional
signs and optional fractional parts (i.e., a decimal point followed by zero or more digits) in numbers.
current_state = start;
do {
if (current_state == identifier) {
printf(" - Identifier\n");
current_state = start;
} else if (current_state == number) {
printf(" - Number\n");
current_state = start;
}
scanf("%c", &transition_char);
if (transition_char != ’ ’)
printf("%c", transition_char);
current_state = transition(current_state, transition_char);
} while (current_state != stop);

15
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

FSM Inplementation ▼

#include <stdio.h>
#include <ctype.h>
enum State {
start,
identifier,
number,
stop
};
enum State transition(enum State current, char c) {
if (current == start) {
if (isalpha(c)) {
return identifier;
} else if (isdigit(c)) {
return number;
}
} else if (current == identifier) {
if (isalnum(c) || c == '_') {
return identifier;
}
} else if (current == number) {
if (isdigit(c) || c == '.') {
return number;
}
}
return stop;
}
int main() {
enum State current_state = start;
char transition_char;
do {
if (current_state == identifier) {
printf(" - Identifier\n");
current_state = start;
} else if (current_state == number) {
printf(" - Number\n");
current_state = start;
}
scanf("%c", &transition_char);
if (transition_char != ' ') {
printf("%c", transition_char);
}
current_state = transition(current_state, transition_char);
} while (current_state != stop);

return 0;
}

16
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

FSM Inplementation ▼
OUTPUT

 rate -
Identifier
rate R2D2 48 2 time 555666
 R2D2 -
Identifier
 48 -
Number
 2-
Number
 time -
Identifier
 555666 -
Number

17

You might also like