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

Vaibhav Prasad 2K22CE142 Assignment 1

The document is a lab file from Delhi Technological University focusing on Data Structures, authored by Vaibhav Prasad. It covers fundamental concepts such as Structures, Unions, Pointers, and 1D Arrays, providing definitions, usage, and relevant code examples for each topic. The aim is to revise programming prerequisites essential for efficient data organization and manipulation.

Uploaded by

sumitkmary
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)
13 views8 pages

Vaibhav Prasad 2K22CE142 Assignment 1

The document is a lab file from Delhi Technological University focusing on Data Structures, authored by Vaibhav Prasad. It covers fundamental concepts such as Structures, Unions, Pointers, and 1D Arrays, providing definitions, usage, and relevant code examples for each topic. The aim is to revise programming prerequisites essential for efficient data organization and manipulation.

Uploaded by

sumitkmary
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

Delhi Technological University

Data Structures Lab File

Subject Code- CO201(E1)

Title: Introduction to DataStructures


Name-: Vaibhav Prasad
Roll No.- 2K22/CE/142
Lab Subject: Data Structures
Submitted to: Ms. Disha Dua, Dept. of
CS&E
Introduction to Data Structures

Aim
To revise the useful concepts of programming prerequisites, including Structures, Unions,
Pointers, and 1D Arrays.

Theory

Introduction to Data Structures


Data structures are ways to organize and store data efficiently for easy access and
modification. They form the backbone of efficient programming. A data structure is a way to
organize, process, retrieve, and store data in a computer. It's a collection of data values, the
relationships between them, and the operations that can be applied to the data. Data
structures are designed to make it easier for users to access and work with the data they need.

Structures
A structure in C/C++ is a user-defined data type that allows combining data items of
different kinds. It is used to group related variables.

usage: Structures are used to group related variables, like combining a student’s name, age,
and grades into a single data type.
Example Syntax:

struct Student {

char name[50];
int age;
float marks;
};

Revelent code

struct Student {
char name[50];
int age;
float marks;
};

int main() {
struct Student s1 = {"John", 20, 85.5};
printf("Name: %s\nAge: %d\nMarks: %.2f\n", s1.name, s1.age, s1.marks);
return 0;
}
Unions
A union allows storing different data types in the same memory location. Only one member
can hold a value at any time.

Difference from Structures: In a union, all members share the same memory location, so only
one member can hold a value at any time.
Example Syntax:

union Data {

int i;
float f;
char str[20];
};

