0% found this document useful (0 votes)
15 views5 pages

Worksheet 1

The document contains various programming exercises in C, C++, and SQL, including printing environment variables, using the div() function, reading dates, implementing a date class, tree traversal algorithms, and SQL queries for software cost analysis. Additionally, it features coding questions related to finding the longest increasing subsequence and printing a freedom fighter pattern. Each section provides code examples and expected outputs for clarity.

Uploaded by

jaikp000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Worksheet 1

The document contains various programming exercises in C, C++, and SQL, including printing environment variables, using the div() function, reading dates, implementing a date class, tree traversal algorithms, and SQL queries for software cost analysis. Additionally, it features coding questions related to finding the longest increasing subsequence and printing a freedom fighter pattern. Each section provides code examples and expected outputs for clarity.

Uploaded by

jaikp000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Worksheet – 1

C Programming
1. Printing Environment Variables

Question: How do you print the contents of environment variables?

Code:

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

int main(int argc, char *argv[], char *envp[]) {


for (int i = 0; envp[i] != NULL; i++) {
printf("%s\n", envp[i]);
}
return 0;
}

Input: (No user input required)

Output: Prints all environment variables.

2. Purpose of div() Function

Question: What is the purpose of the div() function in C, and how does it work? Provide an
example.

Code:

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

int main() {
div_t result = div(10, 3);
printf("Quotient: %d, Remainder: %d\n", result.quot, result.rem);
return 0;
}

Input: (No user input required)

Output:

Quotient: 3, Remainder: 1

3. Reading Date using scanf()

Question: How do you use scanf() to read the date in the form 'dd-mm-yy'?

Code:
int day, month, year;
scanf("%d-%d-%d", &day, &month, &year);
printf("Day: %d, Month: %d, Year: %d\n", day, month, year);

Input:

26-02-25

Output:

Day: 26, Month: 2, Year: 25

C++ Programming

1. Date Class Implementation

Question: Write a program that implements a date class containing day, month, and year.

Code:

#include <iostream>
using namespace std;

class Date {
public:
int day, month, year;
Date(int d, int m, int y) : day(d), month(m), year(y) {}
void display() { cout << day << "-" << month << "-" << year << endl; }
};

int main() {
Date today(26, 2, 2025);
today.display();
return 0;
}

Input: (No user input required)

Output:

26-2-2025
Data Structures
1. Tree Traversal Algorithms

Question: Traverse the given tree using Inorder, Preorder, and Postorder traversals.

Code:

struct Node {
int data;
Node* left;
Node* right;
};

void inorder(Node* root) { /* Left -> Root -> Right */ }


void preorder(Node* root) { /* Root -> Left -> Right */ }
void postorder(Node* root) { /* Left -> Right -> Root */ }

SQL Queries
1. Recovered Development Cost

Question: Display the details of packages for which the development cost has been recovered.

Query:

SELECT * FROM SOFTWARE WHERE SCOST >= DCOST;

2. Most Expensive Software in VB

Question: What is the price of the costliest software developed in VB?

Query:

SELECT MAX(SCOST) FROM SOFTWARE WHERE DEVIN = 'VB';

TCS Coding Questions


1. Longest Increasing Subsequence

Question: Find the longest increasing subsequence (LIS) in a given array of integers.

Code:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int LIS(vector<int>& arr) {


vector<int> dp;
for (int num : arr) {
auto it = lower_bound(dp.begin(), dp.end(), num);
if (it == dp.end()) dp.push_back(num);
else *it = num;
}
return dp.size();
}

int main() {
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++) cin >> arr[i];
cout << LIS(arr) << endl;
return 0;
}

Input:

9
15 27 14 38 26 55 46 65 85

Output:

2. Freedom Fighter Pattern

Question: Print the freedom fighter pattern for a given number of photos.

Code:

#include <iostream>
using namespace std;

void printPattern(int n) {
for (int i = 0; i < 2 * n - 1; i++) {
int spaces = abs(n - 1 - i) * 2;
for (int j = 0; j < spaces; j++) cout << "-";
for (char ch = 'e' - abs(n - 1 - i); ch <= 'e'; ch++) cout << ch << "-";
for (char ch = 'e' - 1; ch >= 'e' - abs(n - 1 - i); ch--) cout << ch << (ch ==
'e' - abs(n - 1 - i) ? "" : "-");
cout << endl;
}
}
int main() {
int n;
cin >> n;
printPattern(n);
return 0;
}

Input:

Output:

--------e--------
------e-d-e------
----e-d-c-d-e----
--e-d-c-b-c-d-e--
e-d-c-b-a-b-c-d-e
--e-d-c-b-c-d-e--
----e-d-c-d-e----
------e-d-e------
--------e--------

You might also like