Exp 1 - Dsa Lab File
Exp 1 - Dsa Lab File
C programming is a foundational and widely used programming language known for its
simplicity, efficiency, and versatility. Developed in the early 1970s by Dennis Ritchie at Bell
Labs, C has since become a cornerstone of computer science and software development. Its
strength lies in its ability to provide low-level control over hardware while offering high-level
abstraction for creating complex applications. C's syntax is concise and follows a structured
approach, making it accessible to both novice and experienced programmers. It has been the
inspiration for numerous other programming languages, and its influence can be seen in many
modern systems and applications. Whether you're developing embedded systems, operating
systems, or applications, C remains a crucial language in the world of programming. Its
enduring popularity stems from its powerful features and ability to create efficient and
portable code. Despite the emergence of newer languages, C programming continues to be a
cornerstone of the computing world.
COMPONENTS OF C: -
1. Basic Structure: A C program consists of functions that contain statements. The
program starts execution from the `main () ` function.
#include <stdio.h>
int main ()
{
// Code
return 0;
}
```
2. Data Types: C provides various data types, including int, float, double, char, etc. You
can also create your own data types using structures and unions.
3. Variables: Variables are used to store data. They must be declared with a data type
before they can be used.
int age = 30;
float price = 19.99;
char grade = 'A';
```
4. Operators: C provides a wide range of operators for performing operations on variables
and values. These include arithmetic, comparison, logical, and bitwise operators.
5. Control Structures: C supports conditional statements (if, else, switch) and loops (for,
while, do-while) for controlling the flow of a program.
6. Functions: Functions are blocks of code that can be called and executed. You can define
your own functions in C.
int add(int a, int b)
{
return a + b;
}
```
7. Arrays: Arrays are collections of elements of the same data type. They can be one-
dimensional or multi-dimensional.
int numbers[5] = {1, 2, 3, 4, 5};
```
8. Pointers: Pointers are variables that store memory addresses. They are often used for
dynamic memory allocation and manipulation.
int x = 10;
int* ptr = &x;
```
9. Structures and Unions: These are user-defined data types that allow you to group
different variables together.
struct Person
{
char name[50];
int age;
};
```
10. File Handling: C provides functions for reading from and writing to files.
#include <stdio.h>
FILE *fp;
fp = fopen("example.txt", "w");
fprintf(fp, "This is an example.\n");
fclose(fp);
```
12. Header Files: C libraries and your own code can be organized into header files for
better code structure.
13. Dynamic Memory Allocation: C allows you to allocate memory dynamically using
functions like `malloc`, `calloc`, and `realloc`.
int *arr = (int *)malloc(5 * sizeof(int));
```
14. Standard Library: C comes with a standard library that includes many useful
functions, like those in `stdio.h` for input and output.
Program in c to interchange the value of two variables using function with
pointers .
ALGORITHM: -
1.We define a swap function that takes two integer pointers as parameters.
2.Inside the swap function, we use a temporary variable to store the value pointed to by a.
3.We then update the value at the address pointed to by a with the value pointed to by b.
4.Finally, we update the value at the address pointed to by b with the value stored in the
temporary variable.
When you run the program, it will swap the values of x and y, resulting in x having the
original value of y, and y having the original value of x.
CODE:-
#include<stdio.h>
void swap(int*, int*);
int main()
{
int a, b;
printf("Enter values for a and b\n");
scanf("%d%d", &a, &b);
printf("\n\nBefore swapping: a = %d and b = %d\n", a, b);
swap(&a, &b);
printf("\nAfter swapping: a = %d and b = %d\n", a, b);
return 0;
}
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
OUTPUT:-
C program to add two numbers using pointers.
ALGORITHM: -
We declare two integer variables num1 and num2, which hold the numbers you want
to add.
We declare an integer variable sum to store the sum.
We create two integer pointers, p and q, and assign them the addresses of num1 and
num2, respectively.
We use the * operator to access the values pointed to by p and q and add them
together, storing the result in the sum variable.
CODE: -
#include <stdio.h>
int main()
{
int first, second, *p, *q, sum;
p = &first;
q = &second;
sum = *p + *q;
return 0;
}
OUTPUT: -
Given an array arr[] of size N.The task is to rotate by d position to the left.
Rotations in the array are defined as the process of rearranging the elements in an array by
shifting each element to a new position. This is mostly done by rotating the elements of the
array clockwise or counterclockwise.
ALGORITHM:-
For Rotating the array to left by d position,the following steps should be undertaken:-
Store the first element of the array in a temporary variable.
Shift the rest of the elements in the original array by d place.
Update the last index of the array with the temporary variable.
Repeat the above steps for the number of left rotations required.
CODE:-
#include<stdio.h>
void PrintArray(int a[],int N)
{
for(int i=0;i<N;i++)
{
printf("%d\t",a[i]);
}
}
int main()
{
int i,j,d,temp,N;
printf("Enter the size of the array:");
scanf("%d",&N);
int a[N];
printf("Enter the array elements:");
for (i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
printf("\n Number of times to left rotate an array:");
scanf("%d",&d);
for (i=0;i<d;i++)
{
temp=a[0];
for (int j=0;j<N-1;j++)
{
a[j]=a[j+1];
}
a[N-1]=temp;
}
printf("\n Array elements after left rotating Array:");
PrintArray(a,N);
printf("\n");
}
OUTPUT:-