Assignment 1 Week 1 961
Assignment 1 Week 1 961
Date: 10/12/24
Name: Barnavo Dey
Reg No: RA2211003011961
ANSWERS
C PROGRAMMING
2. The issue with the given code lies in how the multiplication of x and y is handled
before being assigned to z. Here's the explanation:
int x = 3000, y = 2000;
long int z = x * y;
- x and y are declared as int values are limited by the range of the int type
- z is declared as long int, which has a larger range.
When x * y is computed, both x and y are int types. The result of their multiplication is
computed in int type, not long int, which exceeds the maximum range of a 16-bit int
Overflow Occurs: Since x * y exceeds the int range, it causes integer overflow, resulting
in an incorrect value being stored in z. The value stored in z is incorrect
Correct Solution:
To fix this issue, we use explicit casting by putting to long int before the multiplication
int x = 3000, y = 2000;
long int z = (long int)x * y;
Now the multiplication is done in the long int range, preventing overflow, and z will
correctly hold 6,000, 000.
5. To read data from a memory location using a segment and offset, you can add the
segment and offset to get the actual memory address and then use a pointer to access
the data.
Code Example:
#include <stdio.h>
#include <stdint.h> // For uintptr_t
int main() {
unsigned int segment = 0x1000; // Example segment
unsigned int offset = 0x200; // Example offset
uintptr_t physical_address = (uintptr_t)(segment * 16 + offset);
printf("Simulated physical memory address: 0x%X\n", (unsigned int)physical_address);
unsigned int* ptr = (unsigned int*)malloc(sizeof(unsigned int));
if (ptr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
*ptr = 42; // Example write to memory
printf("Data at allocated memory: %d\n", *ptr);
// Free allocated memory
free(ptr);
return 0;
}
OUTPUT
Explanation:
Segment + Offset: Adds the segment and offset to form the full memory address.
Pointer: The pointer ptr is used to access the data at the specified address.
This approach works in low-level programming but is generally avoided in modern
systems for safety.
C++ PROGRAMMING
1. CODE EXPLANATION:
class Sample {
public:
int *ptr;
Sample(int i) {
ptr = new int(i); // Dynamically allocate memory and store the value i.
}
~Sample() {
delete ptr; // Deallocate memory for the pointer ptr when the object is destroyed
}
void PrintVal() {
cout << "The value is " << *ptr; // Dereference the pointer to print the value
}
};
void SomeFunc(Sample x) {
cout << "Say I am in SomeFunc " << endl;
// x goes out of scope and its destructor is called at the end of the function.
}
int main() {
Sample s1 = 10; SomeFunc(s1); s1.PrintVal(); // Calls the constructor, dynamically allocates memory, and
assigns 10.
// Passes s1 to SomeFunc. This causes a copy of s1 to be made.
// Prints the value stored in s1.
}
OUTPUT
Say I am in SomeFunc
The value is 10
EXPLANATION
2. The parameter that is automatically added to every non-static member function when it
is called is the this pointer.
Explanation:
The this pointer is an implicit pointer that refers to the current instance of the object.
It is automatically passed to every non-static member function of a class when that
function is called, allowing the function to access and modify the object's non-static
Members.
For example:
class MyClass {
public:
int value;
void setValue(int val) {
this->value = val; // 'this' pointer is used to refer to the object's 'value'
}
};
Here, this->value refers to the value of the current object of type MyClass. You don't need
to explicitly pass this; it is automatically available inside non-static member functions
TECHNICAL QUESTION
Data Structures
A data structure is a way of organizing and storing data so that it can be accessed and
modified efficiently. Ex: arrays, linked lists, stacks, queues, trees, and graphs.
2. List out the areas in which data structures are applied extensively?
Unix
DBMS
1. What is a database?
A database is a collection of structured data stored in a systematic way that allows easy
retrieval, insertion, updating, and deletion of data. It is managed by a Database
Management System (DBMS).
2. What is a key? What are different keys in a database?
A key is a field (or a set of fields) used to identify records in a database.
Types of keys include:
- Primary Key: Uniquely identifies each record in a table.
- Foreign Key: A field in one table that uniquely identifies a record in another table.
- Candidate Key: A set of fields that could be used as a primary key.
- Composite Key: A key formed by combining multiple columns to uniquely identify a
record.
- Unique Key: Ensures that all values in a column are distinct.
Operating System
SQL