0% found this document useful (0 votes)
4 views4 pages

Assignment 2 SPL

The document outlines the final exam for the Structured Programming Language course at United International University for Spring and Summer 2024, detailing the exam structure, rules, and five questions covering various programming tasks in C. Each question requires students to write C programs, trace code execution, identify errors, and manipulate data structures. The exam emphasizes understanding of functions, arrays, structures, and file handling in C programming.

Uploaded by

mshawon2330218
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)
4 views4 pages

Assignment 2 SPL

The document outlines the final exam for the Structured Programming Language course at United International University for Spring and Summer 2024, detailing the exam structure, rules, and five questions covering various programming tasks in C. Each question requires students to write C programs, trace code execution, identify errors, and manipulate data structures. The exam emphasizes understanding of functions, arrays, structures, and file handling in C programming.

Uploaded by

mshawon2330218
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/ 4

United International University (UIU)

Dept. of Computer Science & Engineering (CSE)


Final Exam:: Trimester: Spring 2024
Course Code: CSE 1111, Course Title: Structured Programming Language
Total Marks: 40 Duration: 2 hours
[Any examinee found adopting unfair means will be expelled from
the trimester/program as per UIU disciplinary rules.]
There are FIVE questions. Answer all the questions. Marks are indicated in the right margin.
Q.1 a) Write a C program according to the following: [4]
i. Write a function int factorial(int n) that will return the factorial of a given number. Factorial of a
number can be calculated by multiplying the numbers from 1 to n consecutively. For example,
factorial of 4 = 1×2×3×4 = 24. Assume n will not be greater than 10.
ii. Write a function int sum(int a, int b) that will return the sum of two given numbers.
iii. In the main function, take three integers as inputs and calculate the sum of the factorial of those
integers using the above functions factorial() and sum(). Note that you cannot make any
modifications to the previously defined functions.
b) Find the output of the following program (left). Notice the local and global contexts. [4]

#include<stdio.h> #include <stdio.h>


int x = 2, y = 3; #include <string.h>
int fun1(int n){
return n%11; int main() {
} char A[101] = {'\0'};
void fun2(int arr[], int n){ char B[101] = "string";
for(int i = 0; i<n; i++){
x = fun1(x) + fun1(y); strncpy(A, B, 4);
arr[i] = arr[i] + x; strncat(A, "kernel", 3);
y = fun1(y) + fun1(x);
} for(int i=0; B[i]!='\0'; i++) {
} if(B[i]=='i') {
int main(){ B[i] = '\0';
int a[] = {2, 3, 5, 7, 11}; }
fun2(a, 5); }
for(int i = 0; i<5; i++) printf("%s - %s\n", A, B);
printf("%d ", a[i]); return 0;
} }
C Code for 1(b) C Code for 2(a)
Q.2 a) Show manual tracing (every change) of variables i, A, and B of the program above at right. [4]
b) Consider the following string declaration: [4]
Char str[55]=“I love spl. Uiu has some good labs for spl.”;
Write a C program that will replace each occurrence of the word "spl" with "dsa" and print out the
resulting text. You cannot use any library functions.

Q.3 a) Identify and correct the errors of the following code: [3]
struct student{
char name[];
int ID;
}
int main() {
student s1,s2;
s1.name="Rahim";
s1.ID=101;
struct student* s_ptr = s2;
scanf("%s",&s_ptr.name);
scanf("%d",&s_ptr.ID);
}

Page 1 of 2
Q.3 b) Write a C program to store the following information about patients and perform the following [5]
operations:
i. Create a structure named Patient with the following members: name (string), age (int),
height (float), weight (float) and BMI (float).
ii. Declare an array of size 100 of type Patient structures.
iii. Take inputs (name, age, height, weight) from the keyboard and calculate the BMI of the
respective patient using the formula: weight / (height).
iv. Find and display all the information of the youngest patient with lowest age.
Q.4 a) Write the output of the program provided below on the left. [4]
b) Find the output of the code provided below on the right. [4]

#include <stdio.h> #include <stdio.h>


int power_of_2(int n) { void inc(int *ap, int dummy){
printf("Inside power_of_2(%d)\n", n); for(int i=0; i<dummy; i++){
if(n == 1) return 1; *ap = *ap + 1;
if(n % 2 != 0) return 0; ap = ap + 2;
return power_of_2(n / 2); }
} dummy = 100;
int main(void){ }
int num = 16; int main() {
power_of_2(num); int a[] = {1,2,3,4,5,6,7,8};
return 0; int dummy = 3;
} inc(&a[2], dummy);
C Code for 4(a) for(int i=0; i<8; i++)
printf("%d ",a[i]);
printf("\nDummy=%d\n", dummy);
return 0;
}
C Code for 4(b)

Q.5 a) Write a C program that does the following: [4]


 Declare an integer array arr with array size 100.
 Declare a pointer variable arrPtr and assign the array arr to it.
 Scan the elements of the array arr using the pointer arrPtr with offset.
 Find and print the largest element of arr using the pointer arrPtr.

b) Suppose, you are trying to save a simple string in a file named “string.txt”, and for this you have
written the following code:
FILE *fp = fopen("string.txt", "w");
fprintf(fp, "Yet another string\n");
fclose(fp);
i. What will happen if the file does not exist? [1]
ii. What is the difference between read and append mode? [1]
iii. Write C code segment to re-open the same file in append mode and add a new string “This
[2]
is another string” into the file and then close the file.

