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

Ds Assignment 01

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 views11 pages

Ds Assignment 01

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/ 11

Enrollment number:- 0801IT231048

Lab assignment 01
Que1) Write a program to print largest number among the given number in an array using
dynamic array.

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

int main(){
printf("Divyansh chourey \n0801IT231048\n");
int n;
printf("Enter the size of array: ");
scanf("%d", &n);
int *ptr;
ptr = (int*)malloc(n*sizeof(int));
for(int i=0; i<n; i++){
printf("Enter the element %d: ", i+1);
scanf("%d", &ptr[i]);
}
printf("the array is: ");
for(int i=0; i<n; i++){
printf("%d ", ptr[i]);
}
printf("\n");
int largest = ptr[0];
for(int i=0; i<n; i++){
if(ptr[i]>largest){
largest = ptr[i];
}
}
printf("Largest element is: %d", largest);

free(ptr);
return 0;
}
Enrollment number:- 0801IT231048
Output:
Enrollment number:- 0801IT231048
Que2) Write a program to print smallest number among the given number in an array using
dynamic array.

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

int main(){
printf("Divyansh chourey \n0801IT231048\n");
int n;
printf("Enter the size of array: ");
scanf("%d", &n);
int *ptr;
ptr = (int*)malloc(n*sizeof(int));
for(int i=0; i<n; i++){
printf("Enter the element %d: ", i+1);
scanf("%d", &ptr[i]);
}
printf("the array is: ");
for(int i=0; i<n; i++){
printf("%d ", ptr[i]);
}
printf("\n");
int smallest = ptr[0];
for(int i=0; i<n; i++){
if(ptr[i]<smallest){
smallest = ptr[i];
}
}
printf("Smallest element is: %d", smallest);

free(ptr);
return 0;
}
Enrollment number:- 0801IT231048
Output:
Enrollment number:- 0801IT231048
Que3) Write a program to print element of array in reverse order.

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

int main(){
printf("Divyansh chourey \n0801IT231048\n");
int n;
printf("Enter the size of array: ");
scanf("%d", &n);
int *ptr;
ptr = (int*)malloc(n*sizeof(int));
for(int i=0; i<n; i++){
printf("Enter the element %d: ", i+1);
scanf("%d", &ptr[i]);
}
printf("the array is: ");
for(int i=n-1; i>=0; i--){
printf("%d ", ptr[i]);
}
printf("\n");

free(ptr);
return 0;
}

Output:
Enrollment number:- 0801IT231048
Que4) Write a program to find the occurrence of duplicate elements in the array.

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

void findDuplicates(int arr[], int n){


int i,j,count;
for(i=0; i<n; i++){
count = 0;
for(j = i+1; j<n; j++){
if(arr[i] == arr[j]){
count++;
}
}
if(count>0){
printf("%d occurs %d times\n", arr[i], count+1);
}
}
}

int main(){
printf("Divyansh chourey \n0801IT231048\n");
int n;
printf("Enter the size of array: ");
scanf("%d", &n);
int *ptr;
ptr = (int*)malloc(n*sizeof(int));
for(int i=0; i<n; i++){
printf("Enter the element %d: ", i+1);
scanf("%d", &ptr[i]);
}
printf("the array is: ");
for(int i=0; i<n; i++){
printf("%d ", ptr[i]);
}
printf("\n");

findDuplicates(ptr, n);

return 0;
}
Enrollment number:- 0801IT231048
Output:
Enrollment number:- 0801IT231048
Que5. Write a program to take an array as input from user and find out sum of odd and even
number in an array.

Input:
#include<stdio.h>
#include<stdlib.h>
int main(){
int n;
printf("Divyansh chourey \n0801IT231048\n");
printf("Enter the size of array: ");
scanf("%d", &n);
int *ptr = (int*)malloc(n*sizeof(int));
for(int i=0; i<n; i++){
printf("enter element %d: ", i+1);
scanf("%d", &ptr[i]);
}
int even=0, odd=0;
for(int i=0; i<n; i++){
if(ptr[i]%2 == 0){
even += ptr[i];
} else {
odd += ptr[i];
}
}
printf("Sum of even numbers is: %d \n", even);
printf("Sum of odd number is: %d \n", odd);
return 0;
}

Output:
Enrollment number:- 0801IT231048
Que 6. Write a program to take an array as input from user and sort the data in ascending
order.

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

int main(){
printf("Divyansh chourey \n0801IT231048\n");
int n;
printf("Enter the size of array: ");
scanf("%d", &n);
int *ptr;
ptr = (int*)malloc(n*sizeof(int));
for(int i=0; i<n; i++){
printf("Enter the element %d: ", i+1);
scanf("%d", &ptr[i]);
}
printf("the sorted array is: ");
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
if(ptr[i]>ptr[j]){
int temp = ptr[i];
ptr[i] = ptr[j];
ptr[j] = temp;
}
}
}

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


printf("%d ", ptr[i]);
}
printf("\n");

free(ptr);
return 0;
}
Enrollment number:- 0801IT231048
Output:
Enrollment number:- 0801IT231048

You might also like