Revelent code
union Data { int i;
float f;
char str[20]

int main() { union Data d1; d1.i = 10;


printf("d1.i : %d\n", d1.i); d1.f = 220.5;
printf("d1.f : %.2f\n", d1.f); strcpy(d1.str, "C Programming"); printf("d1.str : %s\n", d1.str);
return 0;
}

Example 2
union car
{
char name[50];
int price;
};

int main()
{
union car car1, car2, *car3;
return 0;
}

Pointers
Pointers store memory addresses of other variables. They are crucial for dynamic memory
allocation and efficient function handling
Usage: They are crucial for dynamic memory allocation, arrays, and handling functions more
efficiently.

Example Syntax:
int var = 10; int *ptr = &var;
// 'ptr' now holds the address of 'var'
Revelent code

int main() {
int var = 30;
int *ptr;
ptr = &var;
printf("Value of var: %d\n", var);
printf("Address of var: %p\n", ptr);
printf("Value at ptr: %d\n", *ptr);
return 0;
}

Example 2: Working of
C++Pointers

#include <iostream>
using namespace std;
int main() {
int var = 5;

// store address of var


int* point_var = &var;

// print value of var


cout << "var = " << var << endl;

// print address of var


cout << "Address of var (&var) = "
<< &var << endl
<< endl;

// print pointer point_var


cout << "point_var = " <<
point_var << endl;

// print the content of the


address point_var points to
cout << "Content of the address
pointed to by point_var
(*point_var) = " << *point_var <<
endl;

return 0;
}
Example 3: Changing Value
Pointed by Pointers

#include <iostream>
using namespace std;
int main() {
int var = 5;

// store address of var


int* point_var = &var;

// print var
cout << "var = " << var << endl;

// print *point_var
cout << "*point_var = " <<
*point_var << endl
<< endl;

cout << "Changing value of var to


7:" << endl;

// change value of var to 7


var = 7;

// print var
cout << "var = " << var << endl;

// print *point_var
cout << "*point_var = " <<
*point_var << endl
<< endl;

cout << "Changing value of


*point_var to 16:" << endl;

// change value of var to 16


*point_var = 16;

// print var
cout << "var = " << var << endl;

// print *point_var
cout << "*point_var = " <<
*point_var << endl;
return 0;
}
1D Arrays
A 1D array is a collection of elements stored in contiguous memory. Arrays allow storing
multiple items of the same type under one name. Array is a linear data structure where all
elements are arranged sequentially. It is a collection of elements of same data type stored at
contiguous memory locations.

Usage: Arrays allow storing multiple items of the same type under a single name and accessing
them using an index.

Example Syntax:

int arr[5] = {1, 2, 3, 4, 5};

Array Declaration in C++


In C++, we can declare an array by simply specifying the data type first and then the name
of an array with its size.

data_type array_name[Size_of_array];
Example

int arr[5];

1. Initialize Array with Values in C++


We have initialized the array with values. The values enclosed in curly braces ‘{}’ are
assigned to the array. Here, 1 is stored in arr[0], 2 in arr[1], and so on. Here the size of the
array is 5.

int arr[5] = {1, 2, 3, 4, 5};


2. Initialize Array with Values and without Size in C++
We have initialized the array with values but we have not declared the length of the array,
therefore, the length of an array is equal to the number of elements inside curly braces.

int arr[] = {1, 2, 3, 4, 5};


3. Initialize Array after Declaration (Using Loops)
We have initialized the array using a loop after declaring the array. This method is generally
used when we want to take input from the user or we cant to assign elements one by one to
each index of the array. We can modify the loop conditions or change the initialization
values according to requirements.

for (int i = 0; i < N; i++) {


arr[i] = value;
}
4. Initialize an array partially in C++
Here, we have declared an array ‘partialArray’ with size ‘5’ and with values ‘1’ and ‘2’ only.
So, these values are stored at the first two indices, and at the rest of the indices ‘0’ is stored.

int partialArray[5] = {1, 2};

Relevent code

// Program to take 5 values from the user and store them in an array
// Print the elements stored in the array
#include <stdio.h>

int main() {

int values[5];

printf("Enter 5 integers: ");

// taking input and storing it in an array


for(int i = 0; i < 5; ++i) {
scanf("%d", &values[i]);
}

printf("Displaying integers: ");

// printing elements of an array


for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
return 0;
}

Examole 2:

// Program to find the average of n numbers using arrays

#include <stdio.h>

int main() {

int marks[10], i, n, sum = 0;


double average;

printf("Enter number of elements: ");


scanf("%d", &n);

for(i=0; i < n; ++i) {


printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);

// adding integers entered by the user to the sum variable


sum += marks[i];
}

// explicitly convert sum to double


// then calculate average
average = (double) sum / n;

printf("Average = %.2lf", average);

return 0;
}

Example 3: Displaying Array Elements.

#include <iostream>
using namespace std;

int main() {

int numbers[5] = {7, 5, 6, 12, 35};

cout << "The numbers are: ";

// Printing array elements


// using range based for loop
for (int n : numbers) {
cout << n << " ";
}

cout << "\nThe numbers are: ";

// Printing array elements


// using traditional for loop
for (int i = 0; i < 5; ++i) {
cout << numbers[i] << " ";
}

return 0;
}

Example 4: Take Inputs from User and Store Them in an Array.

#include <iostream>
using namespace std;

int main() {

int numbers[5];

cout << "Enter 5 numbers: " << endl;

// store input from user to array


for (int i = 0; i < 5; ++i) {
cin >> numbers[i];
}

cout << "The numbers are: ";

// print array elements


for (int n = 0; n < 5; ++n) {
cout << numbers[n] << " ";
}

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy