0% found this document useful (0 votes)
50 views

CSC103 - Subjective Part (Fall 2020) 1

The document is a past exam paper for a Programming Fundamentals course. It contains 4 questions asking students to: 1) Provide iterative and recursive implementations of two coding problems and draw call stacks for the recursive solutions. 2) Code an application to count the number of times each number 1-9 appears in a 2D array. 3) Find the length of the longest identical character fragment in a string. 4) Write randomly generated numbers to a text file, read them back sorted in descending order.

Uploaded by

Zojaan Aheer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

CSC103 - Subjective Part (Fall 2020) 1

The document is a past exam paper for a Programming Fundamentals course. It contains 4 questions asking students to: 1) Provide iterative and recursive implementations of two coding problems and draw call stacks for the recursive solutions. 2) Code an application to count the number of times each number 1-9 appears in a 2D array. 3) Find the length of the longest identical character fragment in a string. 4) Write randomly generated numbers to a text file, read them back sorted in descending order.

Uploaded by

Zojaan Aheer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

COMSATS University Islamabad, Islamabad campus

Department of Computer Science

Terminal Examination (Subjective Part), Fall 2020

Class: BCS/BSE 2 Marks: 30


Subject: CSC103 Programming Fundamentals Time: 02 hrs.
Instructors: Dr. Manzoor / Mr. Rizwan / Ms. Saadia / Dr. Behjat Dated: January 11, 2021

Question 01 – Recursion Points: 06


Take two coding problems that can be solved recursively. You are required to provide their:
a. Iterative implementation
b. Recursive implementation
c. Call stack information of recursive implementation (you can draw call stacks for each problem and
insert pictures in the word file)

Answer: Example: Muhammad Farhan sp20-bse-049

Iterative implementation GCD of two numbers

package javaapplication36;

import java.util.Scanner;

public class JavaApplication36 {

public static void main(String[] args) {

Scanner Input = new Scanner (System.in);

System.out.println("enter number 1: ");

int a = Input.nextInt();

System.out.println("enter number 2: ");

int b = Input.nextInt();

int num1=0;

int num2;

if (a > b)

Page 1 of 9
num1 = b;

if (a < b)

num1 = a;

num2 = num1/2;

while (num2 != 0){

if ( a % num2 == 0 && b % num2 == 0)

break;

num2 -= 1;

System.out.println("Greatest common divisor" + "is" + num2);

Recursive implementation GCD finder

package javaapplication37;

import java.util.Scanner;

public class GCDRECURSIVE {

Page 2 of 9
public static void main(String[] args) {

Scanner input = new Scanner (System.in);

System.out.println("Enter number1: ");

int num1 = input.nextInt();

System.out.println("Enter number2: ");

int num2 = input.nextInt();

int hcf = hcf(num1, num2);

System.out.printf("G.C.D of %d and %d is %d.", num1, num2, hcf);

System.out.println();

public static int hcf(int num1, int num2)

if (num2 != 0)

return hcf(num2, num1 % num2);

else

return num1;

Call stack

Page 3 of 9
Example 2:

Iterative implementation

package javaapplication39;

public class JavaApplication39 {

public static void main(String[] args) {

int num = 6;

int fact = 1;

for(int i = 1; i<=num; i++) {

fact = fact * i;

System.out.println("Factorial of the given number is:: "+fact);

Recursive implementation Factorial of 6

package javaapplication38;

public class JavaApplication38 {

Page 4 of 9
public static void main(String[] args) {

int factorial = fact(6);

System.out.println("Factorial of 6 is: "+factorial);

public static int fact(int n){

int out;

if(n==1){

return 1;

out = fact(n-1)* n;

return out;

Call stack

Question 02 – Two-dimensional array Points: 10

Page 5 of 9
Code an application that displays the number of times a value appears in a two-dimensional array. It displays the
number of times each of the numbers from 1 through 9 appears in the numbers array.
Example: Suppose the user entered the following array.
1 2 7
2 2 2
1 1 7

Output: The value 1 appears 3 times.


The value 2 appears 4 times.
The value 7 appears 2 times.

package javaapplication36;

public class JavaApplication36 {

public static void main(String[] args) {


int[][] num = new int[][]{{4,4,4,8},{1,2,4,4},{4,7,9,5}};
Occurence(num);
}
public static void Occurence(int[][] num){

for (int i = 1; i < 9; i++) {


int count = 0;
for (int j = 0; j < num.length; j++) {
for (int k = 0; k < num[j].length; k++) {
if (i == num[j][k]) {
count++;
}
}
}
if (count > 0) {
System.out.println("The value " + i + " appears " + count + " times.");
}
}

Page 6 of 9
Question 03 – Strings Points: 04
Given a string, determine the length of the widest/biggest fragment, where all the characters of the fragment are same.
Example: abbbbacacccafffd
Output: 4
package javaapplication41;

public class JavaApplication41 {

public static void main(String[] args) {


String str = "zzzzzavvvaffaggggggaadddnnnncccafffdddddddd";
Max(str);
}
public static void Max(String str) {
int m = 0;
char element = 0;

for (int i = 0; i < str.length(); i++) {


int count = 1;
for (int j = i; j < str.length(); j++) {
element = str.charAt(i);
if (str.charAt(i) == str.charAt(j)) {
if (++count > m) {
m = count;
}
} else {
i = j;
break;
}

Page 7 of 9
}
}

System.out.println("the character is " + element + " and occurence is " + m);


}

Question 04 – Text I/O Points: 10


Write a program to create a file named randomNumbers.txt if it does not exist. Write 25 numbers, generated randomly,
to the file using text I/O. The numbers must be separated by spaces in the file. Read the data back from the file and
display the data in decreasing order.
package javaapplication42;

import java.util.*;
import java.io.*;
public class JavaApplication42 {

public static void main(String[] args) throws Exception{


File file = new File("randomnumbers.txt");

try (

PrintWriter output = new PrintWriter(file);


){

Page 8 of 9
for (int i = 0; i < 25; i++) {
output.print(((int)(Math.random() * 10000) + 1));
output.print(" ");
}
}
ArrayList<Integer> array = new ArrayList<>();

try(

Scanner input = new Scanner(file);


){

while (input.hasNext()) {
array.add(input.nextInt());
}
}
Collections.sort(array);

System.out.print(array);
System.out.println();

Page 9 of 9

You might also like