0% found this document useful (0 votes)
47 views8 pages

BSCS CC-112 PF 2022 Solved

Uploaded by

anamkhurshid1730
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)
47 views8 pages

BSCS CC-112 PF 2022 Solved

Uploaded by

anamkhurshid1730
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/ 8

WHITE

White
Science college wahdat road

PF 2022 SOLVED PAST PAPER


Short + Long

Short Questions
Q1: Consider the following code. What will be the printed to the
console when the second iteration of for loop runs ?
#include<stdio.h>
#include<string.h>
void display(int v)
{
v+=3;
}
int main(){
int v=15 ;
while(v<20 && v>10)
{
display(v);
printf("%d", v--);
printf(",") ;
}
return 0 ;
}

Answer : 14,

Q2: How many times for loop in the following program will execute
?
#include<stdio.h>
#include<string.h>

int main(){
int i = 4 ;
for (;i>=1;)
printf("PF-2021");
return 0 ;
}

Answer : Infinite times (because there is no updating i— statement )


Q3: Suppose you have an array: int arr2D[4][2] = { 22 , 33 ,11 ,2 }; What
will be the output of printf(“%d” , arr2D[1][1]) ;

Answer : 2

Column0 Column1
Row0 22 33
Row1 11 2

Q4: What will the output of following program ?

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

int main() {
FILE *file;
char c[100]; // Buffer to store the read string
// Open file in write mode and write a string to it
file = fopen("file.txt", "w");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
fprintf(file, "Dirilis Ertugrul is a Turkish historical fiction");
fclose(file);
// Open file in read mode and perform fseek and fscanf operations
file = fopen("file.txt", "r");
// Move file pointer to the 4th character (index 3)
fseek(file, 3, SEEK_SET);
// Read the word from the new position
fscanf(file, "%s", c);
// Print the word
printf("%s", c);
// Close the file
fclose(file);
return 0;
}

Answer: ilis
Q5: What will be the output of following program ?
#include <stdio.h>
#include <string.h>

int main() {
int arrA[2][4] = {1 ,2 ,3,4,1,2,3,4};
int i= 0 ;
printf("%d" , arrA[i++][i]);
return 0 ;
}

Answer: 2
arrA[i++][i] = arrA[0][1]

(2,4) Column0 Column1 Column2 Column3


Row0 1 2 3 4
Row1 1 2 3 4

Q6: Consider the following code. How many times If statement inside the
white loop will execute ?

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

int main() {
int a = 12, b = 14 ;
while (a>10)
{
printf("w\n");
if(a==b)
b = b+1 ;
a = b - (b-a);
}
return 0 ;
}

Answer : Infinite loop ( because a==b just comparing ) it does not changing the
value of a and b
Long Questions

Q.2. Answer the following questions.


Part A
Write a C program that solves the 3n+1 problem
Consider the following algorithm to generate a sequence of members.
Start with an integer. If n is even, divide by 2. If n is odd, multiply by 3
and add 1. Repeat this process with the new value of n, terminating when
n =1. For example, the following sequence of numbers will be generated
for n=22:
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

For an input n, the cycle-length of n is the number of numbers generated


up to and including the 1. In the example above, the cycle length of 22 is
16. Given any two numbers i and j, you are to determine the maximum
cycle length over all numbers between i and J, including both endpoints.

Input:
The input will consist of a series of pairs of integers i and j and pair of
integers per line. All integers will be less than 1,000,000 and greater than
0.

Output:
For each pair of input integers i and j, output i, j in the same order in
which they appeared in the input and then the maximum cycle length for
integers between and including i and j. Those three numbers should be
separated by one space, with all three numbers on one line and with one
line of output for each line of input.

Sample Input
1 10
100 200
201 210
900 1000

