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

Assignment (DSA)

The document contains four C programming tasks related to data structures, including finding the sum of elements in a 1-D array, computing the transpose of a 2-D array, implementing a stack using an array, and performing bubble sort on an array. Each task includes a program code snippet and instructions for user input and output. The document also specifies that screenshots of the program outputs should be attached after each program.

Uploaded by

deepmoinaboruah7
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)
4 views8 pages

Assignment (DSA)

The document contains four C programming tasks related to data structures, including finding the sum of elements in a 1-D array, computing the transpose of a 2-D array, implementing a stack using an array, and performing bubble sort on an array. Each task includes a program code snippet and instructions for user input and output. The document also specifies that screenshots of the program outputs should be attached after each program.

Uploaded by

deepmoinaboruah7
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

DATA STRUCTURS PRACTICAL (PR:2.

2)
Write user defined program for following and compile the programs and attach the screenshot of output
after each program (any four)

Q1. Write a C program to find the sum of elements in a 1-D array.

Ans:
Program:
#include <stdio.h>

// Function to calculate the sum of elements in an array


int sumOfArray(int arr[], int size) {
int sum = 0;
for(int i=0; i < size; i++) {
sum +=arr[i];
}
return sum;
}

int main() {
int size;
// Input the size of the array
printf("Enter the number of element in the array : ");
scanf("%d",&size);

int arr[size];

//Input the element of the array


printf("Enter the element of the array:\n");
for(int i=0; i<size; i++) {
scanf("%d",&arr[i]);
}

// Calculate the Sum of the element


int sum = sumOfArray(arr, size);

// output the result


printf("The sum of the elements in the array in the array is: %d\n", sum);

return 0;
}

Output:
Q2. Write a C program to compute the transpose of a 2-D array.
Ans:
Program:
#include<stdio.h>

//Function to compute the transpose of a 2-D array


void transpose(int rows, int cols, int array[rows][cols],int transposed[cols][rows]) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
transposed[j][i] = array[i][j];
}
}
}

int main() {
int rows, cols;

// Input the number of rows and columns of the array


printf("Enter the number of rows: ");
scanf("%d",&rows);
printf("Enter the number of columns: ");
scanf("%d",&cols);

int array[rows][cols];
int transposed[cols][rows];

// Input the elements of array


printf("Enter the elements of the array:\n");
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
scanf("%d",&array[i][j]);
}
}

// compute the transpose of the array


transpose(rows, cols, array, transposed);

// Output the transposed array


printf("The transposed array is: \n");
for(int i = 0; i < cols; i++) {
for(int j = 0; j < rows; j++) {
printf("%dt", transposed[i][j]);
}
printf("\n");
}
return 0;
}
Output:

Q3. Write a C program to implement a stack using an array.


Ans:
Program:
#include<stdio.h>
#include<stdlib.h>

#define MAX 100 //Maximum size of the stack

typedef struct {
int items[MAX];
int top;
} Stack;
// Function to initialize the stack
void initialize(Stack *s) {
s->top = -1;
}

// Function to check if the stack is full


int isFull(Stack *s) {
return s->top == MAX -1;
}

// Function to check if the stack is empty


int isEmpty(Stack *s) {
return s->top == -1;
}

// Functions to push an element onto the stack


void push(Stack *s, int value) {
if (isFull(s)) {
printf("Stack overflow\n");
} else {
s->items[++(s->top)] = value;
printf("pushed %d onto the stack\n",value);
}
}

// Function to pop an element from the stack


int pop(Stack *s) {
if(isEmpty(s)) {
printf("Stack underflow\n");
return -1;
} else {
return s->items[(s->top)--];
}
}

// Function to peek an element from the stack


int peek(Stack *s) {
if (isEmpty(s)) {
printf("Stack is empty\n");
return -1;
} else {
return s->items[s->top];
}
}

int main() {
Stack s;
initialize(&s);
int choice, value;

while (1)
{
printf("\nStack Menu\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("4. Check if Empty\n");
printf("5. Check if Full\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice)
{
case 1:
printf("Enter the value to Push: ");
scanf("%d", &value);
push(&s, value);
break;
case 2:
value = pop(&s);
if (value != -1) {
printf("Popped %d from the stack\n",value);
}
break;
case 3:
value = peek(&s);
if (value != -1) {
printf("Top element is %d\n",value);
}
break;
case 4:
if (isEmpty(&s)) {
printf("Stack is empty\n");
} else {
printf("Stack is notEmpty");
}
break;
case 5:
if (isFull(&s)) {
printf("Stack is Full\n");
} else {
printf("Stack is not Full");
}
break;
case 6:
exit(0);
default:
printf("Invalid choice\n");
break;
}
}

return 0;
}

Output:
Q3. Write a C program to implement a stack using array.
Ans:
Program
#include<stdio.h>

// Function to perfrom bubble sort


void bubblesort(int arr[], int n) {
int i, j, temp;
for(i = 0; i<n-1; i++) {
// Last i elements are already sorted
for(j = 0; j < n-i-1; j++) {
// Swap if the element found is greater than the next element
if(arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}

// Function to print ana array


void printArray(int arr[], int size) {
int i;
for(i = 0; i < size; i++) {
printf("%4d",arr[i]);
}
printf("\n");
}

int main() {
int n, i;

// Input the number of elements in the array


printf("Enter the number of elements in the array: ");
scanf("%d", &n);

int arr[n];

// Input the elements of the array


printf("Enter the elements of the array:\n");
for(i = 0; i<n; i++) {
scanf("%d", &arr[i]);
}

// Perfrom bubble sort


bubblesort(arr, n);

// Output the sorted array


printf("Sorted array: \n");
printArray(arr, n);

return 0;
}

Output:

You might also like