Page 2 of 2
United International University (UIU)
Dept. of Computer Science & Engineering (CSE)
Final Exam:: Trimester: Summer 2024
Course Code: CSE 1111, Course Title: Structured Programming Language
Total Marks: 50 Duration: 2 hours
[Any examinee found adopting unfair means will be expelled from the
trimester/program as per UIU disciplinary rules.]
There are FIVE questions. Answer all the questions. Marks are indicated in the right margin.
Q.1 a) Implement getSumMean(float marks[ ], int N, int type) function, which computes sum and mean
of marks of N students from an input array A.
i. In the main() function, initialize an array of floats to store the marks of 100 students. Take [1]
the values from the user as input.
ii. Take 2 values as input from user: an integer (number of elements) & an integer (value type). [1]
iii. Call the getSumMean() function from the main() passing those values. If the value type is 1,
then calculate and return the sum of N elements in the array A. If the value type is 2, then [2]
calculate and return the average/mean of N elements in the array A.
iv. What type of argument passing is used in getSumMean() function call? Call-by-value, or call- [1]
by-reference? Justify and explain.

b) Find the output of the following program (left). Notice the local and global contexts. [5]

int x = 6, y = 8; #include <stdio.h>


int func (int a, int b) { #include <string.h>
a *= 2;
printf("a = %d, b = %d.\n", a, b); int main() {
return (--a) * (b--); char A[50] = "Structured";
} char B[50] = "Coding";
int sub (int a, int b) { int len = strlen(A);
b /= 6; for (int i=0; i<strlen(B); i++)
printf("a = %d, b = %d.\n", a, b); A[len-i-1] = B[i];
return (--a) * (--b);
} strncat(A, B, 2);
int main() {
y = func(x, y); for (int j=0; j<4; j++)
printf("x = %d, y = %d.\n", x, y); A[j] = B[strlen(B)-j-1];
x = sub(x, y); printf("A: %s\n", A);
printf("x = %d, y = %d.\n", x, y); printf("B: %s\n", B);
return 0; return 0;
} }
C Code for 1(b) C Code for 2(a)
Q.2 a) Show manual tracing (every change) of variables i, j, A, and B of the program above at right. [5]
b) Write a C program to replace every nth character of a string with the letter ‘Z’. You cannot use any [5]
library functions. For example, if string contains “HelloWorld” and n=3, print “HeZloZorZd”.
Another example: if string contains “Programming” and n=2, print “PZoZrZmZiZg”.
Q.3 a) Identify and correct the errors of the following code: [3]
struct Gadget {
int serialNo,
char category[],
float price;
}
int main() {
struct g1;
serialNo = 123;
category = “Tab”;
price = 5500.00;
scanf("%c",g1.category);
scanf("%s",&G1.price);
}

Page 1 of 2
Q.3 b) Suppose you are developing a C program for the UIU Sports Club to store and manage players’
performance for the VC Cup football Season 3 to be held in 2025. Your task is to create a C
program that can store, manage and manipulate the data to select best goal scorer. Write a C
program that will:
i. Create a structure named player to store the following information of each player [1]
participating in the tournament as follows (use appropriate data types and variable names
for all the features):
- Name
- ID
- Number of matches played, and
- An array G[ ] of scored goals in each of the matches played.
ii. Scan information as input from the user for 120 players registered in the tournament. [1]
iii. Write a function goalsScored(struct player p, int matches) that takes a player structure [2]
and total number of matches played and returns the sum of the elements of array G[ ] to
get total goals scored by a particular player.
iv. Write a function selectBestScorer(struct player b[ ], int total_players) that takes an array [3]
of player structure and total number of players and returns the index of the player array
representing the player with highest number of goals scored.
Q.4 a) Write the output of the program provided below on the left. What type of argument passing is used [5]
in mystery() function call? Call-by-value, or call-by-reference? Justify and explain.
b) Find the output of the code provided below on the right. [5]
int fun(int *a, int *b){ void f(char *s1, char *s2, int n, int *a) {
if (*a > 1) int i;
return *a + *b; for(i=0; i<n; i++) {
else if(s1[i] > s2[i]) *a = *a+1;
return *b - *a; else if(s1[i]<s2[i]) *(s1+i)=*(s2+i);
} else if(s1[i] == s2[i]) *a = *a-1;
}
int main(void) { }
int x = -10; void main() {
int y = 20; int a, b;
char s1[] = "worldcupBangladesh";
printf("The result is %d", char s2[] = "BanglaWash";
fun(&x, &y)); a = 0;
} f(s1,s2,13,&a);
printf("%d\n s1=%s , s2=%s\n",a,s1,s2);
}
C Code for 4(a) C Code for 4(b)

Q.5 a) Write a C program that does the following: [5]


 Declare an integer array A with arraysize 20, a pointer variable aPtr and assign array A to aPtr.
 Scan the elements of the array A using the pointer aPtr with offset.
 Write a function swap(int *a, int *b) to swap any 2 values from array A[ ] using pointers. Call
swap() function from the main() to swap array elements A[5] and A[11].
b) Suppose, a file “students.dat” is created, where each line contains a student’s name followed by
his/her cgpa and the file already have information of 5 students. Now, answer the following
questions:
i. What is the difference between read, write and append mode? [1]
ii. Which mode you should open the file? What will happen if the file does not exist? [1]
iii. Write corresponding C program to open the file, accomplish the above task and close the
[3]
file.

Page 2 of 2

You might also like