Sample output
1 10 20
100 200 125
201 210 89
900 1000 174
#include <stdio.h>
// Function to compute the cycle length of a number
int cycle_length(int n) {
int length = 1; // Start with 1 to include the initial number itself
while (n != 1) {
if (n % 2 == 0) {
n /= 2;
} else {
n = 3 * n + 1;
}
length++;
}
return length;
}
// Function to find the maximum cycle length in the range [i, j]
int find_max_cycle_length(int i, int j) {
if (i > j) {
// Swap i and j if i > j to ensure i <= j
int temp = i;
i = j;
j = temp;
}
int max_length = 0;
for (int n = i; n <= j; n++) {
int current_length = cycle_length(n);
if (current_length > max_length) {
max_length = current_length;
}
}
return max_length;
}
int main() {
int i, j;
// Prompt the user for input
printf("Enter pairs of integers (i j) and press Ctrl+D (or Ctrl+Z on
Windows) to end input:\n");
while (1) {
// Read integers i and j from user input
printf("Enter i and j: ");
if (scanf("%d %d", &i, &j) != 2) {
break; // Exit loop if input is invalid or end-of-file is reached
}
int max_length = find_max_cycle_length(i, j);
// Print the result in the required format
printf("%d %d %d\n", i, j, max_length);
}
return 0;
}
Part B
For each of the following, write C statements that perform the specified
task.
a) Declare a built-in array of type int called values with five elements,
and initialize the elements to the even integers from 2 to 10.
Assume that the constant SIZE has been defined as 5.
#include <stdio.h>
#define SIZE 5 // Define the constant SIZE
int main() {
// Declare and initialize the array with even integers from 2 to 10
int values[SIZE] = {2, 4, 6, 8, 10};
// Print the values to verify initialization
for (int i = 0; i < SIZE; i++) {
printf("values[%d] = %d\n", i, values[i]);
}
return 0;
}

b) Declare a pointer vPtr that points to an object of type unsigned int.


unsigned int *vPtr = NULL;
c) Use a for statement to display the elements of built-in array values
using array subscript notation.
#include <stdio.h>
#define SIZE 5 // Define the size of the array
int main() {
// Declare and initialize the array
int values[SIZE] = {2, 4, 6, 8, 10};
// Use a for loop to display the elements of the array
for (int i = 0; i < SIZE; i++) {
printf("values[%d] = %d\n", i, values[i]);
}
return 0;
}

d) Write two separate statements that assign the starting address of


built-in array values to pointer variable vPtr.
#include <stdio.h>

int main() {
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {6, 7, 8, 9, 10};
// Pointer variables to hold the starting address of arrays
int *vPtr1;
int *vPtr2;
// Assign the starting address of arr1 to vPtr1
vPtr1 = arr1;
// Assign the starting address of arr2 to vPtr2
vPtr2 = arr2;
// Print the values to verify
printf("The first element of arr1 via vPtr1: %d\n", *vPtr1);
printf("The first element of arr2 via vPtr2: %d\n", *vPtr2);
return 0;
}

e) Use a for statement to display the elements of built-in array values


using pointer/offset notation.
#include <stdio.h>

int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr; // Pointer to the first element of the array
// Use a for loop to display elements using pointer/offset notation
for (int i = 0; i < 5; i++) {
printf("Element %d: %d\n", i, *(ptr + i));
}
return 0;
}

f) Use a for statement to display the elements of built-in array values


using pointer/offset notation with the built-in array's name as the
pointer.
#include <stdio.h>

int main() {
int arr[] = {10, 20, 30, 40, 50};

// Use a for loop to display elements using pointer/offset notation


for (int i = 0; i < 5; i++) {
printf("Element %d: %d\n", i, *(arr + i));
}

return 0;
}
g) Use a for statement to display the elements of built-in array values
by subscripting the pointer to the built-in array.
#include <stdio.h>

int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr; // Pointer to the first element of the array
// Use a for loop to display elements using pointer subscripting
for (int i = 0; i < 5; i++) {
printf("Element %d: %d\n", i, ptr[i]);
}
return 0;
}

Part C
Write a C program that prints the following pattern on the screen. You
must use nested loops
otherwise ZERO will be awarded.

1 2 3 4 5 6
6 8 10 12 14 16
16 19 22 25 28 31
31 35 39 43 47 51
51 56 61 66 71 76
76 82 88 94 100 106
#include <stdio.h>

int main() {
int startValue = 1; // Initial value for the first row
int increment;

// Loop through each row


for (int i = 0; i < 6; i++) {
increment = 2 * i + 1; // Adjust the increment based on the row index
for (int j = 0; j < 6; j++) {
printf("%-4d", startValue); // Print the current value
startValue += 2; // Update the value for the next column
}
printf("\n");
// Update the starting value for the next row
startValue = startValue - 2 + increment; // Adjust starting value for
the next row
}
return 0;
}

You might also like